gajus / http-terminator

Gracefully terminates HTTP(S) server.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When should I call the await httpTerminator.terminate();

rubenrajkowski-nbcuni opened this issue · comments

I assume I have to wrap this in a async method or callback like process.on('SIGTERM', callback).
When do you recommend I run the await httpTerminator.terminate() call?

Thanks Ruben

You'd usually use it either because some error occurred that requires you to terminate the service or because you've received an external signal (such as SIGTERM example you gave).

I use http-terminator in combination with https://github.com/gajus/lightship to terminate my services when Kubernetes instructs to shutdown the service.

@gajus Is this the correct way?

const server = app.listen(process.env.PORT, () => {
  console.log(`Listening on localhost:${process.env.PORT}`);
});

const httpTerminator = createHttpTerminator({
  server
});

process.on('SIGTERM', async () => {
  console.log("Shutting down server.");
  await httpTerminator.terminate();
  process.exit(0);
});