Skip to content

Commit

Permalink
Update to 0.8.1 and improve readme
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkleeman committed Sep 5, 2024
1 parent 5af6621 commit 1455adc
Show file tree
Hide file tree
Showing 6 changed files with 447 additions and 243 deletions.
48 changes: 44 additions & 4 deletions patterns-use-cases/xstate/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,46 @@
# Deploying a Restate Typescript greeter on AWS Lambda
# Deploying a XState state machine on Restate

Restate is a system for easily building resilient applications using **distributed durable RPC & async/await**.
This example shows how to integrate Restate deeply with
[XState](https://stately.ai/docs/xstate). The code in [lib.ts](./lib.ts) and
[promise.ts](./promise.ts) converts an XState machine into two Restate
services:

This example contains the greeter service which you can deploy on AWS Lambda.
Take a look at [how to deploy Restate services on AWS Lambda](https://docs.restate.dev/services/deployment/lambda#tutorial) for more information.
1. A keyed service, which stores the state of the state machine, keyed on an
identifier for this instance of the machine. This service is called with
every event that must be processed by the state machine. XState machines are
generally pure and are not async; side effects generally happen through
[Promise Actors](https://stately.ai/docs/promise-actors). As such, this
service should never block the machine, so other events can always be
processed.
2. An unkeyed service, which exists solely to execute Promise Actors and call
back to the state machine with their result. As this is an unkeyed service,
the Promise won't hold up any other events. This service doesn't need to be
called by you directly.

Both services are set up and managed automatically by interpreting the state
machine definition, and should generally be deployed together, whether as a
Lambda or as a long-lived service.

In `app.ts` you will see an example of an XState machine that uses cross-machine
communication, delays, and Promise actors, all running in Restate. However,
most XState machines should work out of the box; this is still experimental, so
we haven't tested everything yet!

To try out this example:

```bash
# start a local Restate instance
docker run -d -it --network=host --name restate_dev --rm restatedev/restate:0.8.1
# start the service
npm run dev
# register the state machine service against restate
npx @restatedev/[email protected] dep register http://localhost:9080

# create a state machine
curl http://localhost:8080/auth/create --json '{"key": "myMachine"}'
# watch the state
watch -n1 'curl -s http://localhost:8080/auth/snapshot --json "{\"key\": \"myMachine\"}"'
# kick off the machine
curl http://localhost:8080/auth/send --json '{"key": "myMachine", "request": {"event": {"type": "AUTH"}}}'
# and watch the auth flow progress!
```
17 changes: 9 additions & 8 deletions patterns-use-cases/xstate/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion patterns-use-cases/xstate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
"dev": "ts-node-dev --respawn --transpile-only ./src/app.ts"
},
"dependencies": {
"@restatedev/restate-sdk": "^0.5.1",
"@restatedev/restate-sdk": "^0.8.1",
"xstate": "^5.2.1"
},
"devDependencies": {
"@types/node": "^20.12.10",
"@typescript-eslint/eslint-plugin": "^6.3.0",
"@typescript-eslint/parser": "^6.3.0",
"esbuild": "^0.19.0",
Expand Down
81 changes: 42 additions & 39 deletions patterns-use-cases/xstate/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,69 +11,72 @@

import * as restate from "@restatedev/restate-sdk";

import {createMachine, sendTo} from 'xstate';
import {bindXStateRouter} from "./lib";
import {fromPromise} from "./promise";
import { createMachine, sendTo } from "xstate";
import { bindXStateRouter } from "./lib";
import { fromPromise } from "./promise";

const authServerMachine = createMachine({
id: 'server',
initial: 'waitingForCode',
states: {
waitingForCode: {
on: {
CODE: {
target: "process"
const authServerMachine = createMachine(
{
id: "server",
initial: "waitingForCode",
states: {
waitingForCode: {
on: {
CODE: {
target: "process",
},
},
},
},
process: {
invoke: {
id: 'process',
src: 'authorise',
onDone: {
actions: sendTo(
({self}) => self._parent!,
{ type: 'TOKEN' },
{ delay: 1000 },
),
process: {
invoke: {
id: "process",
src: "authorise",
onDone: {
actions: sendTo(
({ self }) => self._parent!,
{ type: "TOKEN" },
{ delay: 1000 }
),
},
},
}
},
},
},
{
actors: {
authorise: fromPromise(
() => new Promise((resolve) => setTimeout(resolve, 5000))
),
},
}
}, {
actors: {
authorise: fromPromise(() => new Promise((resolve) => setTimeout(resolve, 5000))),
}
});
);

const authClientMachine = createMachine({
id: 'client',
initial: 'idle',
id: "client",
initial: "idle",
states: {
idle: {
on: {
AUTH: {target: 'authorizing'},
AUTH: { target: "authorizing" },
},
},
authorizing: {
invoke: {
id: 'auth-server',
id: "auth-server",
src: authServerMachine,
},
entry: sendTo('auth-server', ({self}) => ({
type: 'CODE',
entry: sendTo("auth-server", ({ self }) => ({
type: "CODE",
sender: self,
})),
on: {
TOKEN: {target: 'authorized'},
TOKEN: { target: "authorized" },
},
},
authorized: {
type: 'final',
type: "final",
},
},
});


bindXStateRouter(restate.createServer(), "foo", authClientMachine).listen()

bindXStateRouter(restate.endpoint(), "auth", authClientMachine).listen();
Loading

0 comments on commit 1455adc

Please sign in to comment.