Doing a cleanup action just before Node.js exits

后端 未结 11 1472
时光说笑
时光说笑 2020-11-22 13:54

I want to tell Node.js to always do something just before it exits, for whatever reason — Ctrl+C, an exception, or any other reason.

I tried th

11条回答
  •  遥遥无期
    2020-11-22 14:05

    function fnAsyncTest(callback) {
        require('fs').writeFile('async.txt', 'bye!', callback);
    }
    
    function fnSyncTest() {
        for (var i = 0; i < 10; i++) {}
    }
    
    function killProcess() {
    
        if (process.exitTimeoutId) {
            return;
        }
    
        process.exitTimeoutId = setTimeout(() => process.exit, 5000);
        console.log('process will exit in 5 seconds');
    
        fnAsyncTest(function() {
            console.log('async op. done', arguments);
        });
    
        if (!fnSyncTest()) {
            console.log('sync op. done');
        }
    }
    
    // https://nodejs.org/api/process.html#process_signal_events
    process.on('SIGTERM', killProcess);
    process.on('SIGINT', killProcess);
    
    process.on('uncaughtException', function(e) {
    
        console.log('[uncaughtException] app will be terminated: ', e.stack);
    
        killProcess();
        /**
         * @https://nodejs.org/api/process.html#process_event_uncaughtexception
         *  
         * 'uncaughtException' should be used to perform synchronous cleanup before shutting down the process. 
         * It is not safe to resume normal operation after 'uncaughtException'. 
         * If you do use it, restart your application after every unhandled exception!
         * 
         * You have been warned.
         */
    });
    
    console.log('App is running...');
    console.log('Try to press CTRL+C or SIGNAL the process with PID: ', process.pid);
    
    process.stdin.resume();
    // just for testing
    

提交回复
热议问题