Skip to content

Instantly share code, notes, and snippets.

@alizbazar
Created June 14, 2019 15:23
Show Gist options
  • Save alizbazar/d03f914a6015304bd66477eb7ea58b96 to your computer and use it in GitHub Desktop.
Save alizbazar/d03f914a6015304bd66477eb7ea58b96 to your computer and use it in GitHub Desktop.
Scheduled health check for the server.
// To visualize statechart, copy paste code to https://statecharts.github.io/xstate-viz/
// Available variables:
// Machine (machine factory function)
// assign (action)
// XState (all XState exports)
const healthCheck = Machine({
id: 'healthCheck',
context: { attempts: 0 },
initial: 'normal',
states: {
normal: {
onEntry: 'logOK',
on: {
ERROR: 'escalated',
OK: 'normal',
}
},
escalated: {
onEntry: [
assign({ attempts: 0 }),
'scheduleRecheck',
'logError',
],
on: {
OK: 'normal',
ERROR: [{
cond: 'maxAttempts',
target: 'restarting',
}, {
actions: [
assign({
attempts: ctx => ctx.attempts + 1
}),
'scheduleRecheck'
],
}]
},
},
restarting: {
onEntry: ['restart', 'logRestart'],
on: {
OK: 'normal',
},
},
}
}, {
guards: {
maxAttempts: ctx => ctx.attempts >= 2
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment