From efc9ec9e29cd8dc238a47439ff2f124effe1b47c Mon Sep 17 00:00:00 2001 From: jusax23 Date: Sat, 23 Oct 2021 16:21:40 +0200 Subject: [PATCH] modeule --- main.js | 5 +- manager.js | 152 ++++++++++++++++++++++++++--------------------------- 2 files changed, 75 insertions(+), 82 deletions(-) diff --git a/main.js b/main.js index defc75d..40660f5 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,4 @@ -import dnc from "./manager.js"; - -const M = new dnc(process); - +import * as M from "./manager.js"; M.addShutdownTask(()=>{ return new Promise(function(res, rej) { diff --git a/manager.js b/manager.js index 15b2de8..f729ad0 100644 --- a/manager.js +++ b/manager.js @@ -1,81 +1,77 @@ -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)); +async function exitHandler(options, exitCode) { + 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 = [] + +export const addShutdownTask = function(task,maxDuration=5000){ + shutdownTasks.push({t:task,d:maxDuration}); +}; + +export const 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 const saveShutdown = function(){ + shutdown().then(() => { + setTimeout(function() { //some save time + process.exit(); + }, 1000); + }).catch(() => { + setTimeout(function() { //shutdown on error with more save time + process.exit(); + }, 10000); }); - - 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(); - }); - }; - t.saveShutdown = function(){ - t.shutdown().then(() => { - setTimeout(function() { //some save time - process.exit(); - }, 1000); - }).catch(() => { - setTimeout(function() { //shutdown on error with more save time - process.exit(); - }, 10000); - }); - } } -export default manager;