dbos 0.5.0

Lightweight durable workflows on Postgres
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
//! Running workflows, against real databases.

use std::borrow::Cow;
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;

use dbos::sysdb::SystemDatabase;
use dbos::sysdb::postgres::{PostgresSystemDatabase, Settings};
use dbos::sysdb::types::WorkflowStatus;
use dbos::sysdb::{BackendError, BackendErrorKind};
use dbos::{Config, DBOS, Error, RunOptions};

use dbos_test_support::{TestDatabase, test_database};

/// The version every instance in this file launches with: DBOS computes none, so a launch without
/// one fails — and sharing it is what lets a relaunch recover what the previous launch left.
const APP_VERSION: &str = "1.0.0";

fn config(app_name: &str, db: &TestDatabase) -> Config {
    Config {
        migrate: false,
        app_version: Some(APP_VERSION.to_owned()),
        ..Config::new(app_name, db.url())
    }
}

/// A handle for reading rows behind the instance's back.
async fn reader(db: &TestDatabase) -> PostgresSystemDatabase {
    PostgresSystemDatabase::from_pool(db.pool().await, &Settings::default())
}

async fn double(n: u32) -> dbos::Result<u32> {
    Ok(n * 2)
}

async fn greet(_: ()) -> dbos::Result<String> {
    Ok("hello".to_owned())
}

/// The ordinary case: a workflow runs, returns, and its row records what it returned.
#[tokio::test]
async fn a_workflow_runs_and_records_its_output() {
    let db = test_database().await;
    let dbos = DBOS::new(config("run-app", &db));
    let double = dbos.register_workflow("double", double).unwrap();
    dbos.launch().await.expect("launch failed");

    assert_eq!(double.run(21).await.expect("the workflow failed"), 42);

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    let [row] = &rows[..] else {
        panic!("expected exactly one workflow, got {}", rows.len())
    };
    assert_eq!(row.status, WorkflowStatus::Success);
    assert_eq!(row.name.as_deref(), Some("double"));
    assert_eq!(row.output.as_deref(), Some("42"));
    assert_eq!(row.input.as_deref(), Some("21"));
    assert_eq!(row.serialization.as_deref(), Some("rust_serde"));
    assert_eq!(
        row.application_version.as_deref(),
        Some(&*dbos.app_version().unwrap())
    );
    assert_eq!(row.executor_id.as_deref(), Some("local"));

    dbos.shutdown().await;
}

/// Attributes reach the row of a workflow this process runs, not only of one that was enqueued.
///
/// Searchable metadata is a property of the workflow rather than of the queue it waited on, and
/// TypeScript and Python both attach it on the start path too. The child is here because nothing
/// propagates: a child carries what its own call names, and this one names nothing.
#[tokio::test]
async fn a_started_workflow_carries_the_attributes_it_was_given() {
    let db = test_database().await;
    let dbos = DBOS::new(config("attributes-app", &db));
    let double = dbos.register_workflow("double", double).unwrap();
    let parent = dbos
        .register_workflow("parent", move |_: ()| {
            let double = double.clone();
            async move { double.run(21).await }
        })
        .unwrap();
    dbos.launch().await.expect("launch failed");

    let mut attributes = serde_json::Map::new();
    attributes.insert("tenant".to_owned(), serde_json::json!("acme"));

    parent
        .run_with(
            (),
            RunOptions {
                workflow_id: Some("attributed"),
                attributes: Some(&attributes),
                ..Default::default()
            },
        )
        .await
        .expect("the workflow failed");

    let reader = reader(&db).await;
    let row = reader
        .get_workflow("attributed")
        .await
        .expect("read failed")
        .expect("the row is missing");
    assert!(
        row.attributes
            .as_deref()
            .is_some_and(|json| json.contains("acme")),
        "{:?}",
        row.attributes
    );

    let child = reader
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed")
        .into_iter()
        .find(|row| row.workflow_id != "attributed")
        .expect("the child is missing");
    assert_eq!(
        child.attributes, None,
        "a child carries what its own call names, and inherits nothing"
    );

    dbos.shutdown().await;
}

/// A zero-argument workflow stores no input, and must not need a literal `null` to run.
#[tokio::test]
async fn a_zero_argument_workflow_records_no_input() {
    let db = test_database().await;
    let dbos = DBOS::new(config("noarg-app", &db));
    let greet = dbos.register_workflow("greet", greet).unwrap();
    dbos.launch().await.expect("launch failed");

    assert_eq!(greet.run(()).await.expect("the workflow failed"), "hello");

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(
        rows[0].input.as_deref(),
        Some("null"),
        "`()` encodes as JSON null"
    );
    assert_eq!(rows[0].output.as_deref(), Some("\"hello\""));

    dbos.shutdown().await;
}

