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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
//! Workflow messages: a payload sent to one workflow, for that workflow to receive once.
//!
//! The counterpart of [`event`](crate::event) and the mirror image of its asymmetry. An event is
//! written by one workflow and readable by anyone, so its *reader* has three surfaces and its
//! writer has one. A message is written by anyone and readable only by the workflow it was
//! addressed to, so it is the *sender* that has three surfaces and the receiver that has one:
//!
//! | | inside a workflow | outside |
//! |---|---|---|
//! | [`send`] | free function | [`DBOS::send`], [`Client::send`](crate::Client::send) |
//! | [`send_bulk`] | free function | [`DBOS::send_bulk`], [`Client::send_bulk`](crate::Client::send_bulk) |
//! | [`recv`] | free function | — |
//!
//! **[`recv`] has no form outside a workflow, and that is structural rather than an omission.** A
//! receive consumes: the message is marked consumed in the same transaction that records the step
//! that took it, so exactly one receiver gets it and a replay gets it again rather than taking
//! another. A caller with no workflow has no step to record against, so it could only consume and
//! then lose the message if anything went wrong afterwards. `sysdb` states this at
//! [`SystemDatabase::recv`](crate::sysdb::SystemDatabase::recv): its caller is not optional, where
//! [`get_event`](crate::get_event)'s is. Code outside a workflow
//! that wants to observe messages without taking them has
//! [`get_all_notifications`](crate::sysdb::SystemDatabase::get_all_notifications).
//!
//! **The two calls differ on standing inside a step.** A [`send`] there is plain and
//! uncheckpointed; a [`recv`] there is [`Error::InsideStep`]. The asymmetry is the point: a send
//! that runs twice delivers twice, while a receive that runs twice *loses* a message, and only the
//! second is unrecoverable.
//!
//! **The free [`send`] is not just symmetry with [`set_event`](crate::set_event).** The registry
//! lives on the instance, so a registered closure that captures a [`DBOS`] is stored inside the
//! very `Arc` it holds a strong reference to — a cycle that keeps the instance, its executor and
//! its connection pool alive for the life of the process. Making a workflow capture a handle in
//! order to send a message would have made that the documented way to write one.
//!
//! **Every send in the crate is here, and they meet at one place per surface.**
//! `Connection::send_encoded` and `Connection::send_all_encoded` are the shared paths — each takes
//! the payloads its caller encoded, names the encoding, and hands `sysdb` the matching call — so
//! the surfaces above them differ only in what they can say before they reach one: which ambient
//! context to read, whether the send is checkpointed, and whether a handle's executor has to be
//! reconciled with it.
//! That is [`event`](crate::event)'s shape too, where `Connection::get_event` is the one read under
//! three callers. The single send and the batch stay separate all the way down, because which one
//! is called is what chooses the recorded step name — `sysdb` takes it from the method rather than
//! deriving it from a count.
//!
//! **What is sent, and how the call behaves, are two different things.** The plain forms read
//! `send(destination_id, message)` and `send_bulk(messages)`. The `_with` forms take a
//! [`SendMessage`] (or a slice of them) for everything about the message itself — destination,
//! payload, topic, idempotency key — and [`SendOptions`] for everything about the call — the fork
//! fan-out and the encoding. [`send_with`] takes one [`SendMessage`] where [`send_bulk_with`] takes
//! a slice, and the same [`SendOptions`] serves both. That is the split
//! [`step`](crate::step())/[`step_with`](crate::step_with) and
//! `fork`/[`fork_with`](crate::DBOS::fork_with) already make: the plain form for the common case,
//! the `_with` form for everything else. [`fork_all`](crate::DBOS::fork_all) splits its batch the
//! same way, with [`Fork`](crate::Fork) beside [`ForkOptions`](crate::ForkOptions).
//!
//! **The batch is on all three surfaces, not just the client.** Inside a workflow it is
//! checkpointed as one step for the whole batch, so a workflow fanning messages out gets the same
//! all-or-none delivery a client does, and a replay sends none of it again.
//!
//! All of these are thin. `sysdb` owns the transactional insert, the fork fan-out, the replay
//! skip, the consuming read, the concurrent-receive guard and the step names
//! (`DBOS.send`, `DBOS.recv`). What the engine adds is the step ids from the ambient context, the
//! payload encoding, and the guards on where each call may stand.
use Arc;
use Duration;
use Serialize;
use DeserializeOwned;
use crateDBOS;
use crate;
use crateConnection;
use crateCtx;
use crate;
use crate;
use crateMessage as SysdbMessage;
use cratestep_names;
/// Sends a message to a workflow, for it to [`recv`] when it is ready.
///
/// The sender for a workflow body, and the counterpart of [`recv`]: it takes the executor and the
/// step-id sequence from the ambient context, so a workflow that sends needs no [`DBOS`] handle and
/// its registered closure captures nothing. That is what keeps the registry free of strong
/// references back to the instance holding it — see the module documentation.
///
/// [`SendMessage::new`] addresses the default topic; a receiver selects on the topic, so a message
/// sent under one nobody is receiving on waits in the database rather than being delivered to a
/// different [`recv`].
///
/// The message waits until the destination reads it, so sending to a workflow that has not reached
/// its receive — or is not running at all — is normal rather than an error. Sending to a workflow
/// that *does not exist* is [`Error::SystemDatabase`] carrying the system database's
/// non-existent-workflow error: the foreign key catches it, so a message is never left addressed to
/// nothing.
///
/// The send is checkpointed under a step id, so a replay does not send twice — `sysdb` finds the
/// recorded step and inserts nothing, in the same transaction that would have written. **That is a
/// workflow's whole idempotency**, and it is why a workflow body rarely sets
/// [`SendMessage::idempotency_key`]: the step already makes the send exactly-once. The key is
/// available here all the same, through [`send_with`], and it is what a send from inside a *step*
/// has instead.
///
/// **The step id is taken at the call, not at the first poll** — see [`PendingStep`] — so a send
/// built beside a step and driven with it by `tokio::join!` takes the same slot on every execution.
/// The payload is encoded first, ahead of the id, so a message that cannot be encoded is a send
/// that never happened and never moved the counter.
///
/// The error is the *workflow's* channel, like [`step`](crate::step())'s and
/// [`set_event`](crate::set_event)'s, so `?` needs no conversion.
///
/// **From inside a step it sends plainly, with no checkpoint** — the leaf rule
/// [`get_event`](crate::get_event) follows, the enclosing step's own checkpoint standing for
/// everything its body did. The cost is real: a step that retries sends again, and this form has no
/// idempotency key to stop it. A send that must happen once across retries wants [`send_with`] and
/// a [`SendMessage::idempotency_key`], or wants hoisting out of the step, where the step id makes
/// it exactly-once.
///
/// Outside a workflow there is no context to take an executor from, so this is
/// [`Error::NotInWorkflow`]. That is where [`DBOS::send`] is the call.
/// [`send`], with a [`SendMessage`] that may carry a topic or an idempotency key, and
/// [`SendOptions`] rather than the defaults.
///
/// ```no_run
/// # async fn approve() -> dbos::Result<()> {
/// use dbos::{SendMessage, SendOptions};
/// dbos::send_with(
/// SendMessage {
/// topic: Some("approvals"),
/// ..SendMessage::new("order-42", &"approved")
/// },
/// SendOptions::default(),
/// )
/// .await
/// # }
/// ```
///
/// The same call in every respect but the options: see [`send`] for what it does, where it may
/// stand, and what a step does to it. Recorded as `DBOS.send`, not `DBOS.sendBulk`, although it
/// takes the same [`SendMessage`] a batch does: the step name says which surface was reached for.
/// Sends many messages in one transaction, for their destinations to [`recv`] when they are ready.
///
/// **All or none**, which is the reason to prefer this over a loop of [`send`]: the batch is one
/// insert, so a failure halfway through delivers nothing rather than a prefix.
///
/// The batch is checkpointed as **one** step, so a replay sends none of it again. The step is
/// recorded as `DBOS.sendBulk` however long the batch is, a batch of exactly one included: the
/// name says which API surface was reached for, not how many messages it carried. A workflow that
/// swaps a [`send`] for a [`send_bulk`] between runs therefore flips names and is caught as a
/// determinism error, while one whose message *count* merely changes is not, that being no change
/// of operation.
///
/// A batch takes [`SendMessage`] values, which carry their own topic and idempotency key: those
/// vary per message, so they travel with the message, while [`SendOptions`] on [`send_bulk_with`]
/// holds what is uniform across the call.
///
/// # Why `bulk` and not `_all`
///
/// The bulk *management* verbs here are [`cancel_all`](DBOS::cancel_all),
/// [`resume_all`](DBOS::resume_all), [`fork_all`](DBOS::fork_all) and
/// [`delete_all`](DBOS::delete_all), so `send_all` would have been the local rhyme. It is not the
/// name because those four render a *pluralised noun* — the `workflows` the free forms below spell
/// out — into a form that reads on a type where the noun is implicit. A batch send is not that: it
/// has no noun to pluralise, and `sysdb` already records this call as `DBOS.sendBulk`. A method
/// named for one word that writes another is a seam for nothing.
///
/// The management verbs' *free* forms do take the plural noun —
/// [`cancel_workflows`](crate::cancel_workflows), [`fork_workflows`](crate::fork_workflows) —
/// because a free function has no type to leave the noun implicit on. That is the same rule from
/// the other side, and it still gives a batch send no noun to pluralise: a free `send_bulk` sits
/// beside a free `send`, as it does on [`DBOS`].
/// [`send_bulk`], with [`SendOptions`] rather than the defaults.
/// Takes the oldest message sent to this workflow, waiting up to `timeout` for one to arrive.
///
/// The receiver, and the one surface in this module with no form outside a workflow: the workflow
/// receiving *is* the workflow calling, so there is no destination to name — a receive is always
/// for the caller's own mailbox. See the module documentation for why no client can have this.
///
/// `topic` of `None` is the default topic, and a receive on one topic never takes a message sent on
/// another.
///
/// `Ok(None)` means nothing was waiting when the deadline passed — absence is a value, not an
/// error, and `Duration::ZERO` makes this a poll: look once, do not wait.
///
/// The receive is checkpointed as two steps (the read and its deadline), so a replay returns the
/// message the first run took, including a timeout's `None`, instead of consuming a second one.
/// **Both ids are taken at the call, not at the first poll** — see [`PendingStep`] — so a receive
/// built beside another durable call and driven with it takes the same slots on every execution.
/// The error is the *workflow's* channel, like [`step`](crate::step())'s, so `?` needs no
/// conversion.
///
/// **Two receives on one topic at once both wait**, and a message goes to one of them. Two in one
/// execution (a receive joined with another) leave the one that missed out waiting out its timeout,
/// and returning `None` unless a second message comes.
///
/// # Why not from inside a step
///
/// [`Error::InsideStep`], where [`get_event`](crate::get_event) reads plainly from inside a step
/// under the leaf rule — the enclosing step's own checkpoint standing for everything its body did.
/// A receive cannot take that rule, because it is not only a read: it *consumes*. The step's
/// checkpoint records that the body ran, not which message it swallowed, so a retried attempt would
/// consume a second message and the first would be gone with nothing recording it. `sysdb` says the
/// same by construction — its `recv` caller is a required parameter, so there is no uncheckpointed
/// form of this call to degrade to.
///
/// **[`send`] is the other way, and deliberately.** `sysdb` takes `None` for its caller there, so
/// an uncheckpointed send is expressible where an uncheckpointed receive is not — and a send that
/// runs twice delivers twice, where a receive that runs twice *loses* a message.
/// Everything [`recv`] settles before it has run anything: the two refusals, and then both ids.
///
/// **Two ids, and only one of them fits in a [`StepPlacement`].** Field order is the contract, as
/// it is for [`get_event`](crate::get_event)'s caller: the read's id first, the deadline's second,
/// matching what every SDK records and what a replay looks up. So the deadline's id comes from the
/// counter immediately behind the one the placement took.
/// Where a send stands, with the payloads it has already encoded kept beside it.
///
/// **The order is the contract**: the payloads are encoded by the caller *before* this is reached,
/// so a send that cannot be encoded is one that never happened and never moved the workflow's step
/// counter. A send refused here — an unlaunched instance, another instance's handle — moves it no
/// further.
/// One message with its payload already encoded, which is what a send carries from its call into
/// its run.
///
/// [`SendMessage`] cannot be that: it holds a *reference* to an unencoded payload, so keeping one
/// would make the run generic over the caller's `T` and demand `T: Sync` of every sender for no
/// reason — the run has no use for the value, only for the string it encoded to.
///
/// **The three addressing fields are owned for the same reason.** Borrowing them would tie the
/// pending step to the caller's [`SendMessage`], so a send built from temporaries could not be held
/// past its statement or joined with another call. Owning them, the step borrows nothing from its
/// caller, as the payload already did not.
/// One message, encoded in `encoding`, which the caller has resolved.
///
/// Encoded before the step id is taken, for the reason [`place_send`] gives.
/// Every payload in a batch, encoded, or nothing.
///
/// Encoded up front so that nothing is sent when one payload cannot be: the whole batch is one
/// transaction, and failing halfway through the encoding would be the prefix a batch exists to
/// avoid. Ahead of the step id for the reason [`place_send`] gives.
/// A single send as a [`PendingStep`], for the two surfaces that take a step id.
///
/// The free [`send`] and [`DBOS::send`] differ only in how they reach a connection and in which
/// error channel they answer in, which is the whole of what `built` carries; a
/// [`Client`](crate::Client)'s stays a plain `async fn`, because it takes no id and is legitimately
/// driven from anywhere.
/// A batch send as a [`PendingStep`] — see [`pending_send`], of which this is the plural.
/// One message: a payload, the workflow it is for, and how to address it.
///
/// **Everything about the message, and nothing about the call.** [`send_with`] takes one and the
/// [`send_bulk`] family a slice of them; how the call behaves — the fork fan-out, the encoding —
/// is [`SendOptions`], beside it. The topic and the idempotency key are here rather than there
/// because in a batch they vary per message.
///
/// [`SendMessage::new`] plus functional update, like everything else that takes options here:
///
/// ```no_run
/// # use dbos::SendMessage;
/// let messages = [SendMessage {
/// topic: Some("approvals"),
/// ..SendMessage::new("order-42", &"approved")
/// }];
/// ```
///
/// [`Clone`] and [`Copy`] whatever `T` is, since every field is a reference; [`Debug`] and the
/// equality traits are derived, and need `T` to have them, because they look at the payload.
/// How a send behaves, as opposed to what it sends.
///
/// **Taken by both [`send_with`] and [`send_bulk_with`], on every surface.** What is sent — the
/// destination, the payload, a topic, an idempotency key — is a [`SendMessage`]; what is left here
/// is what a call decides once for every message it carries: whether the messages also reach the
/// destinations' forks, and how they are encoded.
///
/// **A struct so that the next option is not a breaking change.** A send's options grow over time,
/// and each one arriving as another positional parameter would break every call site. This is where
/// they land instead.
///
/// Built by functional update from [`Default`], as [`Config`](crate::Config) and
/// [`ForkOptions`](crate::ForkOptions) are:
///
/// ```no_run
/// # use dbos::{Forks, SendOptions};
/// let options = SendOptions { send_to_forks: Forks::Include, ..Default::default() };
/// ```
///
/// **Deliberately not `#[non_exhaustive]`.** That attribute forbids struct-literal construction
/// from other crates entirely, functional update included, which is exactly the form above. Adding
/// a field stays source-compatible for every caller who wrote `..Default::default()`, so the
/// convention is documented rather than enforced — the same choice [`Config`](crate::Config) makes
/// and for the same reason.
/// Whether a message also reaches the workflows forked from its destination.
///
/// A fork is a new workflow replaying an old one's steps, so a message the original was waiting for
/// is one the fork will wait for too — and it was consumed by the original. Including the forks is
/// how a send reaches both.
///
/// The fork set is resolved inside the sending transaction, so a fork created while the send is in
/// flight cannot make it stale.