New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

daruk-exit-hook

Package Overview
Dependencies
Maintainers
7
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

daruk-exit-hook

Run some code when the process exits

  • 0.3.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by20%
Maintainers
7
Weekly downloads
 
Created
Source

daruk-exit-hook

进程退出时执行回调函数,支持执行异步函数

一般在下列情况下,node进程会退出,这些情况都会被daruk-exit-hook捕获:ctrl+c进程执行完毕pm2 restart|stopuncaughtExceptionunhandledRejection

注意:使用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()手动退出进程时,退出的回调函数不支持执行异步任务;并且如果退出的回调函数报错,有进程退出失败的风险

TODO

  • 建议研究一下 TS 自动生成 commonjs 的 declaration,现在需要手动的在lib目录中添加 export = ExitHook

Keywords

FAQs

Package last updated on 13 May 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc