duroxide 0.1.27

Durable code execution framework for Rust
Documentation
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#![allow(clippy::unwrap_used)]
#![allow(clippy::clone_on_ref_ptr)]
#![allow(clippy::expect_used)]

use duroxide::EventKind;
use duroxide::providers::Provider;
mod common;
use duroxide::runtime::registry::ActivityRegistry;
use duroxide::runtime::{self};
use duroxide::{ActivityContext, Client, Either3, OrchestrationContext, OrchestrationRegistry};
use std::sync::Arc as StdArc;
use std::time::Duration;

use std::future::Future;
use std::pin::Pin;

// Type alias for boxed futures with heterogeneous outputs (Activity, External, Timer)
type BoxedJoinFuture<'a> = Pin<Box<dyn Future<Output = Either3<Result<String, String>, String, ()>> + Send + 'a>>;

async fn concurrent_orchestrations_different_activities_with(store: StdArc<dyn Provider>) {
    let o1 = |ctx: OrchestrationContext, _input: String| async move {
        // Wrap heterogeneous futures in boxed futures with common type
        let f_a: BoxedJoinFuture = Box::pin(async { Either3::First(ctx.schedule_activity("Add", "2,3").await) });
        let f_e: BoxedJoinFuture = Box::pin(async { Either3::Second(ctx.schedule_wait("Go").await) });
        let f_t: BoxedJoinFuture = Box::pin(async {
            ctx.schedule_timer(Duration::from_millis(1)).await;
            Either3::Third(())
        });
        let results = ctx.join(vec![f_a, f_e, f_t]).await;

        // Find activity and external results (order may vary due to history ordering)
        let mut a = None;
        let mut e = None;
        for result in results {
            match result {
                Either3::First(Ok(activity_result)) => {
                    a = Some(activity_result);
                }
                Either3::Second(external_data) => {
                    e = Some(external_data);
                }
                _ => {} // Ignore timer for now
            }
        }

        let a = a.expect("activity result not found");
        let e = e.expect("external result not found");

        Ok(format!("o1:sum={a};evt={e}"))
    };
    let o2 = |ctx: OrchestrationContext, _input: String| async move {
        // Wrap heterogeneous futures in boxed futures with common type
        let f_a: BoxedJoinFuture = Box::pin(async { Either3::First(ctx.schedule_activity("Upper", "hi").await) });
        let f_e: BoxedJoinFuture = Box::pin(async { Either3::Second(ctx.schedule_wait("Go").await) });
        let f_t: BoxedJoinFuture = Box::pin(async {
            ctx.schedule_timer(Duration::from_millis(1)).await;
            Either3::Third(())
        });
        let results = ctx.join(vec![f_a, f_e, f_t]).await;

        // Find activity and external results (order may vary due to history ordering)
        let mut a = None;
        let mut e = None;
        for result in results {
            match result {
                Either3::First(Ok(activity_result)) => {
                    a = Some(activity_result);
                }
                Either3::Second(external_data) => {
                    e = Some(external_data);
                }
                _ => {} // Ignore timer for now
            }
        }

        let a = a.expect("activity result not found");
        let e = e.expect("external result not found");

        Ok(format!("o2:up={a};evt={e}"))
    };

    let activity_registry = ActivityRegistry::builder()
        .register("Add", |_ctx: ActivityContext, input: String| async move {
            let mut parts = input.split(',');
            let a = parts.next().unwrap_or("0").parse::<i64>().unwrap_or(0);
            let b = parts.next().unwrap_or("0").parse::<i64>().unwrap_or(0);
            Ok((a + b).to_string())
        })
        .register("Upper", |_ctx: ActivityContext, input: String| async move {
            Ok(input.to_uppercase())
        })
        .build();

    let orchestration_registry = OrchestrationRegistry::builder()
        .register("AddOrchestration", o1)
        .register("UpperOrchestration", o2)
        .build();

    let rt = runtime::Runtime::start_with_store(store.clone(), activity_registry, orchestration_registry).await;
    let client = Client::new(store.clone());
    let _ = client.start_orchestration("inst-multi-1", "AddOrchestration", "").await;
    let _ = client
        .start_orchestration("inst-multi-2", "UpperOrchestration", "")
        .await;

    let store_for_wait1 = store.clone();
    tokio::spawn(async move {
        let sfw = store_for_wait1.clone();
        let _ = common::wait_for_subscription(sfw.clone(), "inst-multi-1", "Go", 3000).await;
        let client = Client::new(sfw);
        let _ = client.raise_event("inst-multi-1", "Go", "E1").await;
    });
    let store_for_wait2 = store.clone();
    tokio::spawn(async move {
        let sfw = store_for_wait2.clone();
        let _ = common::wait_for_subscription(sfw.clone(), "inst-multi-2", "Go", 3000).await;
        let client = Client::new(sfw);
        let _ = client.raise_event("inst-multi-2", "Go", "E2").await;
    });

    let out1_result = Client::new(store.clone())
        .wait_for_orchestration("inst-multi-1", std::time::Duration::from_secs(10))
        .await;

    if out1_result.is_err() {
        let hist1 = store.read("inst-multi-1").await.unwrap_or_default();
        println!("inst-multi-1 history ({} events):", hist1.len());
        for (i, e) in hist1.iter().enumerate() {
            println!("  {i}: {e:?}");
        }
    }

    let out1 = match out1_result.unwrap() {
        duroxide::OrchestrationStatus::Completed { output, .. } => Ok(output),
        duroxide::OrchestrationStatus::Failed { details, .. } => Err(details.display_message()),
        _ => panic!("unexpected orchestration status"),
    };

    let out2_result = Client::new(store.clone())
        .wait_for_orchestration("inst-multi-2", std::time::Duration::from_secs(10))
        .await;

    if out2_result.is_err() {
        let hist2 = store.read("inst-multi-2").await.unwrap_or_default();
        println!("inst-multi-2 history ({} events):", hist2.len());
        for (i, e) in hist2.iter().enumerate() {
            println!("  {i}: {e:?}");
        }
    }

    let out2 = match out2_result.unwrap() {
        duroxide::OrchestrationStatus::Completed { output, .. } => Ok(output),
        duroxide::OrchestrationStatus::Failed { details, .. } => Err(details.display_message()),
        _ => panic!("unexpected orchestration status"),
    };

    assert!(
        out1.as_ref().unwrap().contains("o1:sum=5;evt=E1"),
        "unexpected out1: {out1:?}"
    );
    assert!(
        out2.as_ref().unwrap().contains("o2:up=HI;evt=E2"),
        "unexpected out2: {out2:?}"
    );

    // Check histories
    let hist1 = client.read_execution_history("inst-multi-1", 1).await.unwrap();
    let hist2 = client.read_execution_history("inst-multi-2", 1).await.unwrap();

    assert!(
        hist1
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ActivityCompleted { result, .. } if e.source_event_id == Some(2) && result == "5"))
    );
    assert!(
        hist2
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ActivityCompleted { result, .. } if e.source_event_id == Some(2) && result == "HI"))
    );
    assert!(
        hist1
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ExternalEvent { data, .. } if data == "E1"))
    );
    assert!(
        hist2
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ExternalEvent { data, .. } if data == "E2"))
    );
    assert!(hist1.iter().any(|e| matches!(&e.kind, EventKind::TimerFired { .. })));
    assert!(hist2.iter().any(|e| matches!(&e.kind, EventKind::TimerFired { .. })));

    rt.shutdown(None).await;
}

