-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve typescript examples for basics and sagas
- Loading branch information
Showing
9 changed files
with
90 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
71 changes: 71 additions & 0 deletions
71
typescript/patterns-use-cases/microservices-sagas/src/booking_workflow.ts
This file contains 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,71 @@ | ||
/* | ||
* Copyright (c) 2024 - Restate Software, Inc., Restate GmbH | ||
* | ||
* This file is part of the Restate examples, | ||
* which is released under the MIT license. | ||
* | ||
* You can find a copy of the license in the file LICENSE | ||
* in the root directory of this repository or package or at | ||
* https://github.com/restatedev/examples/ | ||
*/ | ||
|
||
import * as restate from "@restatedev/restate-sdk"; | ||
|
||
import { flights } from "./services/flights"; | ||
import { cars } from "./services/cars"; | ||
import { payments } from "./services/payments"; | ||
|
||
// | ||
// An example of a trip reservation workflow, using the SAGAs pattern to | ||
// undo previous steps in case of an error. | ||
// | ||
// The durable execution's guarantee to run code to the end in the presence | ||
// of failures, and to deterministically recover previous steps from the | ||
// journal, makes SAGAs easy. | ||
// Every step pushes a compensation action (an undo operation) to a stack. | ||
// in the case of an error, those operations are run. | ||
// | ||
// The main requirement is that steps are implemented as journald | ||
// operations, like `ctx.run()` or rpc/messaging. | ||
restate.endpoint().bind( | ||
restate.workflow({ | ||
name: "BookingWorkflow", | ||
handlers: { | ||
run: async (ctx: restate.WorkflowContext, tripID: string) => { | ||
// create a list of undo actions | ||
const compensations = []; | ||
try { | ||
|
||
// call the flight API to reserve, keeping track of how to cancel | ||
const flightBooking = await ctx.run(() => flights.reserve(tripID)); | ||
compensations.push(() => flights.cancel(tripID, flightBooking)); | ||
|
||
// call the car rental service API to reserve, keeping track of how to cancel | ||
const carBooking = await ctx.run(() => cars.reserve(tripID)); | ||
compensations.push(() => cars.cancel(tripID, carBooking)); | ||
|
||
// call the payments API, keeping track of how to refund | ||
const paymentId = ctx.rand.uuidv4(); | ||
compensations.push(() => payments.refund({ paymentId })); | ||
await ctx.run(() => payments.process({ tripID })); | ||
|
||
// confirm the flight and car reservations | ||
await flights.confirm(tripID, flightBooking); | ||
await cars.confirm(tripID, carBooking); | ||
|
||
} catch (e) { | ||
|
||
if (e instanceof restate.TerminalError) { | ||
// undo all the steps up to this point by running the compensations | ||
for (const compensation of compensations.reverse()) { | ||
await compensation(); | ||
} | ||
} | ||
|
||
throw e; | ||
} | ||
} | ||
}, | ||
}) | ||
).listen(9080) | ||
|
This file contains 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
75 changes: 0 additions & 75 deletions
75
typescript/patterns-use-cases/microservices-sagas/src/workflow_saga.ts
This file was deleted.
Oops, something went wrong.