npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

daruk-exit-hook

v0.3.0

Published

Run some code when the process exits

Downloads

35

Readme

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