hapi-shutdown

handle SIGTERM and register triggers to be run before the server stops

Usage no npm install needed!

<script type="module">
  import hapiShutdown from 'https://cdn.skypack.dev/hapi-shutdown';
</script>

README

Hapi-shutdown

Build Status NPM version Dependencies

Plugin listens to SIGTERM and then runs optional triggers before calling server.stop

This is for things that need to happen before server.stop is called.

For things that need to happen after server.stop, you can use server.on('stop', ...)

Usage:

var Hapi = require("hapi");
var server = new Hapi.Server();

server.register([
  {
    plugin: require('hapi-shutdown'),
    options: {
      serverSpindownTime: 10000 // time to wait for existing connections before forcibly stopping the server
    }
  }],
  function(err){
    server.start(function(){

      server.plugins['hapi-shutdown'].register({
        taskname: 'do stuff',
        task: function(done){ console.log('doing stuff before server.stop is called'); done(); },
        timeout: 2000 // time to wait
      })
    });
  });

.register(task)

Register a task to be run before server.stop is called. Can be called as part of another plugin, or using server.after().

Param: task

{
  taskname: 'mytask', // used for logging and to guard against multiple registrations
  task: function(done){
    // do stuff
    done()
  },
  timeout: 2000 // time in ms to wait for the task to complete
}

Returns: a joi validation error for the task.

Logging:

Will log using 'server.log()' and the tag "shutdown"