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 pending traces when the process exits gracefully #207

Merged
merged 1 commit into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions LICENSE-3rdparty.csv
Expand Up @@ -32,6 +32,7 @@ dev,eslint-plugin-import,MIT,Copyright 2015 Ben Mosher
dev,eslint-plugin-node,MIT,Copyright 2015 Toru Nagashima
dev,eslint-plugin-promise,ISC,jden and other contributors
dev,eslint-plugin-standard,MIT,Copyright 2015 Jamund Ferguson
dev,eventemitter3,MIT,Copyright 2014 Arnout Kazemier
dev,express,MIT,Copyright 2009-2014 TJ Holowaychuk 2013-2014 Roman Shtylman 2014-2015 Douglas Christopher Wilson
dev,get-port,MIT,Copyright Sindre Sorhus
dev,graphql,MIT,Copyright 2015-present Facebook Inc.
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -68,6 +68,7 @@
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"eventemitter3": "^3.1.0",
"express": "^4.16.2",
"get-port": "^3.2.0",
"graphql": "^0.13.2",
Expand Down
14 changes: 12 additions & 2 deletions src/platform/node/index.js
@@ -1,5 +1,6 @@
'use strict'

const EventEmitter = require('events')
const id = require('./id')
const now = require('./now')
const env = require('./env')
Expand All @@ -8,7 +9,9 @@ const service = require('./service')
const request = require('./request')
const msgpack = require('./msgpack')

module.exports = {
const emitter = new EventEmitter()

const platform = {
_config: {},
name: () => 'nodejs',
version: () => process.version,
Expand All @@ -22,5 +25,12 @@ module.exports = {
load,
service,
request,
msgpack
msgpack,
on: emitter.on.bind(emitter),
once: emitter.once.bind(emitter),
off: emitter.removeListener.bind(emitter)
}

process.once('beforeExit', () => emitter.emit('exit'))

module.exports = platform
6 changes: 5 additions & 1 deletion src/scheduler.js
@@ -1,6 +1,6 @@
'use strict'

// TODO: flush on process exit
const platform = require('./platform')

class Scheduler {
constructor (callback, interval) {
Expand All @@ -12,10 +12,14 @@ class Scheduler {
start () {
this._timer = setInterval(this._callback, this._interval)
this._timer.unref && this._timer.unref()

platform.on('exit', this._callback)
}

stop () {
clearInterval(this._timer)

platform.off('exit', this._callback)
}

reset () {
Expand Down
32 changes: 31 additions & 1 deletion test/scheduler.spec.js
@@ -1,11 +1,20 @@
'use strict'

const EventEmitter = require('eventemitter3')
const proxyquire = require('proxyquire').noCallThru()

describe('Scheduler', () => {
let Scheduler
let clock
let platform

beforeEach(() => {
Scheduler = require('../src/scheduler')
platform = new EventEmitter()

Scheduler = proxyquire('../src/scheduler', {
'./platform': platform
})

clock = sinon.useFakeTimers()
})

Expand All @@ -27,6 +36,16 @@ describe('Scheduler', () => {

expect(spy).to.have.been.calledTwice
})

it('should call the callback when the process exits gracefully', () => {
const spy = sinon.spy()
const scheduler = new Scheduler(spy, 5000)

scheduler.start()
platform.emit('exit')

expect(spy).to.have.been.called
})
})

describe('stop', () => {
Expand All @@ -40,6 +59,17 @@ describe('Scheduler', () => {

expect(spy).to.not.have.been.called
})

it('should stop calling the callback when the process exits gracefully', () => {
const spy = sinon.spy()
const scheduler = new Scheduler(spy, 5000)

scheduler.start()
scheduler.stop()
platform.emit('exit')

expect(spy).to.not.have.been.called
})
})

describe('reset', () => {
Expand Down