Skip to content
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

add deep persistence example #4102

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions examples/deep-persistence-with-restore/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
64 changes: 64 additions & 0 deletions examples/deep-persistence-with-restore/actors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fromPromise } from 'xstate';
import { RunServiceMachine } from './machines';

export async function delay(
ms: number,
errorProbability: number = 0
): Promise<void> {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (Math.random() < errorProbability) {
reject({ type: 'ServiceNotAvailable' });
} else {
resolve();
}
}, ms);
});
}

export function createActors(errorProbability = 0.6) {
const services: Record<string, any> = {
'up-step1-system1': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'up-step2-system1': fromPromise(async () => {
const resp = await delay(1000, errorProbability);
return resp;
}),
'up-step3-system2': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'up-step4-system1': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'up-step5-system3': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'down-step1-system1': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'down-step2-system2': fromPromise(async () => {
const resp = await delay(1000);
return resp;
}),
'down-step3-system3': fromPromise(async () => {
const resp = await delay(1000);
return resp;
})
};

const actors: Record<string, any> = {};
Object.keys(services).forEach(
(key) =>
(actors[key] = RunServiceMachine.provide({
actors: { upDownService: services[key] }
}))
);

return actors;
}
166 changes: 166 additions & 0 deletions examples/deep-persistence-with-restore/historyStates.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
[
{
"status": "active",
"value": { "Up": "Done" },
"historyValue": {},
"context": {},
"children": {}
},
{
"status": "active",
"value": { "Up": "Step5 system3" },
"historyValue": {},
"context": {},
"children": {
"up-step5-system3": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step5-system3",
"syncSnapshot": false
}
}
},
{
"status": "active",
"value": { "Up": "Step4 System1" },
"historyValue": {},
"context": {},
"children": {
"up-step4-system1": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step4-system1",
"syncSnapshot": false
}
}
},
{
"status": "active",
"value": { "Up": "Step3 system2" },
"historyValue": {},
"context": {},
"children": {
"up-step3-system2": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step3-system2",
"syncSnapshot": false
}
}
},
{
"status": "active",
"value": { "Up": "Step2 system1" },
"historyValue": {},
"context": {},
"children": {
"up-step2-system1": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step2-system1",
"syncSnapshot": false
}
}
},
{
"status": "done",
"value": "NotDone",
"historyValue": {},
"context": {},
"children": {}
},
{
"status": "active",
"value": { "Up": "Step2 system1" },
"historyValue": {},
"context": {},
"children": {
"up-step2-system1": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step2-system1",
"syncSnapshot": false
}
}
},
{
"status": "active",
"value": { "Up": "Step1 system1" },
"historyValue": {},
"context": {},
"children": {
"up-step1-system1": {
"snapshot": {
"status": "active",
"value": "running",
"historyValue": {},
"context": { "errorCount": 0, "threshold": 3 },
"children": {
"upDownService": {
"snapshot": { "status": "active" },
"src": "upDownService",
"syncSnapshot": false
}
}
},
"src": "up-step1-system1",
"syncSnapshot": false
}
}
}
]
86 changes: 86 additions & 0 deletions examples/deep-persistence-with-restore/machines/RunningServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { assign, createMachine, sendParent } from 'xstate';

type Context = {
errorCount: number;
threshold: number;
};

type Events = { type: 'Retry' };

export const RunServiceMachine = createMachine(
{
id: 'Run Service',
initial: 'running',
types: {} as {
context: Context;
events: Events;
},
context: ({ input }) => {
return {
errorCount: 0,
threshold: input?.threshold ? input.threshold : 3
};
},
states: {
done: {
entry: ['logDone'],
type: 'final'
},
error: {
entry: ['logError', 'updateCounter'],
after: {
WaitBeforeRetry: [
{ guard: 'exceedsThreshold', target: 'notDone' },
{ target: 'running' }
]
},
on: {
// Manual retry
Retry: {
target: 'running'
}
}
},
running: {
entry: ['init'],
invoke: {
src: 'upDownService',
id: 'upDownService',
onDone: [
{
target: 'done'
}
],
onError: [
{
target: 'error'
}
]
}
},
notDone: {
entry: ['logNotDone', sendParent({ type: 'NOT_DONE' })],
type: 'final'
}
}
},
{
delays: {
WaitBeforeRetry: 3000
},
actions: {
logNotDone: ({ self }) => console.log('---rs: notDone', self.id),
logDone: ({ self }) => console.log('---rs: done', self.id),
logError: ({ self }) => console.log('---rs: error', self.id),
logRunning: ({ self }) => console.log('---rs: init', self.id),
updateCounter: assign({
errorCount: ({ context }) => context.errorCount + 1
})
},
guards: {
exceedsThreshold: ({ context }) => {
return context.errorCount > 3;
}
}
}
);
Loading