mirror of
https://github.com/openstf/stf
synced 2025-10-04 18:29:17 +02:00
Auto-end shared lifecycles on exit.
This commit is contained in:
parent
4b155a3850
commit
4d5319b555
1 changed files with 18 additions and 4 deletions
|
@ -6,6 +6,7 @@ var _ = require('lodash')
|
||||||
|
|
||||||
function Lifecycle() {
|
function Lifecycle() {
|
||||||
this.observers = []
|
this.observers = []
|
||||||
|
this.ending = false
|
||||||
process.on('SIGINT', this.graceful.bind(this))
|
process.on('SIGINT', this.graceful.bind(this))
|
||||||
process.on('SIGTERM', this.graceful.bind(this))
|
process.on('SIGTERM', this.graceful.bind(this))
|
||||||
}
|
}
|
||||||
|
@ -20,24 +21,36 @@ Lifecycle.prototype.share = function(name, emitter, options) {
|
||||||
|
|
||||||
if (opts.end) {
|
if (opts.end) {
|
||||||
emitter.on('end', function() {
|
emitter.on('end', function() {
|
||||||
|
if (!this.ending) {
|
||||||
log.fatal('%s ended; we shall share its fate', name)
|
log.fatal('%s ended; we shall share its fate', name)
|
||||||
this.fatal()
|
this.fatal()
|
||||||
|
}
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
if (opts.error) {
|
if (opts.error) {
|
||||||
emitter.on('error', function(err) {
|
emitter.on('error', function(err) {
|
||||||
|
if (!this.ending) {
|
||||||
log.fatal('%s had an error', name, err.stack)
|
log.fatal('%s had an error', name, err.stack)
|
||||||
this.fatal()
|
this.fatal()
|
||||||
|
}
|
||||||
}.bind(this))
|
}.bind(this))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (emitter.end) {
|
||||||
|
this.observe(function() {
|
||||||
|
emitter.end()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return emitter
|
return emitter
|
||||||
}
|
}
|
||||||
|
|
||||||
Lifecycle.prototype.graceful = function() {
|
Lifecycle.prototype.graceful = function() {
|
||||||
log.info('Winding down for graceful exit')
|
log.info('Winding down for graceful exit')
|
||||||
|
|
||||||
|
this.ending = true
|
||||||
|
|
||||||
var wait = Promise.all(this.observers.map(function(fn) {
|
var wait = Promise.all(this.observers.map(function(fn) {
|
||||||
return fn()
|
return fn()
|
||||||
}))
|
}))
|
||||||
|
@ -49,6 +62,7 @@ Lifecycle.prototype.graceful = function() {
|
||||||
|
|
||||||
Lifecycle.prototype.fatal = function() {
|
Lifecycle.prototype.fatal = function() {
|
||||||
log.fatal('Shutting down due to fatal error')
|
log.fatal('Shutting down due to fatal error')
|
||||||
|
this.ending = true
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue