mirror of
https://github.com/tsightler/ring-mqtt.git
synced 2025-11-02 12:54:04 +08:00
Process exit updates
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs')
|
||||||
|
|
||||||
class Utils
|
class Utils
|
||||||
{
|
{
|
||||||
|
|
||||||
// Sleep function (seconds)
|
// Sleep function (seconds)
|
||||||
sleep(sec) {
|
sleep(sec) {
|
||||||
return this.msleep(sec*1000)
|
return this.msleep(sec*1000)
|
||||||
|
|||||||
32
ring-mqtt.js
32
ring-mqtt.js
@@ -40,28 +40,36 @@ var republishCount = 6 // Republish config/state this many times after startup o
|
|||||||
var republishDelay = 30 // Seconds
|
var republishDelay = 30 // Seconds
|
||||||
|
|
||||||
// Setup Exit Handwlers
|
// Setup Exit Handwlers
|
||||||
process.on('exit', processExit.bind(0))
|
process.on('exit', processExit.bind(null, 0))
|
||||||
process.on('SIGINT', processExit.bind(0))
|
process.on('SIGINT', processExit.bind(null, 0))
|
||||||
process.on('SIGTERM', processExit.bind(0))
|
process.on('SIGTERM', processExit.bind(null, 0))
|
||||||
process.on('uncaughtException', processExit.bind(1))
|
process.on('uncaughtException', processExit.bind(null, 2))
|
||||||
process.on('unhandledRejection', function(err) {
|
process.on('unhandledRejection', function(err) {
|
||||||
if (err.message.match('token is not valid')) {
|
if (err.message.match('token is not valid')) {
|
||||||
debug(err.message);
|
debug(colors.yellow(err.message))
|
||||||
} else {
|
} else {
|
||||||
debug(err);
|
debug(colors.yellow('WARNING - Unhandled Promise Rejection:'))
|
||||||
|
debug(colors.yellow(err))
|
||||||
|
processExit(1)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
|
|
||||||
// Set offline status on exit
|
// Set offline status on exit
|
||||||
async function processExit(exitCode) {
|
async function processExit(exitCode) {
|
||||||
await ringDevices.forEach(async ringDevice => {
|
await utils.sleep(1)
|
||||||
|
debug('The ring-mqtt process is shutting down...')
|
||||||
|
if (ringDevices.length > 0) {
|
||||||
|
debug('Setting all devices offline...')
|
||||||
|
await utils.sleep(1)
|
||||||
|
ringDevices.forEach(ringDevice => {
|
||||||
if (ringDevice.availabilityState === 'online') {
|
if (ringDevice.availabilityState === 'online') {
|
||||||
ringDevice.shutdown = true
|
ringDevice.shutdown = true
|
||||||
await ringDevice.offline()
|
ringDevice.offline()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
await utils.sleep(3)
|
}
|
||||||
if (exitCode || exitCode === 0) debug('Exit code: '+exitCode)
|
await utils.sleep(2)
|
||||||
|
if (exitCode || exitCode === 0) debug(`Exit code: ${exitCode}`);
|
||||||
process.exit()
|
process.exit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user