1use async_trait::async_trait;
2use serde::{Serialize, de::DeserializeOwned};
3
4use crate::backoff::BackoffStrategy;
5use crate::context::TaskContext;
6use crate::error::TaskResult;
7use crate::signature::Signature;
8
9#[async_trait]
11pub trait Task: Send + Sync + Serialize + DeserializeOwned + 'static {
12 const NAME: &'static str;
14
15 const QUEUE: &'static str = "default";
17
18 const MAX_RETRIES: u32 = 3;
20
21 fn backoff() -> BackoffStrategy {
23 BackoffStrategy::default()
24 }
25
26 type Output: Serialize + DeserializeOwned + Send;
28
29 async fn run(&self, ctx: &TaskContext) -> TaskResult<Self::Output>;
31
32 fn signature(&self) -> Signature {
34 Signature::new(
35 Self::NAME,
36 Self::QUEUE,
37 serde_json::to_value(self).expect("task must be serializable"),
38 )
39 .with_max_retries(Self::MAX_RETRIES)
40 }
41}