add a close() method to terminate the module cleanly

closes the statsd connection and cancels registered interval timers
prevents express from hanging when trying to shutdown
This commit is contained in:
Brian Gough 2015-01-05 16:45:32 +00:00
parent 60857982b6
commit 30070f23b8
2 changed files with 13 additions and 1 deletions

View file

@ -6,10 +6,15 @@ hostname = require('os').hostname()
buildKey = (key)-> "#{name}.#{hostname}.#{key}" buildKey = (key)-> "#{name}.#{hostname}.#{key}"
destructors = []
module.exports = module.exports =
initialize: (_name) -> initialize: (_name) ->
name = _name name = _name
registerDestructor: (func) ->
destructors.push func
set : (key, value, sampleRate = 1)-> set : (key, value, sampleRate = 1)->
statsd.set buildKey(key), value, sampleRate statsd.set buildKey(key), value, sampleRate
@ -35,3 +40,7 @@ module.exports =
http: require "./http" http: require "./http"
open_sockets: require "./open_sockets" open_sockets: require "./open_sockets"
close: () ->
for func in destructors
func()
statsd.close()

View file

@ -3,9 +3,12 @@ seconds = 1000
module.exports = OpenSocketsMonitor = module.exports = OpenSocketsMonitor =
monitor: (logger) -> monitor: (logger) ->
setInterval () -> interval = setInterval () ->
OpenSocketsMonitor.gaugeOpenSockets() OpenSocketsMonitor.gaugeOpenSockets()
, 5 * seconds , 5 * seconds
Metrics = require "./metrics"
Metrics.registerDestructor () ->
clearInterval(interval)
gaugeOpenSockets: () -> gaugeOpenSockets: () ->
Metrics = require "./metrics" Metrics = require "./metrics"