亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在node.js退出之前執行清理操作

在node.js退出之前執行清理操作

小唯快跑啊 2019-11-11 12:08:41
在node.js退出之前執行清理操作我想告訴node.js總是在它退出之前做一些事情,不管出于什么原因-Ctrl+C、異?;蛉魏纹渌?。我試過這個:process.on('exit', function (){   console.log('Goodbye!');});開始這個過程,殺死它,什么也沒有發生;再次啟動,按下Ctrl+C,仍然什么也沒發生.
查看完整描述

3 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

最新情況:

您可以為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-11-12
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

下面的腳本允許對所有退出條件都有一個處理程序。它使用一個特定于應用程序的回調函數來執行自定義清理代碼。


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-11-12
  • 3 回答
  • 0 關注
  • 573 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號