1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//! Workflow lifecycle methods: start, get, list, cancel, terminate, complete, fail,
//! finalise_cancellation, upsert_search_attributes, notify_parent_of_child_outcome.
use std::str::FromStr;
use anyhow::Result;
use crate::ctx::{inject_engine_version, timestamp_now, WorkflowCtx};
use crate::events::WorkflowBusEvent;
use crate::store::WorkflowStore;
use crate::types::*;
impl<S: WorkflowStore> WorkflowCtx<S> {
pub async fn start_workflow(
&self,
namespace: &str,
workflow_type: &str,
workflow_id: &str,
input: Option<&str>,
task_queue: &str,
search_attributes: Option<&str>,
) -> Result<WorkflowRecord> {
let now = timestamp_now();
let run_id = format!("run-{workflow_id}-{}", now as u64);
// Auto-stamp the engine version that started this run into its
// search attributes. Makes post-mortem triage concrete: "this
// run was v0.11.9, that's why it was stuck in main instead of
// deployments" is the kind of question that's otherwise guesswork
// once multiple engine versions have coexisted in a deployment.
// The operator's own attributes take precedence if they also
// supplied `assay_engine_version` — we don't overwrite, just
// backfill on the "not set" case.
let stamped_attrs = inject_engine_version(search_attributes);
let wf = WorkflowRecord {
id: workflow_id.to_string(),
namespace: namespace.to_string(),
run_id,
workflow_type: workflow_type.to_string(),
task_queue: task_queue.to_string(),
status: "PENDING".to_string(),
input: input.map(String::from),
result: None,
error: None,
parent_id: None,
claimed_by: None,
search_attributes: stamped_attrs,
archived_at: None,
archive_uri: None,
created_at: now,
updated_at: now,
completed_at: None,
};
self.store.create_workflow(&wf).await?;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: workflow_id.to_string(),
seq: 1,
event_type: "WorkflowStarted".to_string(),
payload: input.map(String::from),
timestamp: now,
})
.await?;
// Phase 9: a freshly-started workflow has new events (WorkflowStarted)
// that need a worker to replay against — make it dispatchable and
// emit WorkflowNeedsDispatch for the dispatch-wakeup loop.
self.mark_and_emit_needs_dispatch(workflow_id).await?;
// Notify SSE subscribers via the engine event bus. An emit
// failure is a notification-layer issue only; the row write
// above is already committed.
self.emit(
namespace,
WorkflowBusEvent::WorkflowStarted {
workflow_id: workflow_id.to_string(),
},
)
.await;
Ok(wf)
}
pub async fn get_workflow(&self, id: &str) -> Result<Option<WorkflowRecord>> {
self.store.get_workflow(id).await
}
pub async fn list_workflows(
&self,
namespace: &str,
status: Option<WorkflowStatus>,
workflow_type: Option<&str>,
search_attrs_filter: Option<&str>,
limit: i64,
offset: i64,
) -> Result<Vec<WorkflowRecord>> {
self.store
.list_workflows(
namespace,
status,
workflow_type,
search_attrs_filter,
limit,
offset,
)
.await
}
pub async fn upsert_search_attributes(
&self,
workflow_id: &str,
patch_json: &str,
) -> Result<()> {
self.store
.upsert_search_attributes(workflow_id, patch_json)
.await
}
pub async fn cancel_workflow(&self, id: &str, reason: Option<&str>) -> Result<bool> {
let wf = self.store.get_workflow(id).await?;
match wf {
None => Ok(false),
Some(wf) => {
let status = WorkflowStatus::from_str(&wf.status)
.map_err(|e| anyhow::anyhow!(e))?;
if status.is_terminal() {
return Ok(false);
}
// Two-phase cancel:
// 1. Append WorkflowCancelRequested + mark dispatchable.
// The next worker replay sees the request, raises a
// cancellation error inside the handler, and submits
// a CancelWorkflow command.
// 2. CancelWorkflow command flips status to CANCELLED,
// cancels pending activities/timers, appends
// WorkflowCancelled.
//
// We cancel pending activities + timers up-front too so a
// worker that's about to claim them sees CANCELLED instead.
self.store.cancel_pending_activities(id).await?;
self.store.cancel_pending_timers(id).await?;
// Operator-supplied reason rides along on the event
// payload so audit queries (and the dashboard's events
// tab) can show why a cancel happened. Symmetric with
// terminate, which has always taken a reason.
let payload = reason.map(|r| {
serde_json::json!({ "reason": r }).to_string()
});
let seq = self.store.get_event_count(id).await? as i32 + 1;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: id.to_string(),
seq,
event_type: "WorkflowCancelRequested".to_string(),
payload,
timestamp: timestamp_now(),
})
.await?;
self.mark_and_emit_needs_dispatch(id).await?;
// Propagate cancellation to all child workflows. Children
// inherit the reason so the audit trail explains the
// whole cascade in one place.
let children = self.store.list_child_workflows(id).await?;
for child in children {
Box::pin(self.cancel_workflow(&child.id, reason)).await?;
}
// For workflows that have NO worker registered (or no
// handler running), cancellation would never complete.
// Fall back: if the workflow has no events past
// WorkflowStarted (handler hasn't actually run yet, e.g.
// PENDING with no claim), finalise immediately.
if matches!(status, WorkflowStatus::Pending) {
self.finalise_cancellation(id).await?;
}
Ok(true)
}
}
}
pub async fn terminate_workflow(&self, id: &str, reason: Option<&str>) -> Result<bool> {
let wf = self.store.get_workflow(id).await?;
match wf {
None => Ok(false),
Some(wf) => {
let status = WorkflowStatus::from_str(&wf.status)
.map_err(|e| anyhow::anyhow!(e))?;
if status.is_terminal() {
return Ok(false);
}
self.store
.update_workflow_status(
id,
WorkflowStatus::Failed,
None,
Some(reason.unwrap_or("terminated")),
)
.await?;
// Live-refresh the dashboard — no more F5 after
// Terminate.
self.emit(
&wf.namespace,
WorkflowBusEvent::WorkflowTerminated {
workflow_id: id.to_string(),
},
)
.await;
Ok(true)
}
}
}
/// Finalise a cancellation: flips status to CANCELLED and appends the
/// terminal WorkflowCancelled event. Called by the CancelWorkflow
/// command handler (worker acknowledged cancel) and by cancel_workflow
/// directly when the workflow has no worker yet.
pub async fn finalise_cancellation(&self, workflow_id: &str) -> Result<()> {
// Avoid double-finalising
if let Some(wf) = self.store.get_workflow(workflow_id).await?
&& wf.status == "CANCELLED"
{
return Ok(());
}
self.store
.update_workflow_status(workflow_id, WorkflowStatus::Cancelled, None, None)
.await?;
let event_seq = self.store.get_event_count(workflow_id).await? as i32 + 1;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: workflow_id.to_string(),
seq: event_seq,
event_type: "WorkflowCancelled".to_string(),
payload: None,
timestamp: timestamp_now(),
})
.await?;
let ns = self
.store
.get_workflow(workflow_id)
.await?
.map(|w| w.namespace)
.unwrap_or_default();
self.emit(
&ns,
WorkflowBusEvent::WorkflowCancelled {
workflow_id: workflow_id.to_string(),
},
)
.await;
Ok(())
}
/// Mark a workflow COMPLETED with a result + append WorkflowCompleted event.
/// If the workflow has a parent, also notifies the parent with a
/// ChildWorkflowCompleted event and marks it dispatchable so it can
/// replay past `ctx:start_child_workflow` and pick up the child's result.
pub async fn complete_workflow(&self, workflow_id: &str, result: Option<&str>) -> Result<()> {
self.store
.update_workflow_status(workflow_id, WorkflowStatus::Completed, result, None)
.await?;
let event_seq = self.store.get_event_count(workflow_id).await? as i32 + 1;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: workflow_id.to_string(),
seq: event_seq,
event_type: "WorkflowCompleted".to_string(),
payload: result.map(String::from),
timestamp: timestamp_now(),
})
.await?;
self.notify_parent_of_child_outcome(
workflow_id,
"ChildWorkflowCompleted",
serde_json::json!({
"child_workflow_id": workflow_id,
"result": result.and_then(|s| serde_json::from_str::<serde_json::Value>(s).ok()),
}),
)
.await?;
let ns = self
.store
.get_workflow(workflow_id)
.await?
.map(|w| w.namespace)
.unwrap_or_default();
self.emit(
&ns,
WorkflowBusEvent::WorkflowCompleted {
workflow_id: workflow_id.to_string(),
},
)
.await;
Ok(())
}
/// Mark a workflow FAILED with an error + append WorkflowFailed event.
/// Notifies the parent if any (ChildWorkflowFailed).
pub async fn fail_workflow(&self, workflow_id: &str, error: &str) -> Result<()> {
self.store
.update_workflow_status(workflow_id, WorkflowStatus::Failed, None, Some(error))
.await?;
let event_seq = self.store.get_event_count(workflow_id).await? as i32 + 1;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: workflow_id.to_string(),
seq: event_seq,
event_type: "WorkflowFailed".to_string(),
payload: Some(serde_json::json!({"error": error}).to_string()),
timestamp: timestamp_now(),
})
.await?;
self.notify_parent_of_child_outcome(
workflow_id,
"ChildWorkflowFailed",
serde_json::json!({
"child_workflow_id": workflow_id,
"error": error,
}),
)
.await?;
let ns = self
.store
.get_workflow(workflow_id)
.await?
.map(|w| w.namespace)
.unwrap_or_default();
self.emit(
&ns,
WorkflowBusEvent::WorkflowFailed {
workflow_id: workflow_id.to_string(),
},
)
.await;
Ok(())
}
/// Append a parent-side event when a child reaches a terminal state and
/// re-dispatch the parent so it can replay past its `start_child_workflow`
/// call. No-op for top-level workflows (no parent_id).
async fn notify_parent_of_child_outcome(
&self,
child_workflow_id: &str,
event_type: &str,
payload: serde_json::Value,
) -> Result<()> {
let Some(child) = self.store.get_workflow(child_workflow_id).await? else {
return Ok(());
};
let Some(parent_id) = child.parent_id else {
return Ok(());
};
let event_seq = self.store.get_event_count(&parent_id).await? as i32 + 1;
self.store
.append_event(&WorkflowEvent {
id: None,
workflow_id: parent_id.clone(),
seq: event_seq,
event_type: event_type.to_string(),
payload: Some(payload.to_string()),
timestamp: timestamp_now(),
})
.await?;
self.mark_and_emit_needs_dispatch(&parent_id).await?;
Ok(())
}
}