-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.ts
32 lines (25 loc) · 897 Bytes
/
listener.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { NATS_SERVER_SETTINGS } from './common/constants';
import { buildStan } from './nats/stan';
import HealthcheckCreatedListener from './nats/listeners/healthcheck-created-listener';
console.clear();
const {
clusterID,
subscriber: {
prefix,
},
url,
} = NATS_SERVER_SETTINGS;
// NATS Client (STAN) for the listener
const stan = buildStan(clusterID, prefix, url.dockerCompose);
stan.on('connect', () => {
console.log(`Listener connected to NATS!`);
// Graceful shutdown
stan.on('close', () => {
console.log(`NATS listener connection closed!`);
process.exit(); // Generates SIGTERM
});
new HealthcheckCreatedListener(stan).listen();
});
// Graceful shutdown. i.e., closes connection when ...
process.on('SIGINT', () => stan.close()); // ... interop signal is intercepted
process.on('SIGTERM', () => stan.close()); // ... terminate signal is intercepted