@mgcrea/fastify-graceful-exit
Version:
Graceful exit for your fastify application
43 lines (41 loc) • 1.35 kB
JavaScript
// src/index.ts
import fastifyPlugin from "fastify-plugin";
// src/plugin.ts
var plugin = async (fastify, options = {}) => {
const { logBindings = { plugin: "fastify-graceful-exit" }, timeout = 3e3 } = options;
const { log } = fastify;
let closePromise = null;
const gracefullyClose = async (signal) => {
if (closePromise) {
return closePromise;
}
log.warn(logBindings, `Fastify is gracefully closing from signal="${signal}" ...`);
setTimeout(() => {
log.warn(logBindings, `Failed to gracefully close before timeout`);
process.exit(1);
}, timeout);
closePromise = fastify.close();
await closePromise;
process.exit(0);
};
process.on("uncaughtException", async (err) => {
log.error({ err }, `Uncaught Exception: ${err.message}`);
await gracefullyClose("uncaughtException");
});
process.on("unhandledRejection", async (reason, _promise) => {
log.error({ reason }, `Unhandled Rejection: ${String(reason)}`);
await gracefullyClose("unhandledRejection");
});
process.on("SIGTERM", gracefullyClose);
process.on("SIGINT", gracefullyClose);
process.on("SIGUSR2", gracefullyClose);
};
// src/index.ts
var src_default = fastifyPlugin(plugin, {
fastify: ">=4",
name: "fastify-graceful-exit"
});
export {
src_default as default
};
//# sourceMappingURL=index.js.map