Move exit handler to library

This commit is contained in:
tsightler
2022-03-04 23:10:12 -05:00
parent 5688ecf6d6
commit 455139c825
2 changed files with 57 additions and 47 deletions

54
lib/exithandler.js Normal file
View File

@@ -0,0 +1,54 @@
const debug = require('debug')('ring-mqtt')
const colors = require('colors/safe')
const utils = require('./utils')
const ring = require('./ring')
class ExitHandler {
init() {
// Setup Exit Handlers
process.on('exit', this.processExit.bind(null, 0))
process.on('SIGINT', this.processExit.bind(null, 0))
process.on('SIGTERM', this.processExit.bind(null, 0))
process.on('uncaughtException', (err) => {
debug(colors.red('ERROR - Uncaught Exception'))
console.log(colors.red(err))
processExit(2)
})
process.on('unhandledRejection', (err) => {
switch(true) {
// For these strings suppress the stack trace and only print the message
case /token is not valid/.test(err.message):
case /https:\/\/github.com\/dgreif\/ring\/wiki\/Refresh-Tokens/.test(err.message):
case /error: access_denied/.test(err.message):
debug(colors.yellow(err.message))
break;
default:
debug(colors.yellow('WARNING - Unhandled Promise Rejection'))
console.log(colors.yellow(err))
break;
}
})
}
// Set offline status on exit
async processExit(exitCode) {
await utils.sleep(1)
debug('The ring-mqtt process is shutting down...')
await ring.rssShutdown()
if (ring.devices.length > 0) {
debug('Setting all devices offline...')
await utils.sleep(1)
ring.devices.forEach(ringDevice => {
if (ringDevice.availabilityState === 'online') {
ringDevice.shutdown = true
ringDevice.offline()
}
})
}
await utils.sleep(2)
if (exitCode || exitCode === 0) debug(`Exit code: ${exitCode}`);
process.exit()
}
}
module.exports = new ExitHandler()

View File

@@ -1,4 +1,5 @@
#!/usr/bin/env node
const exitHandler = require('./lib/exithandler')
const config = require('./lib/config')
const state = require('./lib/state')
const ring = require('./lib/ring')
@@ -8,55 +9,10 @@ const colors = require('colors/safe')
const utils = require('./lib/utils')
const tokenApp = require('./lib/tokenapp')
// Setup Exit Handlers
process.on('exit', processExit.bind(null, 0))
process.on('SIGINT', processExit.bind(null, 0))
process.on('SIGTERM', processExit.bind(null, 0))
process.on('uncaughtException', function(err) {
debug(colors.red('ERROR - Uncaught Exception'))
console.log(colors.red(err))
processExit(2)
})
process.on('unhandledRejection', function(err) {
switch(true) {
// For these strings suppress the stack trace and only print the message
case /token is not valid/.test(err.message):
case /https:\/\/github.com\/dgreif\/ring\/wiki\/Refresh-Tokens/.test(err.message):
case /error: access_denied/.test(err.message):
debug(colors.yellow(err.message))
break;
default:
debug(colors.yellow('WARNING - Unhandled Promise Rejection'))
console.log(colors.yellow(err))
break;
}
})
// Set offline status on exit
async function processExit(exitCode) {
await utils.sleep(1)
debug('The ring-mqtt process is shutting down...')
await ring.rssShutdown()
if (ring.devices.length > 0) {
debug('Setting all devices offline...')
await utils.sleep(1)
ring.devices.forEach(ringDevice => {
if (ringDevice.availabilityState === 'online') {
ringDevice.shutdown = true
ringDevice.offline()
}
})
}
await utils.sleep(2)
if (exitCode || exitCode === 0) debug(`Exit code: ${exitCode}`);
process.exit()
}
/* End Functions */
// Main code loop
const main = async(generatedToken) => {
if (!state.valid) {
if (!state.valid) {
await exitHandler.init()
await state.init(config)
}