Skip to main content

a3s_flow/worker/
task.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::model::JsonValue;
5
6/// Queueable unit of workflow engine work.
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8#[serde(tag = "type", rename_all = "snake_case")]
9pub enum FlowTask {
10    DriveRun {
11        run_id: String,
12    },
13    ResumeWait {
14        run_id: String,
15        wait_id: String,
16    },
17    ResumeHook {
18        run_id: String,
19        hook_id: String,
20        payload: JsonValue,
21    },
22    ResumeHookByToken {
23        token: String,
24        payload: JsonValue,
25    },
26    DisposeHook {
27        run_id: String,
28        hook_id: String,
29    },
30    DisposeHookByToken {
31        token: String,
32    },
33    ResumeScheduledRun {
34        run_id: String,
35        now: DateTime<Utc>,
36    },
37    ResumeDueWaits {
38        now: DateTime<Utc>,
39    },
40    ResumeDueRetries {
41        now: DateTime<Utc>,
42    },
43}
44
45/// Result of handling one queued [`FlowTask`].
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
47pub struct FlowTaskOutcome {
48    pub task: FlowTask,
49    pub run_ids: Vec<String>,
50    pub resumed_waits: Vec<(String, String)>,
51    pub resumed_retries: Vec<(String, String)>,
52    pub resumed_hook: Option<(String, String)>,
53    #[serde(default)]
54    pub disposed_hook: Option<(String, String)>,
55}
56
57impl FlowTaskOutcome {
58    pub(super) fn new(task: FlowTask) -> Self {
59        Self {
60            task,
61            run_ids: Vec::new(),
62            resumed_waits: Vec::new(),
63            resumed_retries: Vec::new(),
64            resumed_hook: None,
65            disposed_hook: None,
66        }
67    }
68}
69
70/// Leased task returned by a queue worker before acknowledgement.
71///
72/// [`super::FlowTaskQueue::heartbeat`] replaces `lease_id` with a new fencing
73/// token. Callers that renew leases manually must acknowledge with the latest
74/// returned token.
75#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
76pub struct FlowTaskLease {
77    pub lease_id: String,
78    pub task: FlowTask,
79}
80
81/// Task moved out of inflight dispatch after exceeding a local lease policy.
82#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
83pub struct LocalFileDeadLetteredTask {
84    pub lease_id: String,
85    pub task: FlowTask,
86    pub reason: String,
87    pub dead_lettered_at: DateTime<Utc>,
88}
89
90/// Task moved out of Postgres inflight dispatch after exceeding a lease policy.
91#[cfg(feature = "postgres")]
92#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
93pub struct PostgresDeadLetteredTask {
94    pub lease_id: String,
95    pub task: FlowTask,
96    pub reason: String,
97    pub dead_lettered_at: DateTime<Utc>,
98}