Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush on exit #1185

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,35 @@ function buildStream (filename, workerData, workerOpts) {
workerOpts
})

stream.on('ready', function () {
stream.on('ready', onReady)
stream.on('close', function () {
process.removeListener('exit', onExit)
})

process.on('exit', onExit)

function onReady () {
process.removeListener('exit', onExit)
stream.unref()

if (workerOpts.autoEnd !== false) {
setupOnExit(stream)
}
})
}

function onExit () {
if (stream.closed) {
return
}
stream.flushSync()
}

return stream
}

function autoEnd (stream) {
stream.ref()
stream.flushSync()
stream.end()
stream.once('close', function () {
stream.unref()
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/transport-exit-immediately.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict'

const pino = require('../..')
const transport = pino.transport({
target: 'pino/file'
})
const logger = pino(transport)

logger.info('Hello')
process.exit(0)
12 changes: 12 additions & 0 deletions test/fixtures/transport-exit-on-ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const pino = require('../..')
const transport = pino.transport({
target: 'pino/file'
})
const logger = pino(transport)

transport.on('ready', function () {
logger.info('Hello')
process.exit(0)
})
24 changes: 24 additions & 0 deletions test/transport/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,30 @@ test('stdout in worker', async ({ not }) => {
not(strip(actual).match(/Hello/), null)
})

test('log and exit on ready', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-on-ready.js')])

child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/Hello/), null)
})

test('log and exit before ready', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, '..', 'fixtures', 'transport-exit-immediately.js')])

child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/Hello/), null)
})

test('pino transport options with target', async ({ teardown, same }) => {
const destination = join(
os.tmpdir(),
Expand Down