mirror of
https://gitlab.com/jusax23/dnc.git
synced 2024-11-22 06:36:40 +01:00
added files
This commit is contained in:
parent
03cf69f257
commit
fcbc24fbf5
4 changed files with 125 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
node_modules
|
30
main.js
Normal file
30
main.js
Normal file
|
@ -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);
|
||||
});
|
71
manager.js
Normal file
71
manager.js
Normal file
|
@ -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;
|
23
package.json
Normal file
23
package.json
Normal file
|
@ -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"
|
||||
}
|
Loading…
Reference in a new issue