This commit is contained in:
jusax23 2021-10-23 16:21:40 +02:00
parent 67fdc60026
commit efc9ec9e29
2 changed files with 75 additions and 82 deletions

View file

@ -1,7 +1,4 @@
import dnc from "./manager.js"; import * as M from "./manager.js";
const M = new dnc(process);
M.addShutdownTask(()=>{ M.addShutdownTask(()=>{
return new Promise(function(res, rej) { return new Promise(function(res, rej) {

View file

@ -1,81 +1,77 @@
const manager = function(process){ async function exitHandler(options, exitCode) {
var t = this; shutdown().then(()=>{
async function exitHandler(options, exitCode) { setTimeout(function() {
t.shutdown().then(()=>{ process.exit();
setTimeout(function() { }, 1000);
process.exit(); }).catch(()=>{
}, 1000); setTimeout(function() {
}).catch(()=>{ process.exit();
setTimeout(function() { }, 10000);
process.exit(); });
}, 10000); }
}); [`SIGINT`, `SIGUSR1`, `SIGUSR2`,`SIGTERM`].forEach((eventType) => {
} process.on(eventType, exitHandler.bind(null, eventType));
[`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;