-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathindex.js
43 lines (33 loc) · 1.24 KB
/
index.js
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
33
34
35
36
37
38
39
40
41
42
43
const { CloudEvent, Emitter, emitterFor, httpTransport } = require('cloudevents');
const K_SINK = process.env['K_SINK'];
K_SINK || logExit('Error: K_SINK Environment variable is not defined');
console.log(`Sink URL is ${K_SINK}`);
const source = 'urn:event:from:heartbeat/example';
const type = 'heartbeat.example';
let eventIndex = 0;
setInterval(() => {
console.log(`Emitting event # ${++eventIndex}`);
// Create a new CloudEvent each second
const event = new CloudEvent({ source, type, data: {'hello': `World # ${eventIndex}`} });
// Emits the 'cloudevent' Node.js event application-wide
event.emit();
}, 1000);
// Create a function that can post an event
const emit = emitterFor(httpTransport(K_SINK));
// Send the CloudEvent any time a Node.js 'cloudevent' event is emitted
Emitter.on('cloudevent', emit);
registerGracefulExit();
function registerGracefulExit() {
process.on('exit', logExit);
//catches ctrl+c event
process.on('SIGINT', logExit);
process.on('SIGTERM', logExit);
// catches 'kill pid' (for example: nodemon restart)
process.on('SIGUSR1', logExit);
process.on('SIGUSR2', logExit);
}
function logExit(message = 'Exiting...') {
// Handle graceful exit
process.stdout.write(`${message}\n`);
process.exit();
}