|
5 months ago | |
---|---|---|
.github/workflows | 5 months ago | |
test | 7 months ago | |
.gitignore | 7 months ago | |
CHANGELOG.md | 7 months ago | |
LICENSE | 7 months ago | |
Makefile | 7 months ago | |
README.md | 5 months ago | |
clihelpers.l | 7 months ago | |
module.l | 7 months ago | |
supervisor.l | 6 months ago | |
test.l | 7 months ago | |
testapp.l | 7 months ago |
This program mimics functionality of Unicorn, without being limited to HTTP applications.
The included supervisor.l
can be used to spawn multiple worker processes which perform tasks in an infinite loop.
v17.12
to v20.6.29
This library is written in pure PicoLisp and contains no external dependencies.
To ensure everything works on your system, run the tests first: make check
Launch the Supervisor with: ./supervisor.l --app testapp.l
You’ll see output showing 1 worker “Performing a task” in a loop. Wait until it completes a task or two, then press Ctrl+C
or send an INT
signal to tell the parent process to exit, along with its worker.
# example output
./supervisor.l --app testapp.l --workers 2
parent process ready pid=814
spawning 2 missing workers:
worker[0] spawning..
worker[1] spawning..
worker[0] spawned pid=815
worker[0] pid=815 do this after forking
worker[0] ready
worker[0] pid=815 Performing a task: sleeping for 12 seconds
worker[1] spawned pid=816
worker[1] pid=816 do this after forking
worker[1] ready
worker[1] pid=816 Performing a task: sleeping for 9 seconds
worker[1] pid=816 Performing a task: sleeping for 16 seconds
^Cworker[1] exited
worker[0] exited
parent exited
Feel free to observe the example code in testapp.l.
The supervisor runs in the foreground and simply manages the worker processes. If a worker (child process) exits, it will be re-spawned automatically by the supervisor. The supervisor periodically checks for missing workers, depending on the *SV_POLL_TIMEOUT
variable.
# supervisor.l
Usage: ./supervisor.l --app <yourapp> [option] [arguments]
Example: ./supervisor.l --app app.l --workers 4 --poll 1
Options:
--help show this help message and exit
--app <yourapp> Filename of the app which contains (worker-start)
--poll <seconds> Number of seconds to poll for missing workers (default: 30)
--preload Load the app in the parent before forking the worker process (default: No)
--workers <number> Number of workers to spawn (default: 1)
--app
: This option accepts 1 argument, a PicoLisp .l
file. It will be (load)
ed in the forked process of each worker (each time a worker is spawned), unless --preload
is specified. Once the process is forked, the (worker-start)
function will be called automatically, so that needs to be defined in your worker app.--preload
: This options takes no arguments. If --preload
is specified, the worker app will be loaded only once, in the parent process. Its code will be inherited by each worker process as it’s spawned. This should be more memory efficient (and faster) for large applications, but prevents changing the worker app “on the fly” (without restarting the supervisor).--poll
: This option accepts 1 argument, the number of seconds where the parent
process will sleep before checking for any missing workers. For processes which take a long time to complete, or for non-busy servers, it’s probably safe to set the polling interval a bit higher (ex: 60
seconds). If there’s a need to know almost “right away” when a worker is missing, it is safe to set it to 1
.--workers
: This option accepts 1 argument, the number of workers which should be spawned. The supervisor will remember this number and always ensure that it’s maintained. If 3 or 4 workers happen to exit, the supervisor will notice and respawn 3 workers.This section will explain some important technical details about the code, and limitations on what this app can and can’t do.
*SV_
, and functions are prefixed with sv-
.(before-fork)
and (after-fork)
hooks which will be called if they’re defined in your app (totally optional). Of course, (before-fork)
happens in the parent process, right before the child is forked, and (after-fork)
happens in the child process, right after it’s forked.(before-fork)
hook will only be called when --preload
is provided, since there’s no way to call the function before the code is even loaded.(before-fork)
and (after-fork)
. This can be used in the app to conditionally perform tasks based on its ID (ex: worker ID 0 could verify the integrity of a database, while the other workers simply query it)./dev/null
if needed.kill -9
(or kill -KILL
), and the workers will continue their work and exit cleanly when they’re done.kill -15
or kill -TERM
) will terminate all workers and the parent immediately.supervisor.l
in PicoLisp, feel free to use those techniques if you prefer.This library comes with very basic integration tests. To run the tests, type:
make check
(native)
) should conditionally check for OS support.Copyright (c) 2020 Alexander Williams, On-Prem license@on-premises.com