pub trait Job:
Send
+ Sync
+ 'static {
const NAME: &'static str;
// Required methods
fn payload(&self) -> Json;
fn from_payload(payload: &Json) -> Result<Self>
where Self: Sized;
fn handle(&self) -> impl Future<Output = Result<()>> + Send;
// Provided methods
fn tries(&self) -> u32 { ... }
fn retry_after(&self) -> Duration { ... }
fn queue(&self) -> &'static str { ... }
fn to_queued(&self) -> QueuedJob { ... }
}Expand description
A unit of work that can be stored and run later.
Deliberately not dyn-compatible: NAME and from_payload have no
receiver, and handle returns an opaque future. Nothing needs a dyn Job,
because the moment a job is queued it becomes a QueuedJob — plain data —
and the way back to a type is the JobRegistry.
Required Associated Constants§
Required Methods§
Sourcefn from_payload(payload: &Json) -> Result<Self>where
Self: Sized,
fn from_payload(payload: &Json) -> Result<Self>where
Self: Sized,
Rebuild the job from a payload the queue handed back.
Returning an error here fails the job rather than panicking the worker, which is what you want when an old payload meets new code.
Sourcefn handle(&self) -> impl Future<Output = Result<()>> + Send
fn handle(&self) -> impl Future<Output = Result<()>> + Send
Do the work.
Declared as -> impl Future + Send rather than async fn because the
registry boxes the future and sends it to another task, and an async fn in a trait promises nothing about Send. An implementation is
free to write async fn handle(&self) -> Result<()> instead: the
compiler can see the body, so it can prove the Send this demands.
Provided Methods§
Sourcefn retry_after(&self) -> Duration
fn retry_after(&self) -> Duration
How long to wait before the first retry. The worker doubles it per attempt.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".