/// An application's own error type survives the database intact.
///
/// This is the point of the error being a type parameter: `CardDeclined` comes back as
/// `CardDeclined`, matchable, rather than as a name and a message describing it.
#[tokio::test]
async fn an_application_error_type_round_trips_as_itself() {
    // Two derives and nothing else. No variant holding a `dbos::Error`, no `From` impl, no trait
    // to implement: the engine wraps this type rather than this type making room for the engine.
    #[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
    enum CheckoutError {
        #[error("the card was declined after {attempts} attempts")]
        CardDeclined { attempts: u32 },
    }

    async fn checkout(_: ()) -> dbos::Result<u32, CheckoutError> {
        // `?` lifts the application's error into ours through the blanket conversion the type
        // parameter buys.
        Err(CheckoutError::CardDeclined { attempts: 3 })?
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("typed-error-app", &db));
    let checkout = dbos.register_workflow("checkout", checkout).unwrap();
    dbos.launch().await.expect("launch failed");

    // The run that failed gets its own error, structure and all.
    let err = checkout.run(()).await.unwrap_err();
    assert!(
        matches!(
            err,
            Error::Application(CheckoutError::CardDeclined { attempts: 3 })
        ),
        "{err:?}"
    );

    // And so does a caller adopting the finished workflow, which reads it back out of the column.
    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(rows[0].status, WorkflowStatus::Error);
    let recorded: Error<CheckoutError> =
        serde_json::from_str(rows[0].error.as_deref().expect("an error was recorded"))
            .expect("the column holds the application's own error");
    assert!(
        matches!(
            recorded,
            Error::Application(CheckoutError::CardDeclined { attempts: 3 })
        ),
        "the field survives, not just the message: {recorded:?}"
    );

    dbos.shutdown().await;
}

/// A workflow converts a foreign error at the boundary, because a durable one must serialize.
///
/// The case a library error lands in: a third party's type has no serde derives and cannot cross a
/// column, so it becomes the workflow's own error where the two meet. One `map_err` at the call
/// site, and everything downstream — the row, the replay, the caller — gets full fidelity.
#[tokio::test]
async fn a_foreign_error_is_converted_at_the_boundary() {
    // Somebody else's error type: `Debug + Display` and nothing more.
    #[derive(Debug, thiserror::Error)]
    #[error("the gateway refused the card")]
    struct GatewayRefused;

    async fn charge() -> Result<(), GatewayRefused> {
        Err(GatewayRefused)
    }

    #[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
    enum PaymentError {
        #[error("charging failed: {reason}")]
        Gateway { reason: String },
    }

    async fn pay(_: ()) -> dbos::Result<(), PaymentError> {
        charge().await.map_err(|e| PaymentError::Gateway {
            reason: e.to_string(),
        })?;
        Ok(())
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("fail-app", &db));
    let pay = dbos.register_workflow("pay", pay).unwrap();
    dbos.launch().await.expect("launch failed");

    let err = pay.run(()).await.unwrap_err();
    assert!(
        matches!(err, Error::Application(PaymentError::Gateway { .. })),
        "{err}"
    );
    assert_eq!(
        err.to_string(),
        "charging failed: the gateway refused the card"
    );

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(rows[0].status, WorkflowStatus::Error);
    // The column holds the failure encoded, not stringified, so what comes back out of it is the
    // error itself rather than a description of it.
    let recorded: Error<PaymentError> =
        serde_json::from_str(rows[0].error.as_deref().expect("an error was recorded"))
            .expect("the error column holds an encoded error");
    assert!(matches!(
        recorded,
        Error::Application(PaymentError::Gateway { .. })
    ));
    assert_eq!(recorded.to_string(), err.to_string());

    dbos.shutdown().await;
}

/// A database failure leaves the workflow `PENDING` instead of recording it as failed.
///
/// The blip case: a connection drops while the engine checkpoints, the error propagates out
/// through the workflow body, and the workflow had not failed — the database had. Recording ERROR
/// would assert something false about work that could still be finished, and the row is the only
/// copy of that fact.
#[tokio::test]
async fn a_database_failure_is_not_the_workflows_outcome() {
    async fn blips(_: ()) -> dbos::Result<()> {
        Err(Error::SystemDatabase(dbos::sysdb::Error::Backend(
            BackendError {
                message: "connection reset by peer".to_owned(),
                sqlstate: None,
                kind: BackendErrorKind::Connection,
            },
        )))
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("blip-app", &db));
    let blips = dbos.register_workflow("blips", blips).unwrap();
    dbos.launch().await.expect("launch failed");

    let err = blips.run(()).await.unwrap_err();
    assert!(matches!(err, Error::SystemDatabase(_)), "{err}");

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(
        rows[0].status,
        WorkflowStatus::Pending,
        "left for a later executor to recover, not recorded as failed"
    );
    assert!(
        rows[0].error.is_none(),
        "nothing terminal was written: {:?}",
        rows[0].error
    );

    dbos.shutdown().await;
}

