obeli_sk_executor/
worker.rs

1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3use concepts::ExecutionId;
4use concepts::ExecutionMetadata;
5use concepts::FunctionMetadata;
6use concepts::PermanentFailureKind;
7use concepts::TrapKind;
8use concepts::prefixed_ulid::ExecutionIdDerived;
9use concepts::prefixed_ulid::RunId;
10use concepts::storage::HistoryEvent;
11use concepts::storage::Version;
12use concepts::storage::http_client_trace::HttpClientTrace;
13use concepts::{
14    FinishedExecutionError, StrVariant,
15    storage::{DbError, JoinSetResponseEvent},
16};
17use concepts::{FunctionFqn, ParamsParsingError, ResultParsingError};
18use concepts::{Params, SupportedFunctionReturnValue};
19use tracing::Span;
20
21#[async_trait]
22pub trait Worker: Send + Sync + 'static {
23    async fn run(&self, ctx: WorkerContext) -> WorkerResult;
24
25    fn exported_functions(&self) -> &[FunctionMetadata];
26
27    fn imported_functions(&self) -> &[FunctionMetadata];
28}
29
30#[must_use]
31#[derive(Debug)]
32pub enum WorkerResult {
33    Ok(
34        SupportedFunctionReturnValue,
35        Version,
36        Option<Vec<HttpClientTrace>>,
37    ),
38    DbUpdatedByWorkerOrWatcher,
39    Err(WorkerError),
40}
41
42#[derive(Debug)]
43pub struct WorkerContext {
44    pub execution_id: ExecutionId,
45    pub run_id: RunId,
46    pub metadata: ExecutionMetadata,
47    pub ffqn: FunctionFqn,
48    pub params: Params,
49    pub event_history: Vec<HistoryEvent>,
50    pub responses: Vec<JoinSetResponseEvent>,
51    pub version: Version,
52    pub execution_deadline: DateTime<Utc>,
53    pub can_be_retried: bool,
54    pub worker_span: Span,
55}
56
57#[derive(Debug, thiserror::Error)]
58pub enum WorkerError {
59    // retriable errors
60    // Used by activity worker
61    #[error("activity {trap_kind}: {reason}")]
62    ActivityTrap {
63        reason: String,
64        trap_kind: TrapKind,
65        detail: String,
66        version: Version,
67        http_client_traces: Option<Vec<HttpClientTrace>>,
68    },
69    // Used by activity worker, must not be returned when retries are exhausted.
70    #[error("activity returned error")]
71    ActivityReturnedError {
72        detail: Option<String>,
73        version: Version,
74        http_client_traces: Option<Vec<HttpClientTrace>>,
75    },
76    /// Workflow trap when `retry_on_trap` is enabled.
77    #[error("workflow trap handled as temporary error: {reason}")]
78    TemporaryWorkflowTrap {
79        reason: String,
80        kind: TrapKind,
81        detail: Option<String>,
82        version: Version,
83    },
84    // Resources are exhausted, retry after a delay as Unlocked, without increasing temporary event count.
85    #[error("limit reached: {reason}")]
86    LimitReached { reason: String, version: Version },
87    // Used by activity worker, best effort. If this is not persisted, the expired timers watcher will append it.
88    #[error("temporary timeout")]
89    TemporaryTimeout {
90        http_client_traces: Option<Vec<HttpClientTrace>>,
91        version: Version,
92    },
93    #[error(transparent)]
94    DbError(DbError),
95    // non-retriable errors
96    #[error("fatal error: {0}")]
97    FatalError(FatalError, Version),
98}
99
100#[derive(Debug, thiserror::Error)]
101pub enum FatalError {
102    /// Used by workflow worker when directly called child execution fails.
103    #[error("child finished with an execution error: {child_execution_id}")]
104    UnhandledChildExecutionError {
105        child_execution_id: ExecutionIdDerived,
106        root_cause_id: ExecutionIdDerived,
107    },
108
109    // Used by workflow worker
110    #[error("nondeterminism detected")]
111    NondeterminismDetected { detail: String },
112    // Used by activity worker, workflow worker
113    #[error(transparent)]
114    ParamsParsingError(ParamsParsingError),
115    // Used by activity worker, workflow worker
116    #[error("cannot instantiate: {reason}")]
117    CannotInstantiate { reason: String, detail: String },
118    // Used by activity worker, workflow worker
119    #[error(transparent)]
120    ResultParsingError(ResultParsingError),
121    /// Used when workflow cannot call an imported function, either a child execution or a function from workflow-support.
122    #[error("error calling imported function {ffqn} : {reason}")]
123    ImportedFunctionCallError {
124        ffqn: FunctionFqn,
125        reason: StrVariant,
126        detail: Option<String>,
127    },
128
129    /// Workflow trap if `retry_on_trap` is disabled.
130    #[error("workflow {trap_kind}: {reason}")]
131    WorkflowTrap {
132        reason: String,
133        trap_kind: TrapKind,
134        detail: String,
135    },
136    /// Workflow attempted to create a join set with the same name twice.
137    #[error("join set already exists with name `{name}`")]
138    JoinSetNameConflict { name: String },
139}
140
141impl From<FatalError> for FinishedExecutionError {
142    fn from(value: FatalError) -> Self {
143        let reason_full = value.to_string();
144        match value {
145            FatalError::UnhandledChildExecutionError {
146                child_execution_id,
147                root_cause_id,
148            } => FinishedExecutionError::UnhandledChildExecutionError {
149                child_execution_id,
150                root_cause_id,
151            },
152            FatalError::NondeterminismDetected { detail } => {
153                FinishedExecutionError::PermanentFailure {
154                    reason_inner: reason_full.clone(),
155                    reason_full,
156                    kind: PermanentFailureKind::NondeterminismDetected,
157                    detail: Some(detail),
158                }
159            }
160            FatalError::ParamsParsingError(params_parsing_error) => {
161                FinishedExecutionError::PermanentFailure {
162                    reason_inner: reason_full.to_string(),
163                    reason_full,
164                    kind: PermanentFailureKind::ParamsParsingError,
165                    detail: params_parsing_error.detail(),
166                }
167            }
168            FatalError::CannotInstantiate {
169                detail,
170                reason: reason_inner,
171                ..
172            } => FinishedExecutionError::PermanentFailure {
173                reason_inner,
174                reason_full,
175                kind: PermanentFailureKind::CannotInstantiate,
176                detail: Some(detail),
177            },
178            FatalError::ResultParsingError(_) => FinishedExecutionError::PermanentFailure {
179                reason_inner: reason_full.to_string(),
180                reason_full,
181                kind: PermanentFailureKind::ResultParsingError,
182                detail: None,
183            },
184            FatalError::ImportedFunctionCallError {
185                detail,
186                reason: reason_inner,
187                ..
188            } => FinishedExecutionError::PermanentFailure {
189                reason_inner: reason_inner.to_string(),
190                reason_full,
191                kind: PermanentFailureKind::ImportedFunctionCallError,
192                detail,
193            },
194            FatalError::WorkflowTrap {
195                detail,
196                reason: reason_inner,
197                ..
198            } => FinishedExecutionError::PermanentFailure {
199                reason_inner,
200                reason_full,
201                kind: PermanentFailureKind::WorkflowTrap,
202                detail: Some(detail),
203            },
204            FatalError::JoinSetNameConflict { name } => FinishedExecutionError::PermanentFailure {
205                reason_inner: name,
206                reason_full,
207                kind: PermanentFailureKind::JoinSetNameConflict,
208                detail: None,
209            },
210        }
211    }
212}