1
1
'use strict' ;
2
2
const eioUtils = require ( 'elasticio-node' ) . messages ;
3
3
const co = require ( 'co' ) ;
4
- const rp = require ( 'request-promise' ) ;
4
+ const amqp = require ( 'amqplib' ) ;
5
+ const encryptor = require ( '../encryptor.js' ) ;
6
+
7
+ var connection , channel ;
5
8
6
9
module . exports . process = processAction ;
7
10
@@ -13,23 +16,31 @@ module.exports.process = processAction;
13
16
* @param snapshot - current values from the snapshot
14
17
*/
15
18
function processAction ( msg , cfg , snapshot ) {
16
- console . log ( 'Action started, snapshot=%j' , snapshot ) ;
17
-
19
+ console . log ( 'Action started' ) ;
20
+ const amqpURI = process . env . ELASTICIO_AMQP_URI ;
21
+ const amqpExchange = `pubsub_${ process . env . ELASTICIO_TASK_ID } _${ process . env . ELASTICIO_USER_ID } ` ;
18
22
co ( function * ( ) {
19
- console . log ( 'Creating new request bin' ) ;
20
-
21
- const bin = yield rp ( {
22
- method : 'POST' ,
23
- uri : 'http://requestb.in/api/v1/bins' ,
24
- json : true
23
+ if ( ! connection ) {
24
+ console . log ( 'Connecting to amqpURI=%s' , amqpURI ) ;
25
+ connection = yield amqp . connect ( amqpURI ) ;
26
+ }
27
+ if ( ! channel ) {
28
+ console . log ( 'Creating a confirm channel' ) ;
29
+ channel = yield conn . createConfirmChannel ( ) ;
30
+ console . log ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
31
+ yield channel . assertExchange ( amqpExchange , 'topic' ) ;
32
+ }
33
+ console . log ( 'Publishing message id=%s' , msg . id ) ;
34
+ let encryptedData = encryptor . encryptMessageContent ( {
35
+ id : msg . id ,
36
+ body : msg . body ,
37
+ attachments : msg . attachments
25
38
} ) ;
26
-
27
- console . log ( 'New request bin created bin=%j' , bin ) ;
28
-
29
- this . emit ( 'data' , eioUtils . newMessageWithBody ( bin ) ) ;
30
-
31
- console . log ( 'Processing completed' ) ;
32
-
39
+ channel . publish ( amqpExchange , 'foo' , encryptedData ) ;
40
+ console . log ( 'Message published id=%s' , msg . id ) ;
41
+ yield channel . waitForConfirms ( ) ;
42
+ console . log ( 'Message publishing confirmed id=%s' , msg . id ) ;
43
+ this . emit ( 'data' , msg ) ;
33
44
this . emit ( 'end' ) ;
34
45
} . bind ( this ) ) . catch ( err => {
35
46
console . log ( 'Error occurred' , err . stack || err ) ;
0 commit comments