pub struct Worker { /* private fields */ }Expand description
Runs a panel’s queued jobs: claims the due ones, runs each with a timeout against the panel’s
context, and retries a failure with backoff.
let (stop, stopped) = tokio::sync::watch::channel(false);
let worker = Worker::new(panel)?
.every(Duration::from_hours(6), ReconcileBilling)
.auth_mail(&mail)
.context(move |cx| cx.with(stripe.clone()));
let running = tokio::spawn(worker.run(stopped));
// On a shutdown signal:
stop.send(true).ok();
running.await.ok();- Delivery is at least once. A worker that stops mid-run leaves the job’s lock to lapse, a
minute past its kind’s
TIMEOUT, and the job runs again after its backoff. - A run that fails, or outlasts its
TIMEOUT, waits from 30 seconds, doubling up to an hour, with up to a quarter more drawn from the job’s id. A job out of attempts turnsDeadand stays until someone queues it again. - A worker claims only the kinds it
registers, so workers may split the kinds between them. A job of a kind no worker registers, such as one a newer release queued, waits for a worker that does. - Any number of workers share one queue. A claim is a version-checked update, so each job runs
once per claim, and the running job of a
laneholds the lane in a unique column, so the database refuses a second. - Every day it deletes the jobs that succeeded more than
keep_succeededago, fourteen days by default. A dead job stays.
Implementations§
Source§impl Worker
impl Worker
Sourcepub fn new(panel: &PanelHandle) -> Result<Self>
pub fn new(panel: &PanelHandle) -> Result<Self>
A worker running jobs in panel’s database, against panel’s context.
§Errors
When panel’s context holds no database, which a handle from
Panel::handle or a router always does.
Sourcepub fn register<J: Job>(self) -> Self
pub fn register<J: Job>(self) -> Self
Runs the jobs of kind J. Registering a kind again changes nothing.
Sourcepub fn every<J: Job + Clone>(self, every: Duration, job: J) -> Self
pub fn every<J: Job + Clone>(self, every: Duration, job: J) -> Self
Registers J and enqueues job every every, the first run one interval after any
worker first sees the schedule. However many workers run, each period enqueues once, and
a run that has not started absorbs the next. Scheduling a kind again replaces its schedule.
Sourcepub fn context(self, extend: impl Fn(Cx) -> Cx + Send + Sync + 'static) -> Self
pub fn context(self, extend: impl Fn(Cx) -> Cx + Send + Sync + 'static) -> Self
Adds to each run’s context what its jobs read beyond the panel’s: a mail transport, an API
client. extend receives the panel’s context and returns it with its additions. Each run
gets a context of its own, since a context memoizes what it loads. Calling it again
replaces the previous extend.
Sourcepub fn auth_mail(self, queue: &AuthMailQueue) -> Self
pub fn auth_mail(self, queue: &AuthMailQueue) -> Self
Sends the letters queue queues, through the Mailer the
context adds. See AuthMailQueue.
Sourcepub fn keep_succeeded(self, age: Duration) -> Self
pub fn keep_succeeded(self, age: Duration) -> Self
Keeps a succeeded job for age before the daily prune deletes it.
Sourcepub async fn run(self, shutdown: Receiver<bool>)
pub async fn run(self, shutdown: Receiver<bool>)
Polls for due jobs until shutdown holds true or its sender drops, then lets the runs
in hand finish. A slot a run frees takes the next due job at once, and the sweep keeps its
pace under any load.
Sourcepub async fn drain(&self) -> Result<()>
pub async fn drain(&self) -> Result<()>
Requeues the jobs whose lock lapsed and enqueues the due schedules, then runs every due job until none is left: what a test calls to say “the worker ran”.
§Errors
When the driver fails to claim jobs.
Sourcepub async fn drain_once(&self) -> Result<usize>
pub async fn drain_once(&self) -> Result<usize>
Claims up to a batch of due jobs, runs them to their end, and answers how many it claimed.
§Errors
When the driver fails to claim jobs. A job’s own failure is recorded on its row instead.