Skip to main content

apalis_workflow/sequential/
router.rs

1use std::{collections::HashMap, time::Duration};
2
3use apalis_core::{
4    backend::{Backend, WireFormatBackend},
5    task::task_id::TaskId,
6};
7use serde::{Deserialize, Serialize};
8
9use crate::SteppedService;
10
11/// Router for workflow steps
12#[derive(Debug, Default)]
13pub struct WorkflowRouter<B>
14where
15    B: Backend + WireFormatBackend,
16{
17    pub(crate) steps: HashMap<usize, SteppedService<B::Compact>>,
18}
19
20impl<B> WorkflowRouter<B>
21where
22    B: Backend + WireFormatBackend,
23{
24    /// Create a new workflow router
25    #[must_use]
26    pub fn new() -> Self {
27        Self {
28            steps: HashMap::new(),
29        }
30    }
31}
32/// Result information for workflow steps
33#[derive(Debug, Clone, Deserialize, Serialize)]
34pub struct StepResponse {
35    /// Result produced by the step
36    pub result: serde_json::Value,
37    /// Optional ID of the next task to execute
38    pub next_task_id: Option<TaskId>,
39}
40
41/// Enum representing the possible transitions in a workflow
42#[derive(Debug, Clone, Serialize, Deserialize)]
43#[non_exhaustive]
44pub enum GoTo<T = ()> {
45    /// Proceed to the next step with the given value
46    Next(T),
47    /// Delay the execution for the specified duration
48    DelayFor(Duration, T),
49    /// Break the workflow with the given value
50    Break(T),
51    /// Marks the workflow as done
52    Done,
53}