/// Steps inside a real `run` are checkpointed under the workflow that ran them.
#[tokio::test]
async fn a_workflow_records_the_steps_it_took() {
    async fn three_steps(_: ()) -> dbos::Result<u32> {
        let mut total = 0;
        for (n, name) in ["one", "two", "three"].into_iter().enumerate() {
            total += dbos::step(name, || async move { dbos::Result::Ok(n as u32 + 1) }).await?;
        }
        Ok(total)
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("steps-app", &db));
    let workflow = dbos.register_workflow("three_steps", three_steps).unwrap();
    dbos.launch().await.expect("launch failed");

    assert_eq!(workflow.run(()).await.expect("the workflow failed"), 6);

    let reader = reader(&db).await;
    let rows = reader
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    let steps = reader
        .list_workflow_steps(&rows[0].workflow_id, true, None, None, None)
        .await
        .expect("read failed");
    let seen: Vec<(i32, &str, Option<&str>)> = steps
        .iter()
        .map(|s| (s.step_id, s.step_name.as_str(), s.output.as_deref()))
        .collect();
    assert_eq!(
        seen,
        [
            (0, "one", Some("1")),
            (1, "two", Some("2")),
            (2, "three", Some("3"))
        ]
    );

    dbos.shutdown().await;
}

/// A step's error type round-trips the same way, so a replay resumes with the error it recorded.
#[tokio::test]
async fn a_step_error_type_round_trips_as_itself() {
    #[derive(Debug, thiserror::Error, serde::Serialize, serde::Deserialize)]
    enum ChargeError {
        #[error("insufficient funds: short by {short}")]
        InsufficientFunds { short: u32 },
    }

    async fn pay(_: ()) -> dbos::Result<u32, ChargeError> {
        // A step shares the workflow's error channel, so the body needs no type of its own.
        dbos::step("charge", || async {
            Err(ChargeError::InsufficientFunds { short: 12 })?
        })
        .await
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("step-error-app", &db));
    let pay = dbos.register_workflow("pay", pay).unwrap();
    dbos.launch().await.expect("launch failed");

    let err = pay.run(()).await.unwrap_err();
    assert!(
        matches!(
            err,
            Error::Application(ChargeError::InsufficientFunds { short: 12 })
        ),
        "{err:?}"
    );

    let reader = reader(&db).await;
    let rows = reader
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    let steps = reader
        .list_workflow_steps(&rows[0].workflow_id, true, None, None, None)
        .await
        .expect("read failed");
    let recorded: Error<ChargeError> = serde_json::from_str(
        steps[0]
            .error
            .as_deref()
            .expect("the step recorded an error"),
    )
    .expect("the column holds the application's own error");
    assert!(
        matches!(
            recorded,
            Error::Application(ChargeError::InsufficientFunds { short: 12 })
        ),
        "{recorded:?}"
    );

    dbos.shutdown().await;
}

/// A panic is not an outcome: nothing is recorded, and an awaiting caller gets the panic back.
///
/// The row staying PENDING is the point — a panic is treated as a crash, not a failure, so a
/// later executor recovers the workflow instead of it being permanently failed by a bug.
#[tokio::test]
async fn a_panicking_workflow_leaves_its_row_pending() {
    async fn explodes(_: ()) -> dbos::Result<()> {
        panic!("a bug, not an outcome")
    }

    let db = test_database().await;
    let dbos = DBOS::new(config("panic-app", &db));
    let explodes = dbos.register_workflow("explodes", explodes).unwrap();
    dbos.launch().await.expect("launch failed");

    // On its own task, because the panic is re-raised in the awaiting caller.
    let joined = tokio::spawn(async move { explodes.run(()).await }).await;
    let panicked = joined.expect_err("the panic must reach the awaiting caller");
    assert!(panicked.is_panic(), "{panicked}");

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(
        rows[0].status,
        WorkflowStatus::Pending,
        "a panic records nothing: the row is left for recovery, like a crash"
    );
    assert!(rows[0].error.is_none(), "{:?}", rows[0].error);

    dbos.shutdown().await;
}

