Skip to main content

Worker

Struct Worker 

Source
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 turns Dead and 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 lane holds the lane in a unique column, so the database refuses a second.
  • Every day it deletes the jobs that succeeded more than keep_succeeded ago, fourteen days by default. A dead job stays.

Implementations§

Source§

impl Worker

Source

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.

Source

pub fn register<J: Job>(self) -> Self

Runs the jobs of kind J. Registering a kind again changes nothing.

Source

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.

Source

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.

Source

pub fn auth_mail(self, queue: &AuthMailQueue) -> Self

Sends the letters queue queues, through the Mailer the context adds. See AuthMailQueue.

Source

pub fn keep_succeeded(self, age: Duration) -> Self

Keeps a succeeded job for age before the daily prune deletes it.

Source

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.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Clone for Worker

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more