#[tokio::test]
async fn concurrent_orchestrations_different_activities_fs() {
    let (store, _temp_dir) = common::create_sqlite_store_disk().await;
    concurrent_orchestrations_different_activities_with(store).await;
}

async fn concurrent_orchestrations_same_activities_with(store: StdArc<dyn Provider>) {
    let o1 = |ctx: OrchestrationContext, _input: String| async move {
        let f_a: BoxedJoinFuture = Box::pin(async { Either3::First(ctx.schedule_activity("Proc", "10").await) });
        let f_e: BoxedJoinFuture = Box::pin(async { Either3::Second(ctx.schedule_wait("Go").await) });
        let f_t: BoxedJoinFuture = Box::pin(async {
            ctx.schedule_timer(Duration::from_millis(1)).await;
            Either3::Third(())
        });
        let results = ctx.join(vec![f_a, f_e, f_t]).await;

        // Find activity and external results (order may vary due to history ordering)
        let mut a = None;
        let mut e = None;
        for result in results {
            match result {
                Either3::First(Ok(activity_result)) => {
                    a = Some(activity_result);
                }
                Either3::Second(external_data) => {
                    e = Some(external_data);
                }
                _ => {} // Ignore timer for now
            }
        }

        let a = a.expect("activity result not found");
        let e = e.expect("external result not found");

        Ok(format!("o1:a={a};evt={e}"))
    };
    let o2 = |ctx: OrchestrationContext, _input: String| async move {
        let _guid = ctx.new_guid().await?;
        let f_a: BoxedJoinFuture = Box::pin(async { Either3::First(ctx.schedule_activity("Proc", "20").await) });
        let f_e: BoxedJoinFuture = Box::pin(async { Either3::Second(ctx.schedule_wait("Go").await) });
        let f_t: BoxedJoinFuture = Box::pin(async {
            ctx.schedule_timer(Duration::from_millis(1)).await;
            Either3::Third(())
        });
        let results = ctx.join(vec![f_a, f_e, f_t]).await;

        // Find activity and external results (order may vary due to history ordering)
        let mut a = None;
        let mut e = None;
        for result in results {
            match result {
                Either3::First(Ok(activity_result)) => {
                    a = Some(activity_result);
                }
                Either3::Second(external_data) => {
                    e = Some(external_data);
                }
                _ => {} // Ignore timer for now
            }
        }

        let a = a.expect("activity result not found");
        let e = e.expect("external result not found");

        Ok(format!("o2:a={a};evt={e}"))
    };

    let activity_registry = ActivityRegistry::builder()
        .register("Proc", |_ctx: ActivityContext, input: String| async move {
            let n = input.parse::<i64>().unwrap_or(0);
            Ok((n + 1).to_string())
        })
        .build();

    let orchestration_registry = OrchestrationRegistry::builder()
        .register("ProcOrchestration1", o1)
        .register("ProcOrchestration2", o2)
        .build();

    let rt = runtime::Runtime::start_with_store(store.clone(), activity_registry, orchestration_registry).await;
    let client = Client::new(store.clone());
    let _ = client
        .start_orchestration("inst-same-acts-1", "ProcOrchestration1", "")
        .await;
    let _ = client
        .start_orchestration("inst-same-acts-2", "ProcOrchestration2", "")
        .await;

    let store_for_wait3 = store.clone();
    tokio::spawn(async move {
        let sfw = store_for_wait3.clone();
        let _ = common::wait_for_subscription(sfw.clone(), "inst-same-acts-1", "Go", 3000).await;
        let client = Client::new(sfw);
        let _ = client.raise_event("inst-same-acts-1", "Go", "P1").await;
    });
    let store_for_wait4 = store.clone();
    tokio::spawn(async move {
        let sfw = store_for_wait4.clone();
        let _ = common::wait_for_subscription(sfw.clone(), "inst-same-acts-2", "Go", 3000).await;
        let client = Client::new(sfw);
        let _ = client.raise_event("inst-same-acts-2", "Go", "P2").await;
    });

    match Client::new(store.clone())
        .wait_for_orchestration("inst-same-acts-1", std::time::Duration::from_secs(10))
        .await
        .unwrap()
    {
        duroxide::OrchestrationStatus::Completed { output, .. } => assert_eq!(output, "o1:a=11;evt=P1"),
        duroxide::OrchestrationStatus::Failed { details, .. } => {
            panic!("orchestration failed: {}", details.display_message())
        }
        _ => panic!("unexpected orchestration status"),
    }

    match Client::new(store.clone())
        .wait_for_orchestration("inst-same-acts-2", std::time::Duration::from_secs(10))
        .await
        .unwrap()
    {
        duroxide::OrchestrationStatus::Completed { output, .. } => assert_eq!(output, "o2:a=21;evt=P2"),
        duroxide::OrchestrationStatus::Failed { details, .. } => {
            panic!("orchestration failed: {}", details.display_message())
        }
        _ => panic!("unexpected orchestration status"),
    }

    // Check histories
    let hist1 = client.read_execution_history("inst-same-acts-1", 1).await.unwrap();
    let hist2 = client.read_execution_history("inst-same-acts-2", 1).await.unwrap();

    assert!(
        hist1
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ActivityCompleted { result, .. } if result == "11"))
    );
    assert!(
        hist2
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ActivityCompleted { result, .. } if result == "21"))
    );
    assert!(
        hist1
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ExternalEvent { data, .. } if data == "P1"))
    );
    assert!(
        hist2
            .iter()
            .any(|e| matches!(&e.kind, EventKind::ExternalEvent { data, .. } if data == "P2"))
    );
    assert!(hist1.iter().any(|e| matches!(&e.kind, EventKind::TimerFired { .. })));
    assert!(hist2.iter().any(|e| matches!(&e.kind, EventKind::TimerFired { .. })));

    rt.shutdown(None).await;
}

