@@ -5,9 +5,6 @@ const amqp = require('amqplib');
5
5
const encryptor = require ( '../encryptor.js' ) ;
6
6
const debug = require ( 'debug' ) ( 'consumer' ) ;
7
7
8
- let channel ;
9
- const queueName = `eio_consumer_${ process . env . ELASTICIO_FLOW_ID } _${ process . env . ELASTICIO_USER_ID } ` ;
10
-
11
8
/**
12
9
* This function will be called on component before the first message will be processed
13
10
*
@@ -17,13 +14,31 @@ function init(cfg) {
17
14
console . log ( 'Starting initialization, cfg=%j queueName=%s' , cfg , queueName ) ;
18
15
const amqpURI = cfg . amqpURI ;
19
16
const amqpExchange = cfg . topic ;
17
+ const queueName = `eio_consumer_${ process . env . ELASTICIO_FLOW_ID } _${ process . env . ELASTICIO_USER_ID } ` ;
20
18
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
+
21
36
return co ( function * initialize ( ) {
22
37
debug ( 'Connecting to amqpURI=%s' , amqpURI ) ;
23
38
const conn = yield amqp . connect ( amqpURI ) ;
24
39
25
40
debug ( 'Creating a receiver channel' ) ;
26
- channel = yield conn . createChannel ( ) ;
41
+ const channel = yield conn . createChannel ( ) ;
27
42
28
43
debug ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
29
44
yield channel . assertExchange ( amqpExchange , 'topic' ) ;
@@ -40,6 +55,13 @@ function init(cfg) {
40
55
yield channel . bindQueue ( queueName , amqpExchange , key ) ;
41
56
}
42
57
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' ) ;
43
65
} ) ;
44
66
}
45
67
@@ -50,24 +72,8 @@ function init(cfg) {
50
72
* @param cfg configuration that is account information and configuration field values
51
73
*/
52
74
function processAction ( msg , cfg ) {
53
- console . log ( 'Trigger started, cfg=%j' , cfg ) ;
54
- const consumer = ( msg ) => {
55
- debug ( 'Have got message fields=%j properties=%j' , msg . fields , msg . properties ) ;
56
- const decrypted = encryptor . decryptMessageContent ( msg . content ) ;
57
- debug ( 'Decrypted message=%j' , decrypted ) ;
58
- const newMsg = eioUtils . newMessageWithBody ( decrypted . body || { } ) ;
59
- newMsg . id = msg . properties . messageId ;
60
- newMsg . attachments = decrypted . attachments || { } ;
61
- this . emit ( 'data' , newMsg ) ;
62
- } ;
63
- return co ( function * consume ( ) {
64
- console . log ( 'Starting consuming from %s' , queueName ) ;
65
- yield channel . consume ( queueName , consumer , {
66
- noAck : true , // We can't really assert if message was consumed if we emit it yet
67
- consumerTag : `consumer_${ process . env . ELASTICIO_EXEC_ID } _${ process . env . ELASTICIO_FLOW_ID } `
68
- } ) ;
69
- console . log ( 'Consumption started' ) ;
70
- } ) ;
75
+ console . log ( 'Trigger started but we don not need to do anything here' ) ;
76
+ return Promise . resolve ( ) ;
71
77
}
72
78
73
79
module . exports . process = processAction ;
0 commit comments