Replace device:support:quit with util:lifecycle, which is usable by non-device processes too.

This commit is contained in:
Simo Kinnunen
2014-04-01 21:41:58 +09:00
parent e17f306d30
commit d6604bcda8
11 changed files with 75 additions and 179 deletions

47
lib/util/lifecycle.js Normal file
View File

@@ -0,0 +1,47 @@
var Promise = require('bluebird')
var logger = require('./logger')
var log = logger.createLogger('util:lifecycle')
function Lifecycle() {
this.observers = []
process.on('SIGINT', this.graceful.bind(this))
process.on('SIGTERM', this.graceful.bind(this))
}
Lifecycle.prototype.share = function(name, emitter) {
emitter.on('end', function() {
log.fatal('%s ended; we shall share its fate', name)
this.fatal()
}.bind(this))
emitter.on('error', function(err) {
log.fatal('%s had an error', name, err.stack)
this.fatal()
}.bind(this))
return emitter
}
Lifecycle.prototype.graceful = function() {
log.info('Winding down for graceful exit')
var wait = Promise.all(this.observers.map(function(fn) {
return fn()
}))
return wait.then(function() {
process.exit(0)
})
}
Lifecycle.prototype.fatal = function() {
log.fatal('Shutting down due to fatal error')
process.exit(1)
}
Lifecycle.prototype.observe = function(promise) {
this.observers.push(promise)
}
module.exports = new Lifecycle()

View File

@@ -1,24 +0,0 @@
var events = require('events')
var util = require('util')
function Vitals() {
events.EventEmitter.call(this)
}
util.inherits(Vitals, events.EventEmitter)
Vitals.prototype.register = function(name, stream) {
var that = this
stream.on('end', function() {
that.emit('end', name)
})
stream.on('error', function(err) {
that.emit('error', name, err)
})
return stream
}
module.exports = Vitals