pub trait Task: 'static + Send {
// Required methods
fn id(&self) -> TaskId;
fn run<'async_trait>(
self: Box<Self>,
stop_receiver: StopReceiver,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait;
// Provided method
fn kind(&self) -> TaskKind { ... }
}
Expand description
A task implementation. Task defines the “runnable” concept of the service, e.g. a unit of work that can be executed by the service.
Based on the task kind, the implemenation will be treated differently by the service.
§Task kinds
There may be different kinds of tasks:
§Task
A regular task. Returning from this task will cause the service to stop. Task::kind
has a default
implementation that returns TaskKind::Task
.
Typically, the implementation of Task::run
will be some form of loop that runs until either an
irrecoverable error happens (then task should return an error), or stop signal is received (then task should
return Ok(())
).
§OneshotTask
A task that can exit when completed without causing the service to terminate.
In case of OneshotTask
s, the service will only exit when all the OneshotTask
s have exited and there are
no more tasks running.
§Precondition
A “barrier” task that is supposed to check invariants before the main tasks are started. An example of a precondition task could be a task that checks if the database has all the required data. Precondition tasks are often paired with some other kind of task that will make sure that the precondition can be satisfied. This is required for a distributed service setup, where the precondition task will be present on all the nodes, while a task that satisfies the precondition will be present only on one node.
§UnconstrainedTask
A task that can run without waiting for preconditions. Tasks of this kind are expected to check all the invariants they rely on themselves. Usually, this kind of task is used either for tasks that must start as early as possible (e.g. healthcheck server), or for tasks that cannot rely on preconditions.
§UnconstrainedOneshotTask
A task that can run without waiting for preconditions and can exit without stopping the service. Usually such tasks may be used for satisfying a precondition, for example, they can perform the database setup.