2017-04-18 108 views

回答

1

我創建了一個NPM包http-graceful-shutdownhttps://github.com/sebhildebrandt/http-graceful-shutdown)前一段時間。這適用於httpexpresskoa。當你想添加自己的清理東西時,我修改了包,這樣你現在就可以添加自己的清理函數,這將在關閉時調用。所以基本上這個包處理所有HTTP關機東西加上調用你的清理功能(如果在選項中提供):

const koa = require('koa'); 
const gracefulShutdown = require('http-graceful-shutdown'); 
const app = new koa(); 

... 
server = app.listen(...); // app can be an express OR koa app 
... 

// your personal cleanup function - this one takes one second to complete 
function cleanup() { 
    return new Promise((resolve) => { 
    console.log('... in cleanup') 
    setTimeout(function() { 
     console.log('... cleanup finished'); 
     resolve(); 
    }, 1000)  
    }); 
} 

// this enables the graceful shutdown with advanced options 
gracefulShutdown(server, 
    { 
     signals: 'SIGINT SIGTERM', 
     timeout: 30000, 
     development: false, 
     onShutdown: cleanup, 
     finally: function() { 
      console.log('Server gracefulls shutted down.....') 
     } 
    } 
);