From e3c1156a4319fcd67c9443ab13cc2e99bb098099 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Wed, 25 Nov 2020 16:10:59 +0100 Subject: [PATCH] Node Js Process Module --- TaskServer/NodeJsProcess.js | 56 +++++++++++++++++++++++++++++++++++++ TaskServer/server.js | 46 ++---------------------------- 2 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 TaskServer/NodeJsProcess.js diff --git a/TaskServer/NodeJsProcess.js b/TaskServer/NodeJsProcess.js new file mode 100644 index 000000000..6f3f1ceb4 --- /dev/null +++ b/TaskServer/NodeJsProcess.js @@ -0,0 +1,56 @@ +exports.newNodeJsProcess = function newNodeJsProcess() { + + let thisObject = { + initialize: initialize + } + + return thisObject; + + function initialize() { + + process.on('uncaughtException', function (err) { + console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) + console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) + console.log(err.stack) + global.EXIT_NODE_PROCESS() + }) + + process.on('unhandledRejection', (reason, p) => { + console.log('[ERROR] Task Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason)) + console.log('[ERROR] Task Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p)) + console.log(reason.stack) + global.EXIT_NODE_PROCESS() + }) + + process.on('exit', function (config) { + + if (global.TASK_NODE !== undefined) { + /* We send an event signaling that the Task is being terminated. */ + let key = global.TASK_NODE.name + '-' + global.TASK_NODE.type + '-' + global.TASK_NODE.id + + global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Stopped') // Meaning Task Stopped + global.EVENT_SERVER_CLIENT.finalize() + global.EVENT_SERVER_CLIENT = undefined + } + + //console.log('[INFO] Task Server -> server -> process.on.exit -> About to exit -> config = ' + config) + }) + + /* Here we listen for the message to stop this Task / Process comming from the Task Manager, which is the paret of this node js process. */ + process.on('message', message => { + if (message === 'Stop this Task') { + + global.STOP_TASK_GRACEFULLY = true; + + /* + There are some process that might no be able to end grafully, for example the ones schedulle to process information in a future day or month. + In order to be sure that the process will be terminated, we schedulle one forced exit in 2 minutes from now. + */ + let key = global.TASK_NODE.name + '-' + global.TASK_NODE.type + '-' + global.TASK_NODE.id + console.log('[INFO] Task Server -> server -> process.on -> Stopping Task ' + key + '. Nodejs process will be exited in less than 1 minute.') + setTimeout(global.EXIT_NODE_PROCESS, 60000); + } + }); + + } +} \ No newline at end of file diff --git a/TaskServer/server.js b/TaskServer/server.js index 75531de4f..26059811b 100644 --- a/TaskServer/server.js +++ b/TaskServer/server.js @@ -2,49 +2,9 @@ let GLOBALS = require('./Globals.js'); let GLOBALS_MODULE = GLOBALS.newGlobals() GLOBALS_MODULE.initialize() -process.on('uncaughtException', function (err) { - console.log('[ERROR] Task Server -> server -> uncaughtException -> err.message = ' + err.message) - console.log('[ERROR] Task Server -> server -> uncaughtException -> err.stack = ' + err.stack) - console.log(err.stack) - global.EXIT_NODE_PROCESS() -}) - -process.on('unhandledRejection', (reason, p) => { - console.log('[ERROR] Task Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason)) - console.log('[ERROR] Task Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p)) - console.log(reason.stack) - global.EXIT_NODE_PROCESS() -}) - -process.on('exit', function (config) { - - if (global.TASK_NODE !== undefined) { - /* We send an event signaling that the Task is being terminated. */ - let key = global.TASK_NODE.name + '-' + global.TASK_NODE.type + '-' + global.TASK_NODE.id - - global.EVENT_SERVER_CLIENT.raiseEvent(key, 'Stopped') // Meaning Task Stopped - global.EVENT_SERVER_CLIENT.finalize() - global.EVENT_SERVER_CLIENT = undefined - } - - //console.log('[INFO] Task Server -> server -> process.on.exit -> About to exit -> config = ' + config) -}) - -/* Here we listen for the message to stop this Task / Process comming from the Task Manager, which is the paret of this node js process. */ -process.on('message', message => { - if (message === 'Stop this Task') { - - global.STOP_TASK_GRACEFULLY = true; - - /* - There are some process that might no be able to end grafully, for example the ones schedulle to process information in a future day or month. - In order to be sure that the process will be terminated, we schedulle one forced exit in 2 minutes from now. - */ - let key = global.TASK_NODE.name + '-' + global.TASK_NODE.type + '-' + global.TASK_NODE.id - console.log('[INFO] Task Server -> server -> process.on -> Stopping Task ' + key + '. Nodejs process will be exited in less than 1 minute.') - setTimeout(global.EXIT_NODE_PROCESS, 60000); - } -}); +let NODE_JS_PROCESS = require('./NodeJsProcess.js'); +let NODE_JS_PROCESS_MODULE = NODE_JS_PROCESS.newNodeJsProcess() +NODE_JS_PROCESS_MODULE.initialize() require('dotenv').config();