hemerajs / fastify-graceful-shutdown

Gracefully shutdown fastify
MIT License
70 stars 13 forks source link

Just checking if this code is correct #29

Closed cyruzin closed 1 year ago

cyruzin commented 2 years ago

It's just a doubt. Is this code working? Not showing 'Server closed'. and 'Postgres closed.' in the console.

import FastifyGracefulShutdown from 'fastify-graceful-shutdown';

fastify.register(FastifyGracefulShutdown).after((err) => {
  fastify.log.error(err);

  fastify.gracefulShutdown(async (signal: string, next: (err?: Error | undefined) => void) => {
    try {
      fastify.log.info(signal + ' received...');

      fastify.log.info('Closing postgres...');
      await postgres.end();
      fastify.log.info('Postgres closed.');

      next();
    } catch (err: any) {
      fastify.log.error(err);
      next(err);
    }
  });
});
{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"received signal"}

{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","msg":"triggering close hook"}

{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","msg":"SIGINT received..."}

{"level":30,"time":1659923347644,"pid":6696,"hostname":"dubeux","msg":"Closing postgres..."}

{"level":50,"time":1659923357654,"pid":6696,"hostname":"dubeux","plugin":"fastify-graceful-shutdown","signal":"SIGINT","timeout":10000,"msg":"terminate process after timeout"}
bstenersen commented 1 year ago

Hi @cyruzin , i encountered the same behaviour. Calling fastify.log.flush() before next() solved the issue.

cyruzin commented 1 year ago

Hi @cyruzin , i encountered the same behaviour. Calling fastify.log.flush() before next() solved the issue.

Thanks! 😀

StarpTech commented 1 year ago

Hi, the issue was fixed with the last release. Flush() is called before exiting the process.