Task

Trait Task 

Source
pub trait Task:
    Sized
    + Debug
    + DeserializeOwned {
    type State: Clone + Debug;

    // Required method
    fn run(self, state: Arc<Self::State>) -> BoxFuture<'static, Result<(), ()>>;

    // Provided methods
    fn decode(data: Vec<u8>) -> Result<Self, RabbitDecodeError> { ... }
    fn display(&self) -> String { ... }
    fn before_job(self) -> impl Future<Output = Self> + Send
       where Self: Send { ... }
    fn after_job(self) -> impl Future<Output = ()> + Send
       where Self: Sync + Send { ... }
}
Expand description

A regular task
Implement this trait to any struct to make it a runnable non-rpc Task job.

Examples


#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct EmailJob {
    send_to: String,
    contents: String,
}
impl Task for EmailJob {
    type State = State;
    fn run(
        self,
        state: Self::State,
    ) -> futures::prelude::future::BoxFuture<'static, Result<(), ()>>
    {
        Box::pin(async move {
            todo!();
        })
    }
}

Required Associated Types§

Required Methods§

Source

fn run(self, state: Arc<Self::State>) -> BoxFuture<'static, Result<(), ()>>

The method that will be run by the worker

Provided Methods§

Source

fn decode(data: Vec<u8>) -> Result<Self, RabbitDecodeError>

Source

fn display(&self) -> String

A function to display the task

Source

fn before_job(self) -> impl Future<Output = Self> + Send
where Self: Send,

A function that runs before the job is ran by the worker. This allows you to modify any values inside it, add tracing ect.

Source

fn after_job(self) -> impl Future<Output = ()> + Send
where Self: Sync + Send,

A function that runs after the job has finished and the worker has acked the request.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§