forked from hyperflow-lsc/hyperflow-job-executor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhflow-job-listener.js
99 lines (86 loc) · 2.92 KB
/
hflow-job-listener.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const amqp = require('amqplib/callback_api'),
redis = require('redis'),
rcl = redis.createClient(process.env.REDIS_URL),
uuid = require('uuid'),
handleJob = require('./handler').handleJob;
const queue = process.env.QUEUE_NAME;
const CONSUMER_TAG = uuid.v4();
let connection_handler = null
let channel_handler = null
let msg_processing = false
let consumer_created = false
let consumer_cancelled = false
process.on('SIGTERM', async () => {
console.log("SIGTERM received. Closing process")
if (channel_handler !== null && consumer_created) {
await channel_handler.cancel(CONSUMER_TAG);
}
consumer_cancelled = true
if (msg_processing === false) {
setTimeout(closeConnections, 5000);
}
})
async function executeTask(tasks) {
for (let idx = 0; idx < tasks.length; idx++) {
let jobExitCode = await handleJob(tasks[idx].id, rcl, tasks[idx].message);
console.log("Task", tasks[idx], "job exit code:", jobExitCode);
}
}
async function onMessage(channel, msg) {
console.log(" [x] Received %s", msg.content.toString());
msg_processing = true
executeTask(JSON.parse(msg.content).tasks).then((value) => {
console.log("Message completed")
channel.ack(msg)
msg_processing = false
}).catch(function () {
console.error("Message processing error")
channel.nack(msg);
msg_processing = false
}).finally(function () {
if (consumer_cancelled === true) {
setTimeout(closeConnections, 5000);
}
});
}
function onChannelCreated(error, channel) {
if (error) {
throw error;
}
channel_handler = channel
const consumerOptions = {noAck: false, consumerTag: CONSUMER_TAG}
const queueOptions = {durable: false, expires: 6000000}
const prefetch = parseInt(process.env['RABBIT_PREFETCH_SIZE']) || 1
channel.prefetch(prefetch);
channel.assertQueue(queue, queueOptions);
console.log(" [*] Waiting for messages in queue: %s", queue);
console.log("Consumer tag: " + CONSUMER_TAG);
channel.consume(queue, (msg) => onMessage(channel, msg), consumerOptions);
consumer_created = true
}
function onConnectionCreated(error, connection) {
if (error) {
throw error;
}
connection_handler = connection
connection.createChannel(onChannelCreated);
}
async function closeConnections() {
console.log("Terminate listener invoked")
if (channel_handler !== null) {
await channel_handler.close()
console.log("RabbitMQ channel closed")
}
if (connection_handler !== null) {
await connection_handler.close()
console.log("RabbitMQ connection closed")
}
if (rcl !== null) {
await rcl.quit()
console.log("Redis connection closed")
}
console.log("Terminate listener processed")
process.exit(0)
}
amqp.connect(`amqp://${process.env.RABBIT_HOSTNAME}`, onConnectionCreated);