pub struct Worker { /* private fields */ }Expand description
One worker. Cloning is cheap and gives a second worker on the same queue.
Implementations§
Source§impl Worker
impl Worker
pub fn new(queue: Arc<dyn Queue>, registry: Arc<JobRegistry>) -> Self
Sourcepub fn reporting_to(self, store: Arc<dyn ProgressStore>) -> Self
pub fn reporting_to(self, store: Arc<dyn ProgressStore>) -> Self
Give jobs somewhere to report progress. The web process reads the same store, so it must be one both can reach — the database one, unless the worker runs inside the web process.
pub fn on_queue(self, queue: impl Into<String>) -> Self
pub fn poll_interval(self, interval: Duration) -> Self
Sourcepub fn max_jobs(self, count: u64) -> Self
pub fn max_jobs(self, count: u64) -> Self
Stop after count attempts, whatever the shutdown signal says.
pub fn backoff_cap(self, cap: Duration) -> Self
pub fn options(&self) -> &WorkerOptions
Sourcepub async fn run(&self, shutdown: Shutdown) -> Result<WorkerStats>
pub async fn run(&self, shutdown: Shutdown) -> Result<WorkerStats>
Work until shutdown is signalled or max_jobs is reached.
The shutdown check is at the top of the loop and nowhere else. That is the entire graceful-shutdown guarantee: a job already reserved is always run to completion, because there is no point at which the loop can abandon one.
An error from the queue is not an error from the worker. This is a
long-running process and keeping going is the only thing it is for, so
a failure to reach the queue — a deadlock, a dropped connection, a
failover — is logged and retried with a growing pause, and the loop
carries on. It used to propagate straight out: one deadlock on one poll
ended the worker, and run_pool joining its tasks with ? ended the
whole queue:work process, mid-queue, with jobs still waiting.
The pause starts at the poll interval, doubles, and is capped at
MAX_QUEUE_ERROR_BACKOFF; it resets the first time the queue answers.
It is cut short by shutdown, like every other wait here.
The Result stays in the signature so callers do not change, but this
no longer returns Err for anything the queue did.
Sourcepub async fn run_once(&self) -> Result<Option<Outcome>>
pub async fn run_once(&self) -> Result<Option<Outcome>>
Reserve and run at most one job. None when the queue had nothing ready.
Sourcepub async fn process(&self, reserved: ReservedJob) -> Result<Outcome>
pub async fn process(&self, reserved: ReservedJob) -> Result<Outcome>
Run one reserved job and settle its fate.