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§
Provided Methods§
fn decode(data: Vec<u8>) -> Result<Self, RabbitDecodeError>
Sourcefn before_job(self) -> impl Future<Output = Self> + Sendwhere
Self: Send,
fn before_job(self) -> impl Future<Output = Self> + Sendwhere
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.
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.