Commit cce03c1feb3a7526b720bd50a6676df81aec8fff

Authored by Adhidarma Hadiwinoto
1 parent ba57a43bf6
Exists in master

healthy core status

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