Skip to content

Commit

Permalink
Only listen to all events if it is an extreme mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed May 10, 2018
1 parent 06872ae commit 91792c7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
35 changes: 25 additions & 10 deletions lib/events.js
@@ -1,10 +1,18 @@
'use strict'

const SonicBoom = require('sonic-boom')

module.exports = function (pinoInstance, internalExtremeHandler) {
function isExtreme () {
return pinoInstance.stream instanceof SonicBoom && pinoInstance.stream.minLength > 0
}

function theWorldIsBurning (err) {
runInternalHandler()
handlers.handledOnTerminate = true
pinoInstance.onTerminated(this.name, err)
if (isExtreme()) {
pinoInstance.onTerminated(this.name, err)
}
}

function hup () {
Expand All @@ -19,19 +27,26 @@ module.exports = function (pinoInstance, internalExtremeHandler) {
internalExtremeHandler()
}

process.on('beforeExit', wrap('beforeExit'))
process.on('exit', wrap('exit'))

var handlers = {
beforeExit: theWorldIsBurning.bind({name: 'beforeExit'}),
exit: theWorldIsBurning.bind({name: 'exit'}),
uncaughtException: theWorldIsBurning.bind({name: 'uncaughtException'}),
uncaughtException: wrap('uncaughtException'),
SIGHUP: hup,
SIGINT: theWorldIsBurning.bind({name: 'SIGINT'}),
SIGQUIT: theWorldIsBurning.bind({name: 'SIGQUIT'}),
SIGTERM: theWorldIsBurning.bind({name: 'SIGTERM'})
SIGINT: wrap('SIGINT'),
SIGQUIT: wrap('SIGQUIT'),
SIGTERM: wrap('SIGTERM')
}

Object.keys(handlers).forEach(function (k) {
process.on(k, handlers[k])
})
if (isExtreme()) {
Object.keys(handlers).forEach(function (k) {
process.on(k, handlers[k])
})
}

return handlers

function wrap (name) {
return theWorldIsBurning.bind({ name })
}
}
20 changes: 9 additions & 11 deletions test/events.test.js
Expand Up @@ -18,7 +18,7 @@ test('no event loop logs successfully', function (t) {
}))

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(null, output.match(/"msg":"h"/))
})
})

Expand All @@ -39,9 +39,9 @@ test('terminates when uncaughtException is fired with onTerminate registered', f
}))

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(output.match(/terminated/g), null)
t.notEqual(errorOutput.match(/this is not caught/g), null)
t.notEqual(null, output.match(/"msg":"h"/))
t.notEqual(null, output.match(/terminated/g))
t.notEqual(null, errorOutput.match(/this is not caught/g))
})
})

Expand All @@ -60,7 +60,7 @@ test('terminates when uncaughtException is fired without onTerminate registered'
})

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(null, output.match(/"msg":"h"/))
})
})

Expand All @@ -81,7 +81,7 @@ test('terminates on SIGHUP when no other handlers registered', function (t) {
})

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(null, output.match(/"msg":"h"/))
})

setTimeout(function () { child.kill('SIGHUP') }, 2000)
Expand All @@ -102,8 +102,8 @@ test('lets app terminate when SIGHUP received with multiple handlers', function
})

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(output.match(/app sighup/), null)
t.notEqual(null, output.match(/"msg":"h"/))
t.notEqual(null, output.match(/app sighup/))
})

setTimeout(function () { child.kill('SIGHUP') }, 2000)
Expand All @@ -126,8 +126,6 @@ test('destination', function (t) {
})

child.on('close', function () {
t.notEqual(output.match(/"msg":"h"/), null)
t.notEqual(null, output.match(/"msg":"h"/))
})

setTimeout(function () { child.kill('SIGHUP') }, 2000)
})
10 changes: 0 additions & 10 deletions test/fixtures/events/destination.js
Expand Up @@ -5,18 +5,8 @@ global.process = { __proto__: process, pid: 123456 }
Date.now = function () { return 1459875739796 }
require('os').hostname = function () { return 'abcdefghijklmnopqr' }

if (process.listenerCount('SIGHUP') > 0) {
// needed because of a hook added by code coverage
process.removeAllListeners('SIGHUP')
}

var pino = require(require.resolve('./../../../'))

// use a destination
var log = pino(pino.destination())
log.info('h')

function foo () {
setTimeout(foo, 500)
}
foo()

0 comments on commit 91792c7

Please sign in to comment.