Add a task to the queue.
Usage: add_task typ execute
typ
is a type description for values returned by execute
. When a worker is ready to execute this task, this worker will run execute
. Note that execute
is serialized to the worker using Marshal
. If this closure captures some variables, those variables should thus be serializable using Marshal
.
add_task
can be called before run
, or while run
is running (i.e. from an event handler like on_start
, on_message
, on_finish
; or the on_empty_queue
argument of run
).
If term_timeout
is specified, sigterm
is sent to the worker if the task has not finished (successfully or not) after term_timeout
seconds. sigterm
defaults to Sys.sigterm
.
If kill_timeout
is specified but term_timeout
is not, SIGKILL
is sent to the worker if the task has not finished (successfully or not) after kill_timeout
seconds.
If both term_timeout
and kill_timeout
are specified, sigterm
is sent first, and if the task is not willing to end gracefully kill_timeout
seconds after sigterm
was sent, SIGKILL
is sent as well. Note that in that case, kill_timeout
is relative to the time sigterm
was sent, not to the time the task started.
on_start
is triggered when the task is sent to a worker. It takes a scheduler_context
argument that allows to send a message to this worker, typically with additional information that was not known at the time the task was queued, such as a free port number that the worker can use.
on_message
is triggered for each message that is sent from the worker. It also takes a scheduler_context
argument to be able to respond.
on_finish
is triggered with:
Ok result
when the task returns successfully, in which case result
is the return value of execute
;Error error_message
when the task fails, in which case error_message
can be the result of Printexc.to_string
(if execute
raised an exception) or something else (e.g. if the worker died).