Repository navigation
feat(worker): context.saga() — compensating undos, machinery failures exempt
#418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| --- | ||
| "@temporal-contract/worker": minor | ||
| --- | ||
|
|
||
| `context.saga()`: steps with compensating undos, unwound LIFO — with the | ||
| machinery failures exempt by default. | ||
|
|
||
| `declareWorkflow` handed a workflow `context.activities` and `context.errors` | ||
| and nothing for the walk-back, so every saga wrote its own. The LIFO machinery | ||
| is now `@unthrown/saga`'s. What this adds is the decision that belongs to | ||
| Temporal rather than to a `Result` combinator: **which failures compensate.** | ||
|
|
||
| ```ts | ||
| const fulfilled = await context | ||
| .saga() | ||
| .step( | ||
| () => context.activities.reserveStock(order), | ||
| (reservation) => context.activities.releaseStock({ id: reservation.id }), | ||
| ) | ||
| .step( | ||
| () => context.activities.chargeCard(order), | ||
| (charge) => context.activities.refund({ id: charge.id }), | ||
| ) | ||
| .step(() => context.activities.ship(order)) | ||
| .run(); | ||
| ``` | ||
|
|
||
| The undos run on a **declared contract error** — a permanent domain answer, | ||
| where what the step did before saying no is knowable. They do **not** run on an | ||
| `ActivityError`, a `ChildWorkflowError` or a defect: a step that failed | ||
| unmodelled left state nobody can see, and un-deciding what you cannot see is a | ||
| second bug. That failure propagates untouched, so `propagateActivityFailure` | ||
| still re-raises Temporal's original failure — which deletes the per-step | ||
|
|
||
| ```ts | ||
| .with(P.tag(ACTIVITY_ERROR_TAG), P.tag(ACTIVITY_CANCELLED_ERROR_TAG), (error) => ErrAsync(error)) | ||
| ``` | ||
|
|
||
| arm that had to be repeated, was easy to omit, and was invisible when omitted. | ||
|
|
||
| Cancellation is the one case a caller may opt back in to, with | ||
| `saga({ compensateOnCancellation: true })`. Every undo runs inside a | ||
| non-cancellable scope: a cancelled scope schedules no activity at all, so | ||
| without one that opt-in could never compensate for the failure it exists for. | ||
| A compensation that itself fails | ||
| becomes a defect carrying its own failure, which outranks the failure that | ||
| triggered the unwind — a refund that never happened is worse news than the order | ||
| that could not ship — and the remaining undos still run first. | ||
|
|
||
| `workflowSaga` is the same function, exported from | ||
| `@temporal-contract/worker/workflow` for a workflow that composes its steps in a | ||
| helper. | ||
|
|
||
| Closes #413. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import { defineActivity, defineContract, defineWorkflow } from "@temporal-contract/contract"; | ||
| import { z } from "zod"; | ||
|
|
||
| /** | ||
| * A three-step fulfilment whose last step fails, plus the two activities that | ||
| * take the first two back. `mode` chooses how the last step fails, which is | ||
| * the only variable the saga's policy reads. | ||
| */ | ||
| const reserve = defineActivity({ | ||
| input: z.object({}), | ||
| output: z.object({ reservationId: z.string() }), | ||
| activityOptions: { retry: { maximumAttempts: 1 } }, | ||
| }); | ||
|
|
||
| const charge = defineActivity({ | ||
| input: z.object({ sleepMs: z.number() }), | ||
| output: z.object({ chargeId: z.string() }), | ||
| // Temporal delivers a cancellation notification only in the response to a | ||
| // heartbeat RPC, so `cancelled` is unobservable without this. | ||
| activityOptions: { heartbeatTimeout: "2 seconds", retry: { maximumAttempts: 1 } }, | ||
| }); | ||
|
|
||
| /** | ||
| * `declared` answers the contract error the walk-back exists for; `unmodelled` | ||
| * fails as an `ActivityError`, which must leave the earlier steps standing. | ||
| */ | ||
| const ship = defineActivity({ | ||
| input: z.object({ mode: z.enum(["declared", "unmodelled"]) }), | ||
| output: z.object({ shipmentId: z.string() }), | ||
| errors: { | ||
| OutOfStock: { data: z.object({ sku: z.string() }), nonRetryable: true }, | ||
| }, | ||
| activityOptions: { retry: { maximumAttempts: 1 } }, | ||
| }); | ||
|
|
||
| const release = defineActivity({ | ||
| input: z.object({}), | ||
| output: z.object({}), | ||
| activityOptions: { retry: { maximumAttempts: 1 } }, | ||
| }); | ||
|
|
||
| const refund = defineActivity({ | ||
| input: z.object({}), | ||
| output: z.object({}), | ||
| activityOptions: { retry: { maximumAttempts: 1 } }, | ||
| }); | ||
|
|
||
| const fulfil = defineWorkflow({ | ||
| input: z.object({ mode: z.enum(["declared", "unmodelled"]) }), | ||
| output: z.object({ failedWith: z.string() }), | ||
| idempotency: "allow-duplicate", | ||
| activities: { ship, refund }, | ||
| }); | ||
|
|
||
| /** | ||
| * The `compensateOnCancellation` branch: step two blocks until the workflow | ||
| * is cancelled, and the undo of step one must still run — which it can only | ||
| * do from a non-cancellable scope, since a cancelled scope schedules nothing. | ||
| */ | ||
| const fulfilUntilCancelled = defineWorkflow({ | ||
| input: z.object({}), | ||
| output: z.object({ failedWith: z.string() }), | ||
| idempotency: "allow-duplicate", | ||
| activities: {}, | ||
| }); | ||
|
|
||
| export const sagaContract = defineContract({ | ||
| taskQueue: "saga-tests", | ||
| // `reserve`, `charge` and `release` are global: both workflows use them, and | ||
| // activities share one flat namespace at runtime, so a per-workflow copy | ||
| // would be two implementations of one name. | ||
| activities: { reserve, charge, release }, | ||
| workflows: { fulfil, fulfilUntilCancelled }, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.