Skip to content

Commit 5c83f5e

Browse files
committed
Now ignoring second runs
1 parent 7405a06 commit 5c83f5e

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

lib/triggers/consume.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,26 @@ const amqp = require('amqplib');
55
const encryptor = require('../encryptor.js');
66
const debug = require('debug')('consumer');
77

8+
let channel;
9+
const queueName = `eio_consumer_${process.env.ELASTICIO_FLOW_ID}_${process.env.ELASTICIO_USER_ID}`;
10+
let listening;
11+
812
/**
913
* This function will be called on component before the first message will be processed
1014
*
1115
* @param cfg
1216
*/
1317
function init(cfg) {
14-
console.log('Starting initialization, cfg=%j', cfg);
18+
console.log('Starting initialization, cfg=%j queueName=%s', cfg, queueName);
1519
const amqpURI = cfg.amqpURI;
1620
const amqpExchange = cfg.topic;
17-
const queueName = `eio_consumer_${process.env.ELASTICIO_FLOW_ID}_${process.env.ELASTICIO_USER_ID}`;
1821
const keys = (cfg.bindingKeys || '#').split(',').map((s) => s.trim());
19-
20-
/**
21-
* Consumer function that will be called when new message
22-
* is received
23-
*
24-
* @param msg
25-
*/
26-
const consumer = (msg) => {
27-
debug('Have got message fields=%j properties=%j', msg.fields, msg.properties);
28-
const decrypted = encryptor.decryptMessageContent(msg.content);
29-
debug('Decrypted message=%j', decrypted);
30-
const newMsg = eioUtils.newMessageWithBody(decrypted.body || {});
31-
newMsg.id = msg.properties.messageId;
32-
newMsg.attachments = decrypted.attachments || {};
33-
this.emit('data', newMsg);
34-
};
35-
3622
return co(function* initialize() {
3723
debug('Connecting to amqpURI=%s', amqpURI);
3824
const conn = yield amqp.connect(amqpURI);
3925

4026
debug('Creating a receiver channel');
41-
const channel = yield conn.createChannel();
27+
channel = yield conn.createChannel();
4228

4329
debug('Asserting topic exchange exchange=%s', amqpExchange);
4430
yield channel.assertExchange(amqpExchange, 'topic');
@@ -55,13 +41,6 @@ function init(cfg) {
5541
yield channel.bindQueue(queueName, amqpExchange, key);
5642
}
5743
console.log('Initialization completed');
58-
59-
console.log('Starting consuming from queue=%s', queueName);
60-
yield channel.consume(queueName, consumer, {
61-
noAck: true, // We can't really assert if message was consumed if we emit it yet
62-
consumerTag: `consumer_${process.env.ELASTICIO_EXEC_ID}_${process.env.ELASTICIO_FLOW_ID}`
63-
});
64-
console.log('Consumption started');
6544
});
6645
}
6746

@@ -72,8 +51,29 @@ function init(cfg) {
7251
* @param cfg configuration that is account information and configuration field values
7352
*/
7453
function processAction(msg, cfg) {
75-
console.log('Trigger started but we don not need to do anything here');
76-
return Promise.resolve();
54+
console.log('Trigger started, cfg=%j', cfg);
55+
if (listening) {
56+
console.log('Trigger was called again, we will ignore this run');
57+
return Promise.resolve();
58+
}
59+
const consumer = (msg) => {
60+
debug('Have got message fields=%j properties=%j', msg.fields, msg.properties);
61+
const decrypted = encryptor.decryptMessageContent(msg.content);
62+
debug('Decrypted message=%j', decrypted);
63+
const newMsg = eioUtils.newMessageWithBody(decrypted.body || {});
64+
newMsg.id = msg.properties.messageId;
65+
newMsg.attachments = decrypted.attachments || {};
66+
this.emit('data', newMsg);
67+
};
68+
return co(function* consume() {
69+
console.log('Starting consuming from %s', queueName);
70+
yield channel.consume(queueName, consumer, {
71+
noAck: true, // We can't really assert if message was consumed if we emit it yet
72+
consumerTag: `consumer_${process.env.ELASTICIO_EXEC_ID}_${process.env.ELASTICIO_FLOW_ID}`
73+
});
74+
console.log('Consumption started');
75+
listening = true;
76+
});
7777
}
7878

7979
module.exports.process = processAction;

0 commit comments

Comments
 (0)