4

There are a lot of examples of graceful stop for expressjs, how can I achieve the same for koajs?

I would like to disconnect database connections as well

I have a mongoose database connection, and 2 oracle db connection (https://github.com/oracle/node-oracledb)

Sibelius Seraphini
  • 5,303
  • 9
  • 34
  • 55

4 Answers4

6

I created an npm package http-graceful-shutdown (https://github.com/sebhildebrandt/http-graceful-shutdown) some time ago. This works perfectly with http, express and koa. As you want to add also your own cleanup stuff, I modified the package, so that you now can add your own cleanup function, that will be called on shutdown. So basically this package handles all http shutdown things plus calling your cleanup function (if provided in the options):

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.....')
        }
    }
);
Sebastian Hildebrandt
  • 2,661
  • 1
  • 14
  • 20
2

I have answered a variation of "how to terminate a HTTP server" many times on different support channels. Unfortunately, I couldn't recommend any of the existing libraries because they are lacking in one or another way. I have since put together a package that (I believe) is handling all the cases expected of graceful HTTP server termination.

https://github.com/gajus/http-terminator

The main benefit of http-terminator is that:

  • it does not monkey-patch Node.js API
  • it immediately destroys all sockets without an attached HTTP request
  • it allows graceful timeout to sockets with ongoing HTTP requests
  • it properly handles HTTPS connections
  • it informs connections using keep-alive that server is shutting down by setting a connection: close header
  • it does not terminate the Node.js process

Usage with Koa:

import Koa from 'koa';
import {
  createHttpTerminator,
} from 'http-terminator';

const app = new Koa();

const server = app.listen();

const httpTerminator = createHttpTerminator({
  server,
});

await httpTerminator.terminate();

Gajus
  • 69,002
  • 70
  • 275
  • 438
1

To make sure the Oracle DB connections are closed nicely, you can use a connection pool and call pool.close() with a drainTime of 0 or greater. This will let the app relatively cleanly interrupt any operation that is currently using a connection. It allows freeing the DB end of the connections without the DB waiting for whatever timeout period to expire before it cleans itself up. Even with two connections this is a solution I'd look at, since it doesn't matter that the pool is small. You may need to set the Oracle Net out-of-band break detection as well, see Connections and High Availability.

Christopher Jones
  • 9,449
  • 3
  • 24
  • 48
1

Modern versions of node have support for AbortController, so no need for external libraries. A Simple example:

const app = new Koa();
const server = http.createServer(app.callback());


const controller = new AbortController();
server.listen({
  host: 'localhost',
  port: 80,
  signal: controller.signal
});


// middleware... etc.
app.use(async (ctx) => {
  ctx.body = 'Hello World';
});

// Later, when you want to close the server.
controller.abort();
enjoylife
  • 3,801
  • 1
  • 24
  • 33