Skip to main content

apalis_core/backend/
results.rs

1use futures_core::Stream;
2
3use crate::{
4    backend::Backend,
5    task::{status::Status, task_id::TaskId},
6};
7
8/// Represents the result of a task execution
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[derive(Debug, Clone)]
11pub struct TaskResult<T> {
12    /// The unique identifier of the task
13    pub task_id: TaskId,
14    /// The most recent result
15    pub attempt: usize,
16    /// The status of the task
17    pub status: Status,
18    /// The result of the task execution
19    pub result: Result<T, String>,
20}
21
22impl<T> TaskResult<T> {
23    /// Get the ID of the task
24    pub fn task_id(&self) -> &TaskId {
25        &self.task_id
26    }
27
28    /// Get the status of the task
29    pub fn status(&self) -> &Status {
30        &self.status
31    }
32
33    /// Get the result of the task
34    pub fn result(&self) -> &Result<T, String> {
35        &self.result
36    }
37
38    /// Take the result of the task
39    pub fn take(self) -> Result<T, String> {
40        self.result
41    }
42}
43
44/// Allows waiting for tasks to complete and checking their status
45pub trait WaitForCompletion<Output>: Backend {
46    /// The result stream type yielding task results
47    type ResultStream: Stream<Item = Result<TaskResult<Output>, Self::Error>> + Send + 'static;
48
49    /// Wait for multiple tasks to complete, yielding results as they become available
50    fn wait_for(&self, task_ids: impl IntoIterator<Item = TaskId>) -> Self::ResultStream;
51
52    /// Wait for a single task to complete, yielding its result
53    fn wait_for_single(&self, task_id: TaskId) -> Self::ResultStream {
54        self.wait_for(std::iter::once(task_id))
55    }
56
57    /// Check current status of tasks without waiting
58    fn check_status(
59        &self,
60        task_ids: impl IntoIterator<Item = TaskId> + Send,
61    ) -> impl Future<Output = Result<Vec<TaskResult<Output>>, Self::Error>> + Send;
62}