2017-04-18 11 views
1

expressjsのための優雅な停止の例はたくさんありますが、どのように私はkoajsのために同じことを達成することができますか?koajsサーバーの正常な停止方法

私も

、私はいくつかの時間前にNPMパッケージhttp-graceful-shutdownhttps://github.com/sebhildebrandt/http-graceful-shutdown)を作成したマングースデータベース接続、および2のOracle DB接続(https://github.com/oracle/node-oracledb

+0

:だから、基本的には、このパッケージには、すべてのhttpシャットダウン物事プラス(オプションで提供されている場合)あなたのクリーンアップ関数を呼び出しを処理します – zeronone

答えて

1

を持っているデータベース接続を切断したいと思います。これはhttp,expresskoaと完全に機能します。独自のクリーンアップも追加したいので、パッケージを変更しました。これで、シャットダウン時に呼び出される独自のクリーンアップ機能を追加できるようになりました。私は `など優雅な再読み込みをサポートして生産のpm2`を、使用することをお勧めします

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.....') 
     } 
    } 
); 
関連する問題