Exit cleanly on SIGINT et al

This commit is contained in:
James Allen 2014-05-07 10:05:07 +01:00
parent d1434f7646
commit f511ebd4b6

View file

@ -24,8 +24,11 @@ app.configure ->
rclient.subscribe("pending-updates") rclient.subscribe("pending-updates")
rclient.on "message", (channel, doc_key) -> rclient.on "message", (channel, doc_key) ->
[project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key) [project_id, doc_id] = Keys.splitProjectIdAndDocId(doc_key)
if !Settings.shuttingDown
UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, (error) -> UpdateManager.processOutstandingUpdatesWithLock project_id, doc_id, (error) ->
logger.error err: error, project_id: project_id, doc_id: doc_id, "error processing update" if error? logger.error err: error, project_id: project_id, doc_id: doc_id, "error processing update" if error?
else
logger.log project_id: project_id, doc_id: doc_id, "ignoring incoming update"
UpdateManager.resumeProcessing() UpdateManager.resumeProcessing()
@ -47,6 +50,9 @@ app.get '/total', (req, res)->
res.send {total:count} res.send {total:count}
app.get '/status', (req, res)-> app.get '/status', (req, res)->
if Settings.shuttingDown
res.send 503 # Service unavailable
else
res.send('document updater is alive') res.send('document updater is alive')
app.use (error, req, res, next) -> app.use (error, req, res, next) ->
@ -56,6 +62,19 @@ app.use (error, req, res, next) ->
else else
res.send(500, "Oops, something went wrong") res.send(500, "Oops, something went wrong")
shutdownCleanly = (signal) ->
return () ->
logger.log signal: signal, "received interrupt, cleaning up"
Settings.shuttingDown = true
setTimeout () ->
logger.log signal: signal, "shutting down"
process.exit()
, 10000
port = Settings.internal?.documentupdater?.port or Settings.apis?.documentupdater?.port or 3003 port = Settings.internal?.documentupdater?.port or Settings.apis?.documentupdater?.port or 3003
app.listen port, "localhost", -> app.listen port, "localhost", ->
logger.log("documentupdater-sharelatex server listening on port #{port}") logger.log("documentupdater-sharelatex server listening on port #{port}")
for signal in ['SIGINT', 'SIGHUP', 'SIGQUIT', 'SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGABRT']
process.on signal, shutdownCleanly(signal)