Graceful Shutdown & Event Flushing
By default, SettleSettle buffers event tracking signals in local application memory. Every 2 seconds (or when the bucket hits 50 items), it flushes them in a single batched REST request.
This boosts performance massively, but introduces a small risk: What if your server restarts? Any events currently in that 2-second window will disappear.
The Cure: Listen to OS Signals
Tell Node.js to force a memory flush right before exiting.
typescript
import { settle } from './lib/settle'
async function shutdown() {
console.log('⚠️ Node process shutting down. Draining event buffer...')
await settle.destroy() // Forces a sync flush and destroys timers
process.exit(0)
}
// Catch OS termination signals
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)Essential if you are deploying to Render, Vercel, Heroku, or Docker where container lifecycles restart frequently during deployments.