#[tokio::test]
async fn concurrent_orchestrations_same_activities_fs() {
    let (store, _temp_dir) = common::create_sqlite_store_disk().await;
    concurrent_orchestrations_same_activities_with(store).await;
}

#[tokio::test]
async fn single_orchestration_with_join_test() {
    let (store, _temp_dir) = common::create_sqlite_store_disk().await;

    // Just run ONE orchestration (same as o1 from the concurrent test)
    let o1 = |ctx: OrchestrationContext, _input: String| async move {
        let f_a: BoxedJoinFuture = Box::pin(async { Either3::First(ctx.schedule_activity("Proc", "10").await) });
        let f_e: BoxedJoinFuture = Box::pin(async { Either3::Second(ctx.schedule_wait("Go").await) });
        let f_t: BoxedJoinFuture = Box::pin(async {
            ctx.schedule_timer(Duration::from_millis(1)).await;
            Either3::Third(())
        });
        let results = ctx.join(vec![f_a, f_e, f_t]).await;

        let mut a = None;
        let mut e = None;
        for result in results {
            match result {
                Either3::First(Ok(activity_result)) => {
                    a = Some(activity_result);
                }
                Either3::Second(external_data) => {
                    e = Some(external_data);
                }
                _ => {}
            }
        }

        let a = a.expect("activity result not found");
        let e = e.expect("external result not found");
        Ok(format!("o1:a={a};evt={e}"))
    };

    let activity_registry = ActivityRegistry::builder()
        .register("Proc", |_ctx: ActivityContext, input: String| async move {
            let n = input.parse::<i64>().unwrap_or(0);
            Ok((n + 1).to_string())
        })
        .build();

    let orchestration_registry = OrchestrationRegistry::builder()
        .register("ProcOrchestration1", o1)
        .build();

    let rt = runtime::Runtime::start_with_store(store.clone(), activity_registry, orchestration_registry).await;
    let client = Client::new(store.clone());

    // Start only ONE orchestration
    let _ = client
        .start_orchestration("inst-single", "ProcOrchestration1", "")
        .await;

    // Raise event
    let store_for_wait = store.clone();
    tokio::spawn(async move {
        let _ = common::wait_for_subscription(store_for_wait.clone(), "inst-single", "Go", 3000).await;
        let client = Client::new(store_for_wait);
        let _ = client.raise_event("inst-single", "Go", "P1").await;
    });

    // Wait for completion
    let result = client
        .wait_for_orchestration("inst-single", std::time::Duration::from_secs(10))
        .await;

    if result.is_err() {
        let hist = store.read("inst-single").await.unwrap_or_default();
        println!("❌ Timeout! History ({} events):", hist.len());
        for (i, e) in hist.iter().enumerate() {
            println!("  {i}: {e:?}");
        }
    }

    match result.unwrap() {
        duroxide::OrchestrationStatus::Completed { output, .. } => {
            println!("✅ Single orch completed: {output}");
            assert_eq!(output, "o1:a=11;evt=P1");
        }
        duroxide::OrchestrationStatus::Failed { details, .. } => {
            panic!("orchestration failed: {}", details.display_message())
        }
        _ => panic!("unexpected orchestration status"),
    }

    rt.shutdown(None).await;
}