第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

在node.js退出之前執(zhí)行清理操作

在node.js退出之前執(zhí)行清理操作

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

3 回答

?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個贊

最新情況:

您可以為process.on('exit')而在任何其他情況下(SIGINT或未處理的異常)調(diào)用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}));



查看完整回答
反對 回復(fù) 2019-10-13
?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個贊

下面的腳本允許對所有退出條件都有一個處理程序。它使用一個特定于應(yīng)用程序的回調(diào)函數(shù)來執(zhí)行自定義清理代碼。


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和正常的退出事件。然后,它在退出之前調(diào)用一個可選的用戶清理回調(diào)函數(shù),用一個對象處理所有退出條件。


該模塊只是擴(kuò)展Process對象,而不是定義另一個事件發(fā)射器。如果沒有應(yīng)用程序特定的回調(diào),清理默認(rèn)為無OP函數(shù)。當(dāng)Ctrl-C退出時,子進(jìn)程仍在運(yùn)行,這對我的使用來說已經(jīng)足夠了。


您可以根據(jù)需要輕松添加其他退出事件,如SIGHUP。注意:根據(jù)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 



查看完整回答
反對 回復(fù) 2019-10-13
  • 3 回答
  • 0 關(guān)注
  • 530 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號