Created
June 14, 2019 15:23
-
-
Save alizbazar/d03f914a6015304bd66477eb7ea58b96 to your computer and use it in GitHub Desktop.
Scheduled health check for the server.
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
// 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