Skip to main content

kojin_core/
task.rs

1use async_trait::async_trait;
2use serde::{Serialize, de::DeserializeOwned};
3
4use crate::backoff::BackoffStrategy;
5use crate::context::TaskContext;
6use crate::error::TaskResult;
7
8/// A unit of work that can be enqueued and executed by a worker.
9#[async_trait]
10pub trait Task: Send + Sync + Serialize + DeserializeOwned + 'static {
11    /// Unique name used for routing. Must be stable across deploys.
12    const NAME: &'static str;
13
14    /// Default queue name.
15    const QUEUE: &'static str = "default";
16
17    /// Maximum number of retry attempts.
18    const MAX_RETRIES: u32 = 3;
19
20    /// Backoff strategy for retries.
21    fn backoff() -> BackoffStrategy {
22        BackoffStrategy::default()
23    }
24
25    /// The output type of the task.
26    type Output: Serialize + DeserializeOwned + Send;
27
28    /// Execute the task.
29    async fn run(&self, ctx: &TaskContext) -> TaskResult<Self::Output>;
30}