/// The workflow is recorded before its body runs, which is what makes a crash recoverable.
#[tokio::test]
async fn the_row_exists_before_the_body_starts() {
    let started = Arc::new(tokio::sync::Notify::new());
    let release = Arc::new(tokio::sync::Notify::new());

    let db = test_database().await;
    let dbos = DBOS::new(config("early-row-app", &db));
    let blocked = {
        let (started, release) = (Arc::clone(&started), Arc::clone(&release));
        dbos.register_workflow("blocked", move |()| {
            let (started, release) = (Arc::clone(&started), Arc::clone(&release));
            async move {
                started.notify_one();
                release.notified().await;
                Ok::<_, dbos::Error>(())
            }
        })
        .unwrap()
    };
    dbos.launch().await.expect("launch failed");

    let running = tokio::spawn(async move { blocked.run(()).await });
    started.notified().await;

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(
        rows.len(),
        1,
        "the row is written before the body is entered"
    );
    assert_eq!(rows[0].status, WorkflowStatus::Pending);

    release.notify_one();
    running
        .await
        .expect("the task panicked")
        .expect("the workflow failed");
    dbos.shutdown().await;
}

/// Shutdown cancels what is still running and leaves it PENDING for a later executor.
#[tokio::test]
async fn shutdown_cancels_a_running_workflow_and_leaves_it_pending() {
    let started = Arc::new(tokio::sync::Notify::new());
    let finished = Arc::new(AtomicU32::new(0));

    let db = test_database().await;
    let dbos = DBOS::new(config("cancel-app", &db));
    let forever = {
        let (started, finished) = (Arc::clone(&started), Arc::clone(&finished));
        dbos.register_workflow("forever", move |()| {
            let (started, finished) = (Arc::clone(&started), Arc::clone(&finished));
            async move {
                started.notify_one();
                tokio::time::sleep(Duration::from_secs(3600)).await;
                finished.fetch_add(1, Ordering::SeqCst);
                Ok::<_, dbos::Error>(())
            }
        })
        .unwrap()
    };
    dbos.launch().await.expect("launch failed");

    let running = tokio::spawn(async move { forever.run(()).await });
    started.notified().await;

    dbos.shutdown().await;

    let err = running.await.expect("the task panicked").unwrap_err();
    assert!(matches!(err, Error::Interrupted { .. }), "{err}");
    assert_eq!(
        finished.load(Ordering::SeqCst),
        0,
        "the body never got past the sleep"
    );

    let rows = reader(&db)
        .await
        .list_workflows(&Default::default(), None)
        .await
        .expect("read failed");
    assert_eq!(
        rows[0].status,
        WorkflowStatus::Pending,
        "left PENDING on purpose: a later executor recovers it, so nothing is lost"
    );

    dbos.shutdown().await;
}

/// Dropping the returned future does not stop the workflow — awaiting is a convenience over a
/// durable run, not the thing that makes it durable.
#[tokio::test]
async fn dropping_the_future_does_not_stop_the_workflow() {
    let started = Arc::new(tokio::sync::Notify::new());
    let db = test_database().await;
    let dbos = DBOS::new(config("detached-app", &db));
    let slow = {
        let started = Arc::clone(&started);
        dbos.register_workflow("slow", move |()| {
            let started = Arc::clone(&started);
            async move {
                started.notify_one();
                tokio::time::sleep(Duration::from_millis(200)).await;
                Ok::<_, dbos::Error>(7u32)
            }
        })
        .unwrap()
    };
    dbos.launch().await.expect("launch failed");

    {
        let mut future = Box::pin(slow.run(()));
        // Poll it far enough to start the workflow, then throw the future away.
        tokio::select! {
            _ = &mut future => panic!("it should not have finished yet"),
            () = started.notified() => {}
        }
    }

    let reader = reader(&db).await;
    for _ in 0..50 {
        tokio::time::sleep(Duration::from_millis(100)).await;
        let rows = reader
            .list_workflows(&Default::default(), None)
            .await
            .expect("read failed");
        if rows[0].status == WorkflowStatus::Success {
            assert_eq!(rows[0].output.as_deref(), Some("7"));
            dbos.shutdown().await;
            return;
        }
    }
    panic!("the workflow never finished after its caller stopped waiting");
}

/// A workflow started against an instance that is not launched is an error, not a panic.
#[tokio::test]
async fn running_before_launch_is_refused() {
    let db = test_database().await;
    let dbos = DBOS::new(config("unlaunched-app", &db));
    let double = dbos.register_workflow("double", double).unwrap();

    // `run` is `start` plus awaiting, so the refusal names the start — the half that was refused.
    let err = double.run(1).await.unwrap_err();
    assert!(
        matches!(
            err,
            Error::NotLaunched {
                operation: Cow::Borrowed("start a workflow")
            }
        ),
        "{err}"
    );
}