From 5da2030b167ce515053e86929297c44bbfe03cdc Mon Sep 17 00:00:00 2001 From: David House Date: Tue, 12 Nov 2019 16:43:57 -0500 Subject: [PATCH] :sparkles: Now handles shutdown gracefully --- bin/stampede-worker.js | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/bin/stampede-worker.js b/bin/stampede-worker.js index 87f7e42..fcbebc4 100755 --- a/bin/stampede-worker.js +++ b/bin/stampede-worker.js @@ -89,13 +89,14 @@ if ( process.exit(1); } +let workerQueue = null; +let responseQueue = null; +let heartbeatQueue = null; + if (conf.taskTestFile == null) { - const workerQueue = new Queue("stampede-" + conf.taskQueue, redisConfig); - const responseQueue = new Queue( - "stampede-" + conf.responseQueue, - redisConfig - ); - const heartbeatQueue = + workerQueue = new Queue("stampede-" + conf.taskQueue, redisConfig); + responseQueue = new Queue("stampede-" + conf.responseQueue, redisConfig); + heartbeatQueue = conf.heartbeatQueue != null ? new Queue("stampede-" + conf.heartbeatQueue, redisConfig) : null; @@ -117,6 +118,24 @@ if (conf.taskTestFile == null) { handleTask(task, responseTestFile); } +/** + * Handle shutdown gracefully + */ +process.on("SIGINT", function() { + gracefulShutdown(); +}); + +/** + * gracefulShutdown + */ +async function gracefulShutdown() { + console.log("Closing queues"); + await workerQueue.close(); + await responseQueue.close(); + await heartbeatQueue.close(); + process.exit(0); +} + /** * Handle an incoming task * @param {*} task