Shutting down cleanly
Ensure your app shuts down cleanly when terminated
Often you'll want to run your app in a way that it can terminate cleanly and give it time to finish doing its work before exiting. This is common if you manually terminate it (CTRL + C), send it a kill signal like kill -INT 1234 or if the underlying pod/container/host is stopping.
In these situations your app should finish processing the any messages its read from the queue and not read any more, allowing it to exit gracefully.
To do this, hooking into the process signals and calling dispose on the bus is the cleanest way.

Example handling SIGINT

1
import { Bus, BusInstance } from '@node-ts/bus-core'
2
​
3
let bus: BusInstance
4
​
5
const start = async () => {
6
bus = Bus.configure().initialize()
7
await bus.start()
8
}
9
​
10
/**
11
* Listens for a signal interrupt and gracefully disposes the bus
12
* before exiting.
13
*/
14
const listenForSigInt = () => {
15
process.once('SIGINT', async () => {
16
console.log('Received SIGINT, shutting down...')
17
if (bus) {
18
await bus.dispose()
19
}
20
})
21
}
22
​
23
listenForSigInt()
24
start().catch(console.err)
Copied!
Copy link