raspi-back/app/huphandler.js

42 lines
901 B
JavaScript
Executable File

const huptasks = [];
const addHupTask = (id, task)=>{
huptasks.push({id, task})
}
const handleHups = ()=>{
const promises = [];
huptasks.forEach(({id, task})=>{
const fulfill = task();
if(fulfill&&fulfill.then){
promises.push(id);
fulfill.then((error)=>{
promises.splice(promises.indexOf(id), 1)
if(error){
require("fs").writeFileSync("./.error", `${error}\n`, {flag:"a"})
}
if(!promises.length){
process.exit()
}
})
}
})
if(!promises.length){
process.exit()
}
}
process.on("SIGINT", ()=>{
handleHups()
})
process.on("SIGTERM", ()=>{
handleHups()
})
process.on("SIGHUP", ()=>{
handleHups()
})
module.exports = {addHupTask}