Skip to main content

Job

Struct Job 

Source
pub struct Job {
Show 14 fields pub id: Uuid, pub kind: String, pub dedup_key: String, pub payload: Value, pub status: String, pub run_at: i64, pub attempts: i64, pub max_attempts: i64, pub deadline: Option<i64>, pub lease_until: Option<i64>, pub lease_owner: Option<String>, pub last_error: Option<String>, pub created_at: i64, pub updated_at: i64,
}
Expand description

One stored job row.

status and kind come back as the strings they were stored as, for the reason crate::sqlite::audit::AuditEntry keeps event a String: an older binary meeting a row a newer one wrote should render it, not refuse to load. The runner never claims a kind its registry does not hold, so an unrecognised one is simply left alone.

Fields§

§id: Uuid§kind: String§dedup_key: String§payload: Value§status: String§run_at: i64§attempts: i64§max_attempts: i64§deadline: Option<i64>§lease_until: Option<i64>§lease_owner: Option<String>§last_error: Option<String>§created_at: i64§updated_at: i64

Implementations§

Source§

impl Job

Source

pub async fn enqueue( row: NewJob<'_>, database: &Database, ) -> Result<bool, Error>

Queues one job, unless a live one already holds (kind, dedup_key).

Ok(false) is “already queued”, not an error: it is what two racing callers must both be able to survive, and the partial unique index is what decides which of them won. A job that already reached done or failed releases the identity, so the same key can be queued again — which is what makes a retried order and a periodic sweep both expressible without a second table.

Source

pub async fn claim_next( runner_id: &str, kinds: &[&str], lease_until: i64, now: i64, database: &Database, ) -> Result<Option<Self>, Error>

Claims the oldest eligible job of one of kinds, or None.

One statement, and that is the point: the subselect picks a candidate and the outer AND status = 'ready' is what makes the pick binding, so two runners choosing the same row end with exactly one write. None collapses “the queue was empty” and “somebody else won” — which is correct, because the caller does the same thing either way.

attempts increments here rather than at completion: a job that reliably kills the process must still exhaust its budget, and nothing reports back from a process that died.

deadline is deliberately not filtered here. Skipping a dead row would leave it ready and re-read on every tick for ever; the runner claims it and retires it on the spot.

Source

pub async fn complete( id: Uuid, runner_id: &str, database: &Database, ) -> Result<bool, Error>

Marks a claimed job finished. Ok(false) means the lease was lost.

Source

pub async fn retry( id: Uuid, runner_id: &str, run_at: i64, error: &str, database: &Database, ) -> Result<bool, Error>

Returns a claimed job to the queue, to run again at run_at.

Source

pub async fn reschedule( id: Uuid, runner_id: &str, run_at: i64, database: &Database, ) -> Result<bool, Error>

Returns a claimed job to the queue as a fresh occurrence: the attempt counter goes back to zero and the last error is cleared.

That reset is what separates a periodic job from a retried one. A sweep that runs every day for a year must not accumulate 365 attempts and retire itself, and a successful occurrence must not leave the previous failure’s text sitting on the row as though it were current.

Source

pub async fn abandon( id: Uuid, runner_id: &str, error: &str, database: &Database, ) -> Result<bool, Error>

Retires a claimed job permanently, recording why.

Source

pub async fn reclaim_expired( now: i64, database: &Database, ) -> Result<u64, Error>

Returns to the queue every job whose runner died holding its lease.

attempts is deliberately left alone: the attempt really was spent, and a counter rewritten to look better would let a job that crashes the process loop for ever.

Source

pub async fn release_owned( runner_id: &str, database: &Database, ) -> Result<u64, Error>

Releases every lease this runner holds, without settling the jobs.

What a graceful shutdown runs, so a restart re-claims its own work immediately instead of waiting out a full lease.

Source

pub async fn find_by_id( id: Uuid, database: &Database, ) -> Result<Option<Self>, Error>

One row by id.

Source

pub async fn find_live( kind: &str, dedup_key: &str, database: &Database, ) -> Result<Option<Self>, Error>

The live job holding (kind, dedup_key), if there is one.

Source

pub async fn count_live(kind: &str, database: &Database) -> Result<i64, Error>

How many live jobs of kind are queued or running.

The kind-wide counterpart to Self::find_live, for the callers that want “is there work of this sort outstanding?” without naming a dedup_key — a notify_deliver key is a per-occurrence uuid, so there is no single key to ask about.

Source

pub async fn cleanup(cutoff: i64, database: &Database) -> Result<u64, Error>

Deletes terminal rows settled before cutoff, returning how many went.

Only terminal ones: a ready job scheduled far in the future is not old, however long ago it was written, and a running one is somebody’s work.

Trait Implementations§

Source§

impl Clone for Job

Source§

fn clone(&self) -> Job

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
Source§

impl Debug for Job

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Job

§

impl RefUnwindSafe for Job

§

impl Send for Job

§

impl Sync for Job

§

impl Unpin for Job

§

impl UnsafeUnpin for Job

§

impl UnwindSafe for Job

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
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
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

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

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

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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

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
Source§

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

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