Skip to content

Instantly share code, notes, and snippets.

@itacirgabral
Created December 22, 2020 15:23
Show Gist options
  • Save itacirgabral/84cdfe5e1220d55d9515c2179eeb3eb6 to your computer and use it in GitHub Desktop.
Save itacirgabral/84cdfe5e1220d55d9515c2179eeb3eb6 to your computer and use it in GitHub Desktop.
const Redis = require('ioredis')
const redisConn = process.env.REDIS_CONN
const hardid = process.env.HARDID
const panoptickey = 'zap:panoptic'
const catcherrKey = 'zap:catcherr'
const actbooting = JSON.stringify({ type: 'booting', hardid, timestamp: Date.now() })
const mkactbigerr = ({ err }) => JSON.stringify({ type: 'bigerr', hardid, err, timestamp: Date.now() })
const speaker = new Redis(redisConn)
const listener = new Redis(redisConn) // speaker.duplicate() works too
;(async () => {
let sisyphus = true
while (sisyphus) {
try {
// open the while loop
sisyphus = false
await speaker.publish(panoptickey, actbooting)
let gracefuldownresolver
const gracefuldownpromise = new Promise((resolve, reject) => {
gracefuldownresolver = resolve
})
// screw on the redis
await listener.subscribe(panoptickey)
listener.on('message', (channel, message) => {
const { type, ...leftover } = JSON.parse(message)
switch (type) {
case 'gracefuldown':
if (leftover.hardid === hardid) {
gracefuldownresolver()
}
break
/*
** write your management cases
*/
}
})
await gracefuldownpromise
} catch (err) {
// lock the while loop
sisyphus = true
const actbigerr = mkactbigerr({ err })
const warnlogs = await Promise.all([
speaker.publish(panoptickey, actbigerr),
speaker.lpush(catcherrKey, actbigerr)
])
console.error(warnlogs)
}
}
process.exit(0)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment