Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Sep 30, 2022
1 parent 2035675 commit 7cd3356
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,14 @@ 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.
* If a callback on an event call is included, this also waits for all callbacks to be called, and any of their subsequent promises to be resolved.
```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
Expand All @@ -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
Expand Down

0 comments on commit 7cd3356

Please sign in to comment.