为了账号安全,请及时绑定邮箱和手机立即绑定

请问在node.js退出之前执行清理操作

请问在node.js退出之前执行清理操作

慕哥6287543 2019-09-02 12:04:07
在node.js退出之前执行清理操作我想告诉node.js总是在它退出之前做一些事情,不管出于什么原因-Ctrl+C、异常或任何其他原因。我试过这个:process.on('exit', function (){   console.log('Goodbye!');});开始这个过程,杀死它,什么也没有发生;再次启动,按下Ctrl+C,仍然什么也没发生.
查看完整描述

3 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

最新情况:

您可以为process.on('exit')而在任何其他情况下(SIGINT或未处理的异常)调用process.exit()

process.stdin.resume();//so the program will not close instantlyfunction exitHandler(options, exitCode) {
    if (options.cleanup) console.log('clean');
    if (exitCode || exitCode === 0) console.log(exitCode);
    if (options.exit) process.exit();}//do something when app is closingprocess.on('exit', exitHandler.bind(null,{cleanup:true}));
    //catches ctrl+c eventprocess.on('SIGINT', exitHandler.bind(null, {exit:true}));
    // catches "kill pid" (for example: nodemon restart)process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
    process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
    //catches uncaught exceptionsprocess.on('uncaughtException', exitHandler.bind(null, {exit:true}));



查看完整回答
反对 回复 2019-09-04
?
梦里花落0921

TA贡献1772条经验 获得超5个赞

下面的脚本允许对所有退出条件都有一个处理程序。它使用一个特定于应用程序的回调函数来执行自定义清理代码。


Clearup.js


// Object to capture process exits and call app specific cleanup function


function noOp() {};


exports.Cleanup = function Cleanup(callback) {


  // attach user callback to the process event emitter

  // if no callback, it will still exit gracefully on Ctrl-C

  callback = callback || noOp;

  process.on('cleanup',callback);


  // do app specific cleaning before exiting

  process.on('exit', function () {

    process.emit('cleanup');

  });


  // catch ctrl+c event and exit normally

  process.on('SIGINT', function () {

    console.log('Ctrl-C...');

    process.exit(2);

  });


  //catch uncaught exceptions, trace, then exit normally

  process.on('uncaughtException', function(e) {

    console.log('Uncaught Exception...');

    console.log(e.stack);

    process.exit(99);

  });

};

这段代码截取了未识别的异常、Ctrl-C和正常的退出事件。然后,它在退出之前调用一个可选的用户清理回调函数,用一个对象处理所有退出条件。


该模块只是扩展Process对象,而不是定义另一个事件发射器。如果没有应用程序特定的回调,清理默认为无OP函数。当Ctrl-C退出时,子进程仍在运行,这对我的使用来说已经足够了。


您可以根据需要轻松添加其他退出事件,如SIGHUP。注意:根据NodeJS手册,SIGKILL不能有侦听器。下面的测试代码演示了使用Clearup.js的各种方法


// test cleanup.js on version 0.10.21


// loads module and registers app specific cleanup callback...

var cleanup = require('./cleanup').Cleanup(myCleanup);

//var cleanup = require('./cleanup').Cleanup(); // will call noOp


// defines app specific callback...

function myCleanup() {

  console.log('App specific cleanup code...');

};


// All of the following code is only needed for test demo


// Prevents the program from closing instantly

process.stdin.resume();


// Emits an uncaught exception when called because module does not exist

function error() {

  console.log('error');

  var x = require('');

};


// Try each of the following one at a time:


// Uncomment the next line to test exiting on an uncaught exception

//setTimeout(error,2000);


// Uncomment the next line to test exiting normally

//setTimeout(function(){process.exit(3)}, 2000);


// Type Ctrl-C to test forced exit 



查看完整回答
反对 回复 2019-09-04
  • 3 回答
  • 0 关注
  • 465 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信