From 7cd335662fd0e1a31572c6306528c2005c8490c7 Mon Sep 17 00:00:00 2001 From: Seth Silesky Date: Thu, 29 Sep 2022 22:52:56 -0500 Subject: [PATCH] update README --- packages/node/README.md | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/node/README.md b/packages/node/README.md index 4f9b04b8c..5e8c7e720 100644 --- a/packages/node/README.md +++ b/packages/node/README.md @@ -20,7 +20,6 @@ analytics.identify('Test User', { loggedIn: true }, { userId: "123456" }) analytics.track('hello world', {}, { userId: "123456" }) ``` - ## Graceful Shutdown ### Avoid losing events on exit! * Call `.closeAndFlush()` to stop collecting new events and flush all existing events. @@ -28,7 +27,7 @@ analytics.track('hello world', {}, { userId: "123456" }) ```ts await analytics.closeAndFlush() // or -await analytics.closeAndFlush({ timeout: 5000 }) // automatically closes after 5000ms +await analytics.closeAndFlush({ timeout: 5000 }) // force resolve after 5000ms ``` ### Graceful Shutdown: Advanced Example ```ts @@ -38,16 +37,22 @@ const app = express() const server = app.listen(3000) app.get('/', (req, res) => res.send('Hello World!')); -const onExit = () => { - setTimeout(async () => { - await analytics.closeAndFlush() // flush all existing events - server.close(() => process.exit()) - }, 0); +const onExit = async () => { + await analytics.closeAndFlush() // flush all existing events + console.log("Closing server ..."); + server.close(() => process.exit()); + setTimeout(() => { + console.log("Force closing!"); + process.exit(1); + }, 5000); // force close if connections are still open after 5 seconds }; +process.on("SIGINT", onExit); +process.on("SIGTERM", onExit); + process.on('SIGINT', onExit) process.on('SIGTERM', onExit); - +``` ``` ## Event Emitter