From fcbc24fbf56cacbda663981c530d152503a9a46f Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 23 Oct 2021 12:25:28 +0200 Subject: [PATCH] added files --- .gitignore | 1 + main.js | 30 ++++++++++++++++++++++ manager.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 23 +++++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 .gitignore create mode 100644 main.js create mode 100644 manager.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/main.js b/main.js new file mode 100644 index 0000000..defc75d --- /dev/null +++ b/main.js @@ -0,0 +1,30 @@ +import dnc from "./manager.js"; + +const M = new dnc(process); + + +M.addShutdownTask(()=>{ + return new Promise(function(res, rej) { + //do something with async + console.log("Shuting down async ..."); + setTimeout(()=>{ + console.log("async done"); + res(); + },5000); + + }); +},30000);//max time + +M.addShutdownTask(()=>{ + console.log("Shuting down sync"); +},30000);//max time + +M.shutdown().then(()=>{ + setTimeout(function() { //some save time + process.exit(); + }, 1000); +}).catch(()=>{ + setTimeout(function() { //shutdown on error with more save time + process.exit(); + }, 10000); +}); diff --git a/manager.js b/manager.js new file mode 100644 index 0000000..f72847f --- /dev/null +++ b/manager.js @@ -0,0 +1,71 @@ + +const manager = function(process){ + var t = this; + async function exitHandler(options, exitCode) { + t.shutdown().then(()=>{ + setTimeout(function() { + process.exit(); + }, 1000); + }).catch(()=>{ + setTimeout(function() { + process.exit(); + }, 10000); + }); + } + [`SIGINT`, `SIGUSR1`, `SIGUSR2`,`SIGTERM`].forEach((eventType) => { + process.on(eventType, exitHandler.bind(null, eventType)); + }); + + var shutdownTasks = [] + + t.addShutdownTask = function(task,maxDuration=5000){ + shutdownTasks.push({t:task,d:maxDuration}); + }; + + t.shutdown = function(){ + return new Promise((res,rej)=>{ + console.log("Shuting down ..."); + + var maxDuration = 1000; + var running = 0; + var timeout = null; + + function mayShutdown(force=false){ + if(running==0||force){ + if(timeout!=null)clearInterval(timeout); + res(); + } + } + running++; + for (var i = 0; i < shutdownTasks.length; i++) { + try { + if(shutdownTasks[i].d>maxDuration)maxDuration=shutdownTasks[i].d; + running++; + let a = shutdownTasks[i].t(); + if(typeof a.then == "function"){ + a.then(()=>{ + running--; + mayShutdown(); + }); + }else{ + running--; + if(shutdownTasks.length-1 == i){ + mayShutdown(); + } + } + } catch (e) { + running--; + mayShutdown(); + } + } + running--; + timeout = setTimeout(function () { + timeout = null; + mayShutdown(true); + }, maxDuration); + mayShutdown(); + }); + }; + +} +export default manager; diff --git a/package.json b/package.json new file mode 100644 index 0000000..db86ff4 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "dnc", + "version": "1.0.0", + "description": "Do not crash!!!\r Simple NodeJS Server Manager", + "main": "manager.js", + "type":"module", + "files":[ + "manager.js" + ], + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://gitlab.com/jusax23/dnc.git" + }, + "author": "jusax23", + "license": "ISC", + "bugs": { + "url": "https://gitlab.com/jusax23/dnc/issues" + }, + "homepage": "https://gitlab.com/jusax23/dnc#readme" +}