1use std::future::Future;
4
5use crate::types::*;
6
7pub trait WorkflowStore: Send + Sync + 'static {
15 fn create_namespace(&self, name: &str) -> impl Future<Output = anyhow::Result<()>> + Send;
18
19 fn list_namespaces(&self) -> impl Future<Output = anyhow::Result<Vec<NamespaceRecord>>> + Send;
20
21 fn delete_namespace(&self, name: &str) -> impl Future<Output = anyhow::Result<bool>> + Send;
22
23 fn get_namespace_stats(
24 &self,
25 namespace: &str,
26 ) -> impl Future<Output = anyhow::Result<NamespaceStats>> + Send;
27
28 fn create_workflow(
31 &self,
32 workflow: &WorkflowRecord,
33 ) -> impl Future<Output = anyhow::Result<()>> + Send;
34
35 fn get_workflow(
36 &self,
37 id: &str,
38 ) -> impl Future<Output = anyhow::Result<Option<WorkflowRecord>>> + Send;
39
40 fn list_workflows(
41 &self,
42 namespace: &str,
43 status: Option<WorkflowStatus>,
44 workflow_type: Option<&str>,
45 search_attrs_filter: Option<&str>,
46 limit: i64,
47 offset: i64,
48 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowRecord>>> + Send;
49
50 fn list_archivable_workflows(
54 &self,
55 cutoff: f64,
56 limit: i64,
57 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowRecord>>> + Send;
58
59 fn mark_archived_and_purge(
64 &self,
65 workflow_id: &str,
66 archive_uri: &str,
67 archived_at: f64,
68 ) -> impl Future<Output = anyhow::Result<()>> + Send;
69
70 fn upsert_search_attributes(
75 &self,
76 workflow_id: &str,
77 patch_json: &str,
78 ) -> impl Future<Output = anyhow::Result<()>> + Send;
79
80 fn update_workflow_status(
81 &self,
82 id: &str,
83 status: WorkflowStatus,
84 result: Option<&str>,
85 error: Option<&str>,
86 ) -> impl Future<Output = anyhow::Result<()>> + Send;
87
88 fn claim_workflow(
89 &self,
90 id: &str,
91 worker_id: &str,
92 ) -> impl Future<Output = anyhow::Result<bool>> + Send;
93
94 fn mark_workflow_dispatchable(
99 &self,
100 workflow_id: &str,
101 ) -> impl Future<Output = anyhow::Result<()>> + Send;
102
103 fn claim_workflow_task(
108 &self,
109 task_queue: &str,
110 worker_id: &str,
111 ) -> impl Future<Output = anyhow::Result<Option<WorkflowRecord>>> + Send;
112
113 fn release_workflow_task(
117 &self,
118 workflow_id: &str,
119 worker_id: &str,
120 ) -> impl Future<Output = anyhow::Result<()>> + Send;
121
122 fn release_stale_dispatch_leases(
127 &self,
128 now: f64,
129 timeout_secs: f64,
130 ) -> impl Future<Output = anyhow::Result<u64>> + Send;
131
132 fn append_event(
135 &self,
136 event: &WorkflowEvent,
137 ) -> impl Future<Output = anyhow::Result<i64>> + Send;
138
139 fn list_events(
140 &self,
141 workflow_id: &str,
142 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowEvent>>> + Send;
143
144 fn list_events_page(
148 &self,
149 workflow_id: &str,
150 cursor: Option<i32>,
151 limit: i64,
152 descending: bool,
153 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowEvent>>> + Send {
154 async move {
155 let limit = limit.clamp(0, 1_000) as usize;
156 if limit == 0 {
157 return Ok(Vec::new());
158 }
159 let mut events = self.list_events(workflow_id).await?;
160 events.retain(|event| {
161 cursor.is_none_or(|sequence| {
162 if descending {
163 event.seq < sequence
164 } else {
165 event.seq > sequence
166 }
167 })
168 });
169 if descending {
170 events.reverse();
171 }
172 events.truncate(limit);
173 Ok(events)
174 }
175 }
176
177 fn get_event_count(
178 &self,
179 workflow_id: &str,
180 ) -> impl Future<Output = anyhow::Result<i64>> + Send;
181
182 fn create_activity(
185 &self,
186 activity: &WorkflowActivity,
187 ) -> impl Future<Output = anyhow::Result<i64>> + Send;
188
189 fn get_activity(
191 &self,
192 id: i64,
193 ) -> impl Future<Output = anyhow::Result<Option<WorkflowActivity>>> + Send;
194
195 fn get_activity_by_workflow_seq(
199 &self,
200 workflow_id: &str,
201 seq: i32,
202 ) -> impl Future<Output = anyhow::Result<Option<WorkflowActivity>>> + Send;
203
204 fn claim_activity(
205 &self,
206 task_queue: &str,
207 worker_id: &str,
208 ) -> impl Future<Output = anyhow::Result<Option<WorkflowActivity>>> + Send;
209
210 fn requeue_activity_for_retry(
215 &self,
216 id: i64,
217 next_attempt: i32,
218 next_scheduled_at: f64,
219 ) -> impl Future<Output = anyhow::Result<()>> + Send;
220
221 fn retry_failed_activity(
222 &self,
223 _workflow_id: &str,
224 _requested_by: &str,
225 _reason: &str,
226 _requested_at: f64,
227 ) -> impl Future<Output = anyhow::Result<RetryFailedActivityResult>> + Send {
228 async { Ok(RetryFailedActivityResult::Unsupported) }
229 }
230
231 fn complete_activity(
232 &self,
233 id: i64,
234 result: Option<&str>,
235 error: Option<&str>,
236 failed: bool,
237 ) -> impl Future<Output = anyhow::Result<()>> + Send;
238
239 fn heartbeat_activity(
240 &self,
241 id: i64,
242 details: Option<&str>,
243 ) -> impl Future<Output = anyhow::Result<()>> + Send;
244
245 fn get_timed_out_activities(
246 &self,
247 now: f64,
248 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowActivity>>> + Send;
249
250 fn cancel_pending_activities(
257 &self,
258 workflow_id: &str,
259 ) -> impl Future<Output = anyhow::Result<u64>> + Send;
260
261 fn cancel_pending_timers(
265 &self,
266 workflow_id: &str,
267 ) -> impl Future<Output = anyhow::Result<u64>> + Send;
268
269 fn create_timer(
270 &self,
271 timer: &WorkflowTimer,
272 ) -> impl Future<Output = anyhow::Result<i64>> + Send;
273
274 fn get_timer_by_workflow_seq(
278 &self,
279 workflow_id: &str,
280 seq: i32,
281 ) -> impl Future<Output = anyhow::Result<Option<WorkflowTimer>>> + Send;
282
283 fn fire_due_timers(
284 &self,
285 now: f64,
286 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowTimer>>> + Send;
287
288 fn send_signal(
291 &self,
292 signal: &WorkflowSignal,
293 ) -> impl Future<Output = anyhow::Result<i64>> + Send;
294
295 fn consume_signals(
296 &self,
297 workflow_id: &str,
298 name: &str,
299 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowSignal>>> + Send;
300
301 fn create_schedule(
304 &self,
305 schedule: &WorkflowSchedule,
306 ) -> impl Future<Output = anyhow::Result<()>> + Send;
307
308 fn get_schedule(
309 &self,
310 namespace: &str,
311 name: &str,
312 ) -> impl Future<Output = anyhow::Result<Option<WorkflowSchedule>>> + Send;
313
314 fn list_schedules(
315 &self,
316 namespace: &str,
317 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowSchedule>>> + Send;
318
319 fn update_schedule_last_run(
320 &self,
321 namespace: &str,
322 name: &str,
323 last_run_at: f64,
324 next_run_at: f64,
325 workflow_id: &str,
326 ) -> impl Future<Output = anyhow::Result<()>> + Send;
327
328 fn delete_schedule(
329 &self,
330 namespace: &str,
331 name: &str,
332 ) -> impl Future<Output = anyhow::Result<bool>> + Send;
333
334 fn update_schedule(
342 &self,
343 namespace: &str,
344 name: &str,
345 patch: &SchedulePatch,
346 ) -> impl Future<Output = anyhow::Result<Option<WorkflowSchedule>>> + Send;
347
348 fn set_schedule_paused(
355 &self,
356 namespace: &str,
357 name: &str,
358 paused: bool,
359 ) -> impl Future<Output = anyhow::Result<Option<WorkflowSchedule>>> + Send;
360
361 fn register_worker(
364 &self,
365 worker: &WorkflowWorker,
366 ) -> impl Future<Output = anyhow::Result<()>> + Send;
367
368 fn heartbeat_worker(
369 &self,
370 id: &str,
371 now: f64,
372 ) -> impl Future<Output = anyhow::Result<()>> + Send;
373
374 fn list_workers(
375 &self,
376 namespace: &str,
377 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowWorker>>> + Send;
378
379 fn remove_dead_workers(
380 &self,
381 cutoff: f64,
382 ) -> impl Future<Output = anyhow::Result<Vec<String>>> + Send;
383
384 fn list_child_workflows(
387 &self,
388 parent_id: &str,
389 ) -> impl Future<Output = anyhow::Result<Vec<WorkflowRecord>>> + Send;
390
391 fn create_snapshot(
394 &self,
395 workflow_id: &str,
396 event_seq: i32,
397 state_json: &str,
398 ) -> impl Future<Output = anyhow::Result<()>> + Send;
399
400 fn get_latest_snapshot(
401 &self,
402 workflow_id: &str,
403 ) -> impl Future<Output = anyhow::Result<Option<WorkflowSnapshot>>> + Send;
404
405 fn get_queue_stats(
408 &self,
409 namespace: &str,
410 ) -> impl Future<Output = anyhow::Result<Vec<QueueStats>>> + Send;
411
412 fn try_acquire_scheduler_lock(&self) -> impl Future<Output = anyhow::Result<bool>> + Send;
420
421 }