Skip to main content

kojin_core/
result_backend.rs

1use async_trait::async_trait;
2
3use crate::error::{KojinError, TaskResult};
4use crate::task_id::TaskId;
5
6/// Backend for storing and retrieving task results.
7#[async_trait]
8pub trait ResultBackend: Send + Sync + 'static {
9    /// Store a task result.
10    async fn store(&self, id: &TaskId, result: &serde_json::Value) -> TaskResult<()>;
11
12    /// Get a stored result.
13    async fn get(&self, id: &TaskId) -> TaskResult<Option<serde_json::Value>>;
14
15    /// Wait for a result to be available, with timeout.
16    async fn wait(
17        &self,
18        id: &TaskId,
19        timeout: std::time::Duration,
20    ) -> TaskResult<serde_json::Value>;
21
22    /// Delete a stored result.
23    async fn delete(&self, id: &TaskId) -> TaskResult<()>;
24
25    /// Initialize a group with the expected total count.
26    async fn init_group(&self, _group_id: &str, _total: u32) -> TaskResult<()> {
27        Err(KojinError::ResultBackend(
28            "group operations not supported by this backend".into(),
29        ))
30    }
31
32    /// Mark a group member as complete and return the number of completed members.
33    async fn complete_group_member(
34        &self,
35        _group_id: &str,
36        _task_id: &TaskId,
37        _result: &serde_json::Value,
38    ) -> TaskResult<u32> {
39        Err(KojinError::ResultBackend(
40            "group operations not supported by this backend".into(),
41        ))
42    }
43
44    /// Get all results for a group.
45    async fn get_group_results(&self, _group_id: &str) -> TaskResult<Vec<serde_json::Value>> {
46        Err(KojinError::ResultBackend(
47            "group operations not supported by this backend".into(),
48        ))
49    }
50}