@@ -3,6 +3,7 @@ const eioUtils = require('elasticio-node').messages;
3
3
const co = require ( 'co' ) ;
4
4
const amqp = require ( 'amqplib' ) ;
5
5
const encryptor = require ( '../encryptor.js' ) ;
6
+ const debug = require ( 'debug' ) ( 'consumer' ) ;
6
7
7
8
module . exports . process = processAction ;
8
9
@@ -19,28 +20,33 @@ function processAction(msg, cfg) {
19
20
const queueName = `eio_consumer_${ process . env . ELASTICIO_TASK_ID } _${ process . env . ELASTICIO_USER_ID } ` ;
20
21
const keys = ( cfg . bindingKeys || '#' ) . split ( ',' ) . map ( ( s ) => s . trim ( ) ) ;
21
22
const consumer = ( msg ) => {
22
- console . log ( 'Have got message %s' , JSON . stringify ( msg . content . toString ( ) ) ) ;
23
- this . emit ( 'data' , eioUtils . newMessageWithBody ( { } ) ) ;
23
+ console . log ( 'Have got message id=%s fields=%j' , msg . id , msg . fields ) ;
24
+ const decrypted = encryptor . decryptMessageContent ( msg . content ) ;
25
+ debug ( 'Decrypted message=%j' , decrypted ) ;
26
+ const newMsg = eioUtils . newMessageWithBody ( decrypted . body || { } ) ;
27
+ newMsg . id = msg . id ;
28
+ newMsg . attachments = decrypted . attachments || { } ;
29
+ this . emit ( 'data' , newMsg ) ;
24
30
} ;
25
31
co ( function * ( ) {
26
- console . log ( 'Connecting to amqpURI=%s' , amqpURI ) ;
32
+ debug ( 'Connecting to amqpURI=%s' , amqpURI ) ;
27
33
const conn = yield amqp . connect ( amqpURI ) ;
28
34
29
- console . log ( 'Creating a receiver channel' ) ;
35
+ debug ( 'Creating a receiver channel' ) ;
30
36
const channel = yield conn . createChannel ( ) ;
31
37
32
- console . log ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
38
+ debug ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
33
39
yield channel . assertExchange ( amqpExchange , 'topic' ) ;
34
40
35
- console . log ( 'Asserting queue' ) ;
41
+ debug ( 'Asserting queue' ) ;
36
42
yield channel . assertQueue ( queueName , {
37
43
exclusive : false ,
38
44
durable : false ,
39
45
autoDelete : true
40
46
} ) ;
41
47
42
48
for ( let key of keys ) {
43
- console . log ( `Binding queue to exchange queue=${ queueName } exchange=${ amqpExchange } bindingKey=${ key } ` ) ;
49
+ debug ( `Binding queue to exchange queue=${ queueName } exchange=${ amqpExchange } bindingKey=${ key } ` ) ;
44
50
yield channel . bindQueue ( queueName , amqpExchange , key ) ;
45
51
}
46
52
@@ -49,6 +55,7 @@ function processAction(msg, cfg) {
49
55
noAck : true , // We can't really assert if message was consumed if we emit it yet
50
56
consumerTag : `consumer_${ process . env . ELASTICIO_EXEC_ID } _${ process . env . ELASTICIO_TASK_ID } `
51
57
} ) ;
58
+ console . log ( 'After consume!' ) ;
52
59
} . bind ( this ) ) . catch ( err => {
53
60
console . log ( 'Error occurred' , err . stack || err ) ;
54
61
this . emit ( 'error' , err ) ;
0 commit comments