UNPKG

1.29 kBJavaScriptView Raw
1/**
2 * @constructs Express middleware to serve the health of your dependencies
3 * @param {Copacetic} copacetic
4 * @param {Integer} [interval] How regular services will be checked
5 * @param {Array<Object>} [dependencies] An explicit set of dependencies to be polled
6 * @param {Boolean} [parallel=true] Check in sequence, or parallel
7 * @param {String} [schedule=start] Schedule the next check as soon as a poll starts, or wait
8 * until it ends
9 */
10module.exports = ({ copacetic, interval, dependencies, parallel, schedule, verbose = true }) => {
11 if (interval) {
12 if (dependencies) {
13 copacetic.poll({ dependencies, interval, parallel, schedule })
14 } else {
15 copacetic.pollAll({ interval, parallel, schedule })
16 }
17 }
18
19 if (verbose) {
20 return (req, res) => {
21 /**
22 * Health information about your service
23 * @typedef {Object} Copacetic~HealthResponse
24 * @property {String} Name - the name of your service
25 * @property {Boolean} Healthy
26 * @property {Array<HealthInfo>}
27 */
28 return res.json({
29 name: copacetic.name || 'service',
30 healthy: copacetic.isHealthy,
31 dependencies: copacetic.healthInfo
32 })
33 }
34 }
35
36 return (req, res) => {
37 return res.sendStatus(copacetic.isHealthy ? 200 : 503)
38 }
39}