pub struct Job {
pub id: i32,
pub payload: String,
pub status: String,
pub attempts: i32,
pub available_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}Expand description
Represents a unit of work in the system. Each Job can be in different states
(pending, processing, etc.) and may have multiple attempts if it fails.
Corresponds to the jobs table in the database.
§Fields
id: Primary key of the job record. Auto-generated by the database.payload: The data associated with this job (e.g., request parameters, message content).status: Current status of the job (e.g., “pending”, “processing”).attempts: Number of times an attempt has been made to run this job.available_at: A timestamp indicating when this job is eligible to be processed.created_at: Timestamp when the job was first created.updated_at: Timestamp of the most recent update to the job record.
Fields§
§id: i32§payload: String§status: String§attempts: i32§available_at: NaiveDateTime§created_at: NaiveDateTime§updated_at: NaiveDateTimeImplementations§
Source§impl Job
impl Job
Sourcepub fn new(payload: String) -> Self
pub fn new(payload: String) -> Self
Creates a new Job instance without inserting it into the database.
This sets default fields (e.g., status = "pending", attempts = 0,
created_at = now, and updated_at = now).
§Parameters
payload: The data associated with the job.
§Returns
A new Job struct ready to be inserted into the DB or used in-memory.
Sourcepub fn with_time(payload: String, _available_at: NaiveDateTime) -> Self
pub fn with_time(payload: String, _available_at: NaiveDateTime) -> Self
Sourcepub fn create(new_job: NewJob) -> QueryResult<Self>
pub fn create(new_job: NewJob) -> QueryResult<Self>
Inserts a new Job record into the jobs table.
This method will:
- Acquire a database connection from
DB::get_conn. - Run a transaction inserting the provided
new_jobinto thejobstable. - Fetch and return the newly inserted
Job(based on its descendingcreated_at).
§Parameters
new_job: TheNewJobstruct containing the data to be inserted.
§Returns
Ok(Job)if the insertion succeeded.Err(diesel::result::Error)if there was a connection or insertion problem.
§Errors
- Returns
diesel::result::Error::NotFoundif unable to get a DB connection. - Returns other Diesel errors if insertion or retrieval fails.
Sourcepub fn update(self) -> QueryResult<()>
pub fn update(self) -> QueryResult<()>
Updates an existing Job record in the jobs table.
The following fields may be updated:
status(copied fromself.status)attemptsavailable_atupdated_at(set tonow)
§Returns
Ok(())if the update succeeded.Err(diesel::result::Error)if there was a connection or update issue.
Sourcepub fn complete(&self, _return_value: String) -> QueryResult<()>
pub fn complete(&self, _return_value: String) -> QueryResult<()>
Marks a job as completed by inserting a corresponding ProcessedJob record
and then removing the Job from the jobs table.
§Parameters
_return_value: A string containing any relevant result data or message about the job’s completion.
§Returns
Ok(())if the operation (insertingProcessedJoband deleting the originalJob) succeeded.Err(diesel::result::Error)if any database operation failed.
Sourcepub fn fail(&mut self, _reason: &str) -> QueryResult<()>
pub fn fail(&mut self, _reason: &str) -> QueryResult<()>
Marks a job as failed. If the job has remaining retry attempts, it increments
the job’s attempts and defers its available_at time by the configured retry interval.
Otherwise, it inserts a FailedJob record and removes the job from jobs.
§Parameters
_reason: A description of why the job failed.
§Returns
Ok(())if the job was updated or moved to failed jobs.Err(diesel::result::Error)if a database operation fails.
§Behavior
- If
max_retry_attempts(fromConfig::get_config) is greater than the current number of attempts, the job is set to retry later. - Otherwise, a record is inserted into
failed_jobs, and the job is deleted from thejobstable.
Sourcepub fn fetch_pending_jobs_count() -> QueryResult<i64>
pub fn fetch_pending_jobs_count() -> QueryResult<i64>
Returns the count of jobs that are currently in pending status
and have an available_at time less than or equal to now.
Useful for checking how many pending jobs are ready to be processed.
§Returns
Ok(i64)with the count of matching jobs.Err(diesel::result::Error)if there was an issue retrieving data.
Sourcepub fn fetch_pending_job() -> QueryResult<Option<Self>>
pub fn fetch_pending_job() -> QueryResult<Option<Self>>
Fetches the next pending job (the earliest by ID) that is ready to be processed,
and marks its status as processing.
§Returns
Ok(Some(Job))if a pending job was found and updated.Ok(None)if no pending job is available.Err(diesel::result::Error)if a database operation fails.
§Transaction
This operation is performed within a transaction to ensure atomicity: retrieving the job and updating its status happen together.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Job
impl<'de> Deserialize<'de> for Job
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<'insert> Insertable<table> for &'insert Job
impl<'insert> Insertable<table> for &'insert Job
Source§type Values = <(Option<Grouped<Eq<id, <&'insert i32 as AsExpression<<id as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<payload, <&'insert String as AsExpression<<payload as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<status, <&'insert String as AsExpression<<status as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<attempts, <&'insert i32 as AsExpression<<attempts as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<available_at, <&'insert NaiveDateTime as AsExpression<<available_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<created_at, <&'insert NaiveDateTime as AsExpression<<created_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<updated_at, <&'insert NaiveDateTime as AsExpression<<updated_at as Expression>::SqlType>>::Expression>>>) as Insertable<table>>::Values
type Values = <(Option<Grouped<Eq<id, <&'insert i32 as AsExpression<<id as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<payload, <&'insert String as AsExpression<<payload as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<status, <&'insert String as AsExpression<<status as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<attempts, <&'insert i32 as AsExpression<<attempts as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<available_at, <&'insert NaiveDateTime as AsExpression<<available_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<created_at, <&'insert NaiveDateTime as AsExpression<<created_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<updated_at, <&'insert NaiveDateTime as AsExpression<<updated_at as Expression>::SqlType>>::Expression>>>) as Insertable<table>>::Values
VALUES clause to insert these records Read moreSource§fn values(
self,
) -> <(Option<Eq<id, &'insert i32>>, Option<Eq<payload, &'insert String>>, Option<Eq<status, &'insert String>>, Option<Eq<attempts, &'insert i32>>, Option<Eq<available_at, &'insert NaiveDateTime>>, Option<Eq<created_at, &'insert NaiveDateTime>>, Option<Eq<updated_at, &'insert NaiveDateTime>>) as Insertable<table>>::Values
fn values( self, ) -> <(Option<Eq<id, &'insert i32>>, Option<Eq<payload, &'insert String>>, Option<Eq<status, &'insert String>>, Option<Eq<attempts, &'insert i32>>, Option<Eq<available_at, &'insert NaiveDateTime>>, Option<Eq<created_at, &'insert NaiveDateTime>>, Option<Eq<updated_at, &'insert NaiveDateTime>>) as Insertable<table>>::Values
Self::Values Read moreSource§fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>
fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>
self into a given table. Read moreSource§impl Insertable<table> for Job
impl Insertable<table> for Job
Source§type Values = <(Option<Grouped<Eq<id, <i32 as AsExpression<<id as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<payload, <String as AsExpression<<payload as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<status, <String as AsExpression<<status as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<attempts, <i32 as AsExpression<<attempts as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<available_at, <NaiveDateTime as AsExpression<<available_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<created_at, <NaiveDateTime as AsExpression<<created_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<updated_at, <NaiveDateTime as AsExpression<<updated_at as Expression>::SqlType>>::Expression>>>) as Insertable<table>>::Values
type Values = <(Option<Grouped<Eq<id, <i32 as AsExpression<<id as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<payload, <String as AsExpression<<payload as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<status, <String as AsExpression<<status as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<attempts, <i32 as AsExpression<<attempts as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<available_at, <NaiveDateTime as AsExpression<<available_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<created_at, <NaiveDateTime as AsExpression<<created_at as Expression>::SqlType>>::Expression>>>, Option<Grouped<Eq<updated_at, <NaiveDateTime as AsExpression<<updated_at as Expression>::SqlType>>::Expression>>>) as Insertable<table>>::Values
VALUES clause to insert these records Read moreSource§fn values(
self,
) -> <(Option<Eq<id, i32>>, Option<Eq<payload, String>>, Option<Eq<status, String>>, Option<Eq<attempts, i32>>, Option<Eq<available_at, NaiveDateTime>>, Option<Eq<created_at, NaiveDateTime>>, Option<Eq<updated_at, NaiveDateTime>>) as Insertable<table>>::Values
fn values( self, ) -> <(Option<Eq<id, i32>>, Option<Eq<payload, String>>, Option<Eq<status, String>>, Option<Eq<attempts, i32>>, Option<Eq<available_at, NaiveDateTime>>, Option<Eq<created_at, NaiveDateTime>>, Option<Eq<updated_at, NaiveDateTime>>) as Insertable<table>>::Values
Self::Values Read moreSource§fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>
fn insert_into(self, table: T) -> InsertStatement<T, Self::Values>
self into a given table. Read moreSource§impl<__DB: Backend, __ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6> Queryable<(__ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6), __DB> for Jobwhere
(i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime): FromStaticSqlRow<(__ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6), __DB>,
impl<__DB: Backend, __ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6> Queryable<(__ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6), __DB> for Jobwhere
(i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime): FromStaticSqlRow<(__ST0, __ST1, __ST2, __ST3, __ST4, __ST5, __ST6), __DB>,
Source§type Row = (i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime)
type Row = (i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime)
Source§fn build(
row: (i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime),
) -> Result<Self>
fn build( row: (i32, String, String, i32, NaiveDateTime, NaiveDateTime, NaiveDateTime), ) -> Result<Self>
impl UndecoratedInsertRecord<table> for Job
Auto Trait Implementations§
impl Freeze for Job
impl RefUnwindSafe for Job
impl Send for Job
impl Sync for Job
impl Unpin for Job
impl UnwindSafe for Job
Blanket Implementations§
Source§impl<T> AggregateExpressionMethods for T
impl<T> AggregateExpressionMethods for T
Source§fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
fn aggregate_distinct(self) -> Self::Outputwhere
Self: DistinctDsl,
DISTINCT modifier for aggregate functions Read moreSource§fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
fn aggregate_all(self) -> Self::Outputwhere
Self: AllDsl,
ALL modifier for aggregate functions Read moreSource§fn aggregate_filter<P>(self, f: P) -> Self::Output
fn aggregate_filter<P>(self, f: P) -> Self::Output
Source§fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
fn aggregate_order<O>(self, o: O) -> Self::Outputwhere
Self: OrderAggregateDsl<O>,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> IntoSql for T
impl<T> IntoSql for T
Source§fn into_sql<T>(self) -> Self::Expression
fn into_sql<T>(self) -> Self::Expression
self to an expression for Diesel’s query builder. Read moreSource§fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
fn as_sql<'a, T>(&'a self) -> <&'a Self as AsExpression<T>>::Expression
&self to an expression for Diesel’s query builder. Read more