Created
June 14, 2019 15:42
-
-
Save alizbazar/db4df94825bfff22686d96ea67f0bc10 to your computer and use it in GitHub Desktop.
Scheduled health check for a 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
/* | |
The idea is that the machine is used by a single cloud function / endpoint, which is called either periodically or based on | |
- Health check using periodic process, such as Google Cloud Scheduler or cron | |
- On failure, schedule additional checks, for ex. using Google Cloud Tasks | |
- If failures continue, restart the system | |
- Go to normal mode of operation if all good | |
*/ | |
// 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