Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
daruk-exit-hook
Advanced tools
进程退出时执行回调函数,支持执行异步函数
一般在下列情况下,node进程会退出,这些情况都会被daruk-exit-hook捕获:ctrl+c
、进程执行完毕
、pm2 restart|stop
、uncaughtException
、unhandledRejection
注意:使用pm2 restart|stop
时,pm2的kill_timeout默认是3000ms,就算异步任务没有执行完,超过3000ms进程也会退出,一般可以将这个时间配置长一点:
// pm2.config.js
module.exports = {
apps: [{
name: 'daruk-app',
script: './index.js',
watch: false,
kill_timeout: 10 * 1000
}]
}
const ExitHook = require('daruk-exit-hook')
const exitHook = new ExitHook({
// 就算异步任务没有执行完毕,也必须退出进程的延时
// 默认是10s
asyncTimeoutMs: 10 * 1000,
// 进程退出的回调函数
// 如果传递了第二个参数callback,callback必须执行
// 否则会等到asyncTimeoutMs设定的时间再退出
onExit (err, callback) {
if (err) {
daruk.logger.error(err.message)
}
daruk.logger.info('process exiting')
setTimeout(() => {
daruk.logger.info('process exited')
callback()
}, 1000)
},
// 执行完退出任务,真正调用process.exit退出进程
// 注意:这里并不保证所有退出任务都成功执行
// 参数code是退出码
onExitDone (code) {
console.log('process exited')
}
})
// 也可以编程式地添加退出的回调
exitHook.addHook((err, cb) => {
setTimeout(() => {
// do something 1
cb()
}, 2000)
})
exitHook.addHook(() => {
// do something 2
})
最好不要使用process.exit()
手动退出进程
使用process.exit()
手动退出进程时,退出的回调函数不支持执行异步任务;并且如果退出的回调函数报错,有进程退出失败的风险
export = ExitHook
FAQs
Run some code when the process exits
The npm package daruk-exit-hook receives a total of 6 weekly downloads. As such, daruk-exit-hook popularity was classified as not popular.
We found that daruk-exit-hook demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.