Skip to content

daemon.sh

Timothée Mazzucotelli edited this page Oct 10, 2018 · 4 revisions

daemon.sh

Provide functions to ease creation of daemon scripts.

Description

Simple consumers:

Multiple consumers can consume the same directory. Each time a consumer wants to process a file, it tries to lock it. To lock it, it tries to create a directory named after the sha256 sum of the file name (file being a regular file or a directory). If the lock fails, it means the file is being processed by another consumer, and the consumer continue to the next file. If the lock succeeds, the consumer process the file, move the file, and then remove the lock. If all files in the directory are locked, consumers wait a certain amount of time before trying again to process files. If the directory is empty, consumers wait a certain amount of time before listing it again.

Chained-consumers:

Multiple directories can each be consumed by several consumers (note: a consumer consumes one and only one directory). The processed files transit from one directory to another, until they finally land in a not-consumed directory. In each directory, they are processed accordingly to what the consumers for this directory are doing (example: filter video files -> extract audio -> reencode to specific format -> normalize to N decibels -> move to final music folder). For a particular directory, consumers behave exactly like simple consumers, except for the following:

In addition to setting a lock for the current directory when processing a file, consumers also set a lock (with the potential post-process name of the file) for the next directory in the chain. This is done to avoid files being processed by the next consumers before the files are completely moved to the next directory.

Environment Variables

  • CONSUMED_DIR: Path to directory to consume.
  • WAIT_WHEN_EMPTY: Time to wait (in seconds) when consumed directory is empty.
  • WAIT_WHEN_LOCKED: Time to wait (in seconds) when all items in consumed directory are locked.

Functions

sha STRING

Compute sha256sum of string.

Arguments

  • STRING: String to compute sum for.

consumer_lock NAME [DIR]

Lock the given item thanks to its name.

Arguments

  • NAME: Name of the item to lock.
  • DIR: Directory in which to create the lock (default to data).

consumer_unlock NAME [DIR]

Unlock the given item thanks to its name.

Arguments

  • NAME: Name of the item to unlock.
  • DIR: Directory in which to remove the lock (default to data).

consumer_locked NAME [DIR]

Test if NAME is locked.

Arguments

  • NAME: Name of the item to test.
  • DIR: Directory in which to check the lock (default to data).

consumer_unlocked NAME [DIR]

Test if NAME is unlocked.

Arguments

  • NAME: Name of the item to test.
  • DIR: Directory in which to check the lock (default to data).

consumer_get FILE...

Lock then move each given file into consumed directory.

Arguments

  • FILE: Single or multiple files to move into consumed directory.

consumer_send DAEMON FILE...

Lock then move each given file to consumed directory of DAEMON.

Arguments

  • DAEMON: The daemon to send the files to (into its consumed directory).
  • FILE: Single or multiple files to move into consumed directory.

consumer_location

Return the path to the consumed directory.


consumer_empty [DIR]

Test if consumed directory is empty.

Arguments

  • DIR: Directory to check (default to consumed directory).

consumer_consume NAME

Consume (process) file identified by NAME. You must rewrite this function.

Arguments

  • NAME: Name of the file/folder to process.

Wiki page generated with shellman.