@@ -3,9 +3,29 @@ const co = require('co');
3
3
const amqp = require ( 'amqplib' ) ;
4
4
const encryptor = require ( '../encryptor.js' ) ;
5
5
6
- var conn , channel ;
6
+ let channel ;
7
7
8
8
module . exports . process = processAction ;
9
+ module . exports . init = init ;
10
+
11
+ /**
12
+ * This methdo will be called from elastic.io platform on initialization
13
+ *
14
+ * @param cfg
15
+ */
16
+ function init ( cfg ) {
17
+ console . log ( 'Starting initialization, cfg=%j' , cfg ) ;
18
+ const amqpURI = cfg . amqpURI ;
19
+ const amqpExchange = cfg . topic ;
20
+ return co ( function * gen ( ) {
21
+ console . log ( 'Connecting to amqpURI=%s' , amqpURI ) ;
22
+ const conn = yield amqp . connect ( amqpURI ) ;
23
+ console . log ( 'Creating a confirm channel' ) ;
24
+ channel = yield conn . createConfirmChannel ( ) ;
25
+ console . log ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
26
+ yield channel . assertExchange ( amqpExchange , 'topic' ) ;
27
+ } ) ;
28
+ }
9
29
10
30
/**
11
31
* This method will be called from elastic.io platform providing following data
@@ -14,42 +34,20 @@ module.exports.process = processAction;
14
34
* @param cfg configuration that is account information and configuration field values
15
35
*/
16
36
function processAction ( msg , cfg ) {
17
- console . log ( 'Action started' ) ;
18
- const amqpURI = cfg . amqpURI ;
19
37
const amqpExchange = cfg . topic ;
20
- co ( function * ( ) {
21
- if ( ! conn ) {
22
- console . log ( 'Connecting to amqpURI=%s' , amqpURI ) ;
23
- conn = yield amqp . connect ( amqpURI ) ;
24
- }
25
- if ( ! channel ) {
26
- console . log ( 'Creating a confirm channel' ) ;
27
- channel = yield conn . createConfirmChannel ( ) ;
28
- console . log ( 'Asserting topic exchange exchange=%s' , amqpExchange ) ;
29
- yield channel . assertExchange ( amqpExchange , 'topic' ) ;
30
- }
38
+ return co ( function * sendMessage ( ) {
31
39
console . log ( 'Publishing message id=%s' , msg . id ) ;
32
40
let encryptedData = encryptor . encryptMessageContent ( {
33
41
body : msg . body . payload || msg . body ,
34
42
attachments : msg . attachments
35
43
} ) ;
36
- console . log ( encryptedData , encryptor . decryptMessageContent ( encryptedData ) ) ;
37
- channel . publish ( amqpExchange , 'foo' , encryptedData , {
44
+ channel . publish ( amqpExchange , msg . body . routingKey || '' , encryptedData , {
38
45
contentType : "application/octet-stream" ,
39
46
messageId : msg . id
40
47
} ) ;
41
48
console . log ( 'Message published id=%s' , msg . id ) ;
42
49
yield channel . waitForConfirms ( ) ;
43
50
console . log ( 'Message publishing confirmed id=%s' , msg . id ) ;
44
51
this . emit ( 'data' , msg ) ;
45
- this . emit ( 'end' ) ;
46
- console . log ( 'Closing the channel' ) ;
47
- yield channel . close ( ) ;
48
- console . log ( 'Closing the connnection' ) ;
49
- yield conn . close ( ) ;
50
- } . bind ( this ) ) . catch ( err => {
51
- console . log ( 'Error occurred' , err . stack || err ) ;
52
- this . emit ( 'error' , err ) ;
53
- this . emit ( 'end' ) ;
54
- } ) ;
52
+ } . bind ( this ) ) ;
55
53
}
0 commit comments