Provider should now shut down gracefully.

This commit is contained in:
Simo Kinnunen
2014-04-12 16:56:12 +09:00
parent bd2b43901d
commit 4ba11eca61

View File

@@ -10,6 +10,7 @@ var wire = require('../wire')
var wireutil = require('../wire/util')
var wirerouter = require('../wire/router')
var procutil = require('../util/procutil')
var lifecycle = require('../util/lifecycle')
module.exports = function(options) {
var log = logger.createLogger('provider')
@@ -21,11 +22,10 @@ module.exports = function(options) {
, ready: []
, waiting: []
}
var totalsTimer
// Information about total devices
var delayedTotals = (function() {
var timer
function totals() {
if (lists.waiting.length) {
log.info(
@@ -54,8 +54,8 @@ module.exports = function(options) {
}
return function() {
clearTimeout(timer)
timer = setTimeout(totals, 10000)
clearTimeout(totalsTimer)
totalsTimer = setTimeout(totals, 10000)
}
})()
@@ -353,41 +353,23 @@ module.exports = function(options) {
flippedTracker.emit(device.id, 'remove', device)
}))
tracker.on('error', function(err) {
log.fatal('Tracker had an error:', err.stack)
process.exit(1)
})
tracker.on('end', function() {
log.fatal('Tracker ended')
process.exit(1)
})
sub.on('message', wirerouter()
.on(wire.DeviceRegisteredMessage, function(channel, message) {
flippedTracker.emit(message.serial, 'register')
})
.handler())
lifecycle.share('Tracker', tracker)
})
function gracefullyExit() {
log.info('Stopping all workers')
Promise.all(Object.keys(workers).map(function(serial) {
return workers[serial].cancel()
}))
.done(function() {
log.info('All cleaned up')
process.exit(0)
})
}
lifecycle.observe(function() {
clearTimeout(totalsTimer)
process.on('SIGINT', function() {
log.info('Received SIGINT')
gracefullyExit()
})
push.close()
sub.close()
process.on('SIGTERM', function() {
log.info('Received SIGTERM')
gracefullyExit()
return Promise.all(Object.keys(workers).map(function(serial) {
return workers[serial].cancel()
}))
})
}