Commit 5b529cbb014143d3fc2efc4e61bc57b09e207fbf

Authored by Adhidarma Hadiwinoto
1 parent cce03c1feb
Exists in master

replace by apikey

Showing 1 changed file with 2 additions and 2 deletions Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 const request = require('request'); 3 const request = require('request');
4 const logger = require('./logger').get(); 4 const logger = require('./logger').get();
5 5
6 var config; 6 var config;
7 var matrix; 7 var matrix;
8 var partner; 8 var partner;
9 9
10 function init(options) { 10 function init(options) {
11 config = options.config; 11 config = options.config;
12 matrix = options.matrix; 12 matrix = options.matrix;
13 partner = options.partner; 13 partner = options.partner;
14 14
15 initMatrix(); 15 initMatrix();
16 16
17 setInterval(pullTask, config.pull_interval_ms || 1000); 17 setInterval(pullTask, config.pull_interval_ms || 1000);
18 } 18 }
19 19
20 function pullTask() { 20 function pullTask() {
21 let options = { 21 let options = {
22 url: config.pull_url.task, 22 url: config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey),
23 qs: { 23 qs: {
24 handler: config.handler_name, 24 handler: config.handler_name,
25 products: config.products.join(',') 25 products: config.products.join(',')
26 } 26 }
27 } 27 }
28 28
29 request(options, function(error, response, body) { 29 request(options, function(error, response, body) {
30 if (error) { 30 if (error) {
31 if (matrix.core_is_healthy) { 31 if (matrix.core_is_healthy) {
32 logger.warn('Error pulling task from CORE', {error: error}); 32 logger.warn('Error pulling task from CORE', {error: error});
33 } 33 }
34 matrix.core_is_healthy = false; 34 matrix.core_is_healthy = false;
35 return; 35 return;
36 } 36 }
37 37
38 if (response.statusCode != 200) { 38 if (response.statusCode != 200) {
39 if (matrix.core_is_healthy) { 39 if (matrix.core_is_healthy) {
40 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); 40 logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
41 } 41 }
42 matrix.core_is_healthy = false; 42 matrix.core_is_healthy = false;
43 return; 43 return;
44 } 44 }
45 45
46 if (!matrix.core_is_healthy) { 46 if (!matrix.core_is_healthy) {
47 logger.verbose('CORE is healthy'); 47 logger.verbose('CORE is healthy');
48 } 48 }
49 matrix.core_is_healthy = true; 49 matrix.core_is_healthy = true;
50 50
51 if (body == 'NONE') { 51 if (body == 'NONE') {
52 return; 52 return;
53 } 53 }
54 54
55 forwardCoreTaskToPartner(body); 55 forwardCoreTaskToPartner(body);
56 }); 56 });
57 } 57 }
58 58
59 function forwardCoreTaskToPartner(coreMessage) { 59 function forwardCoreTaskToPartner(coreMessage) {
60 let task; 60 let task;
61 61
62 try { 62 try {
63 task = JSON.parse(coreMessage); 63 task = JSON.parse(coreMessage);
64 } 64 }
65 catch(e) { 65 catch(e) {
66 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); 66 logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
67 } 67 }
68 68
69 task.remote_product = getRemoteProduct(task.product); 69 task.remote_product = getRemoteProduct(task.product);
70 70
71 partner.buy(task); 71 partner.buy(task);
72 } 72 }
73 73
74 function report(trx_id, rc, message, sn) { 74 function report(trx_id, rc, message, sn) {
75 let options = { 75 let options = {
76 url: config.pull_url.report, 76 url: config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey),
77 qs: { 77 qs: {
78 trx_id: trx_id, 78 trx_id: trx_id,
79 rc: rc, 79 rc: rc,
80 message: message, 80 message: message,
81 handler: config.handler_name 81 handler: config.handler_name
82 } 82 }
83 } 83 }
84 84
85 if (sn) { 85 if (sn) {
86 options.qs.sn = sn; 86 options.qs.sn = sn;
87 } 87 }
88 88
89 request(options, function(error, response, body) { 89 request(options, function(error, response, body) {
90 if (error) { 90 if (error) {
91 logger.warn('Error reporting to CORE', {error: error}); 91 logger.warn('Error reporting to CORE', {error: error});
92 } 92 }
93 else if (response.statusCode != 200) { 93 else if (response.statusCode != 200) {
94 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); 94 logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode});
95 } 95 }
96 else { 96 else {
97 logger.verbose('Report has been sent to CORE', {requestOptions: options}); 97 logger.verbose('Report has been sent to CORE', {requestOptions: options});
98 } 98 }
99 }); 99 });
100 } 100 }
101 101
102 function resendReport(trx_id, rc, message, sn) { 102 function resendReport(trx_id, rc, message, sn) {
103 let sleepBeforeResend = 1000; 103 let sleepBeforeResend = 1000;
104 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') 104 logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
105 105
106 setTimeout( 106 setTimeout(
107 function() { 107 function() {
108 report(trx_id, rc, message, sn); 108 report(trx_id, rc, message, sn);
109 }, 109 },
110 sleepBeforeResend 110 sleepBeforeResend
111 ) 111 )
112 } 112 }
113 113
114 function isPaused() { 114 function isPaused() {
115 return matrix.paused; 115 return matrix.paused;
116 } 116 }
117 117
118 function pause() { 118 function pause() {
119 matrix.paused = true; 119 matrix.paused = true;
120 } 120 }
121 121
122 function resume() { 122 function resume() {
123 matrix.pause = false; 123 matrix.pause = false;
124 } 124 }
125 125
126 function initMatrix() { 126 function initMatrix() {
127 if (!matrix) { 127 if (!matrix) {
128 matrix = {}; 128 matrix = {};
129 } 129 }
130 130
131 matrix.counter = { 131 matrix.counter = {
132 trx: 0 132 trx: 0
133 } 133 }
134 } 134 }
135 135
136 function incrementCounterTrx() { 136 function incrementCounterTrx() {
137 matrix.counter.trx++; 137 matrix.counter.trx++;
138 } 138 }
139 139
140 function getRemoteProduct(product) { 140 function getRemoteProduct(product) {
141 let remoteProduct = config.remote_products[product]; 141 let remoteProduct = config.remote_products[product];
142 return remoteProduct || product; 142 return remoteProduct || product;
143 } 143 }
144 144
145 exports.init = init; 145 exports.init = init;
146 exports.isPaused = isPaused; 146 exports.isPaused = isPaused;
147 exports.pause = pause; 147 exports.pause = pause;
148 exports.resume = resume; 148 exports.resume = resume;
149 exports.report = report; 149 exports.report = report;
150 150