Commit 057c6624635feb9754506057061c8eac7e464e31

Authored by Adhidarma Hadiwinoto
1 parent 30064ae0a7
Exists in master

Add global.KOMODO_SDK_DISABLE_PULL

Showing 1 changed file with 30 additions and 24 deletions Side-by-side Diff

  1 +const MODULE_NAME = 'KOMODO-SDK.PULL';
  2 +
1 3 const IS_DEBUG = process.env.KOMODO_SDK_DEBUG_PULL;
2 4  
3 5 const request = require('request');
... ... @@ -60,7 +62,7 @@ function setPartner(_partner) {
60 62 function pullTask() {
61 63 if (isPaused()) {
62 64 if (IS_DEBUG) {
63   - logger.verbose('PULL TASK paused')
  65 + logger.verbose(`${MODULE_NAME} 76370FE5: PULL TASK paused`);
64 66 }
65 67 return;
66 68 }
... ... @@ -71,7 +73,7 @@ function pullTask() {
71 73  
72 74 if (!partner) {
73 75 if (IS_DEBUG) {
74   - logger.verbose('PULL TASK disabled because of undefined partner')
  76 + logger.verbose(`${MODULE_NAME} FFB54A2A: PULL TASK disabled because of undefined partner`);
75 77 }
76 78  
77 79 return;
... ... @@ -79,7 +81,7 @@ function pullTask() {
79 81  
80 82 if (matrix && matrix.not_ready) {
81 83 if (IS_DEBUG) {
82   - logger.verbose('PULL TASK paused because of gateway is not ready')
  84 + logger.verbose(`${MODULE_NAME} 68BDA23B: PULL TASK paused because of gateway is not ready`)
83 85 }
84 86 return;
85 87 }
... ... @@ -94,14 +96,14 @@ function pullTask() {
94 96 }
95 97  
96 98 if (!core_pull_task_url) {
97   - logger.warn('Unknown CORE task url');
  99 + logger.warn(`${MODULE_NAME} 5F0681B7: Unknown CORE task url`);
98 100 return;
99 101 }
100 102  
101 103 //if (config.pulltask_mutex && pullTaskLocked) {
102 104 if (pullTaskLocked) {
103 105 if (IS_DEBUG) {
104   - logger.verbose('PULL TASK paused because LOCKED')
  106 + logger.verbose(`${MODULE_NAME} B81F0CCD: PULL TASK paused because LOCKED`);
105 107 }
106 108 return;
107 109 }
... ... @@ -126,21 +128,21 @@ function pullTask() {
126 128 if (config.pull_task_use_post) {
127 129 //logger.verbose('Requesting PULL-TASK to CORE using POST');
128 130 if (IS_DEBUG) {
129   - logger.verbose('PULL TASK using HTTP POST');
  131 + logger.verbose(`${MODULE_NAME} CB855B30: PULL TASK using HTTP POST`);
130 132 }
131 133 options.method = 'POST';
132 134 options.form = body_or_qs;
133 135 }
134 136 else {
135 137 if (IS_DEBUG) {
136   - logger.verbose('PULL TASK using HTTP GET');
  138 + logger.verbose(`${MODULE_NAME} BA2EF935: PULL TASK using HTTP GET`);
137 139 }
138 140 options.method = 'GET';
139 141 options.qs = body_or_qs;
140 142 }
141 143  
142 144 if (config && config.debug_request_task_to_core) {
143   - logger.verbose('Requesting task to CORE', {url: options.url, method: options.method, body_or_qs: body_or_qs});
  145 + logger.verbose(`${MODULE_NAME} 0642E25C: Requesting task to CORE`, {url: options.url, method: options.method, body_or_qs: body_or_qs});
144 146 }
145 147  
146 148 const start_time = new Date();
... ... @@ -150,12 +152,12 @@ function pullTask() {
150 152 const lame_limit = 10 * 1000;
151 153 const delta_time = new Date() - start_time;
152 154 if (delta_time > lame_limit) {
153   - logger.warn('LAME-PULL: PULL response from CORE exceeds ' + lame_limit + ' secs', {delta_time: delta_time});
  155 + logger.warn(`${MODULE_NAME} B892DC43: LAME-PULL: PULL response from CORE exceeds ` + lame_limit + ' secs', {delta_time: delta_time});
154 156 }
155 157  
156 158 if (error) {
157 159 if (matrix.core_is_healthy) {
158   - logger.warn('Error pulling task from CORE', {error: error});
  160 + logger.warn(`${MODULE_NAME} FB762F4A: Error pulling task from CORE`, {error: error});
159 161 }
160 162 matrix.core_is_healthy = false;
161 163 onNoTask();
... ... @@ -164,7 +166,7 @@ function pullTask() {
164 166  
165 167 if (response.statusCode != 200) {
166 168 if (matrix.core_is_healthy) {
167   - logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode});
  169 + logger.warn(`${MODULE_NAME} 8943EECB: CORE http response status code for pull task is not 200`, {http_response_status: response.statusCode});
168 170 }
169 171 matrix.core_is_healthy = false;
170 172 onNoTask();
... ... @@ -172,7 +174,7 @@ function pullTask() {
172 174 }
173 175  
174 176 if (!matrix.core_is_healthy) {
175   - logger.verbose('CORE is healthy');
  177 + logger.verbose(`${MODULE_NAME} 099F5B3C: CORE is healthy`);
176 178 }
177 179 matrix.core_is_healthy = true;
178 180  
... ... @@ -241,12 +243,12 @@ function forwardCoreTaskToPartner(coreMessage, start_time) {
241 243 task = JSON.parse(coreMessage);
242 244 }
243 245 catch(e) {
244   - logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e});
  246 + logger.warn(`${MODULE_NAME} E757F11A: Exception on parsing CORE pull task response`, { coreMessage, eCode: e.code, eMessage: e.message });
245 247 return;
246 248 }
247 249  
248 250 if (config.sdk_pull_only_postpaid) {
249   - logger.warn('Got task on sdk_pull_only_postpaid. It should not be happens', { task });
  251 + logger.warn(`${MODULE_NAME} E6662C4F: Got task on sdk_pull_only_postpaid. It should not be happens`, { task });
250 252 report({
251 253 trx_id: task.trx_id,
252 254 rc: '40',
... ... @@ -269,7 +271,7 @@ function forwardCoreTaskToPartner(coreMessage, start_time) {
269 271  
270 272 const created_ts = new Date(task.created);
271 273 const queue_time = ((new Date()) - created_ts) / 1000;
272   - logger.info('Got task from CORE', {trx_id: task.trx_id, destination: task.destination, product: task.product, queue_time: queue_time, core_pull_request_time: core_pull_request_time});
  274 + logger.info(`${MODULE_NAME} 7F131334: Got task from CORE`, { trx_id: task.trx_id, destination: task.destination, product: task.product, queue_time: queue_time, core_pull_request_time: core_pull_request_time });
273 275  
274 276 taskArchive.get(task, function(res) {
275 277 if (res && partner.advice) {
... ... @@ -304,7 +306,7 @@ function report(data, xid) {
304 306 }
305 307  
306 308 if (!core_pull_report_url) {
307   - logger.warn('Unknown CORE report url');
  309 + logger.warn(`${MODULE_NAME} C23CC601: Unknown CORE report url`);
308 310 return;
309 311 }
310 312  
... ... @@ -319,7 +321,7 @@ function report(data, xid) {
319 321  
320 322 let trx_id = Number(data.trx_id) - (Number(config.sdk_trx_id_adder) || 0);
321 323 if (trx_id <= 0) {
322   - logger.warn('REPORT: calculated trx_id is a negative number, using uncalculated trx_id', {uncalculated: data.trx_id, calculated: trx_id, sdk_trx_id_adder: config.sdk_trx_id_adder});
  324 + logger.warn(`${MODULE_NAME} 6A8C7303: REPORT: calculated trx_id is a negative number, using uncalculated trx_id`, {uncalculated: data.trx_id, calculated: trx_id, sdk_trx_id_adder: config.sdk_trx_id_adder});
323 325 trx_id = data.trx_id;
324 326 }
325 327  
... ... @@ -343,29 +345,29 @@ function report(data, xid) {
343 345 }
344 346  
345 347 if (!config.do_not_verbose_log_report) {
346   - logger.verbose('Report to CORE using HTTP POST', { xid });
  348 + logger.verbose(`${MODULE_NAME} 2110168C: Report to CORE using HTTP POST`, { xid });
347 349 }
348 350  
349 351 request.post(options, function(error, response) {
350 352 if (error) {
351   - logger.warn('Error reporting to CORE', { xid, error });
  353 + logger.warn(`${MODULE_NAME} B1CA595F: Error reporting to CORE`, { xid, error });
352 354 resendReport(data);
353 355 }
354 356 else if (response.statusCode != 200) {
355   - logger.warn('Error reporting to CORE, http response status is not 200', {
  357 + logger.warn(`${MODULE_NAME} 4B73BD23: Error reporting to CORE, http response status is not 200`, {
356 358 xid, requestOptions: options, http_response_status: response.statusCode,
357 359 });
358 360 resendReport(data);
359 361 }
360 362 else if (!config.do_not_verbose_log_report) {
361   - logger.verbose('Report has been sent to CORE', { xid, requestOptions: options });
  363 + logger.verbose(`${MODULE_NAME} 379A25AA: Report has been sent to CORE`, { xid, requestOptions: options });
362 364 }
363 365 });
364 366 }
365 367  
366 368 function resendReport(data) {
367 369 const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS)
368   - logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms')
  370 + logger.verbose(`${MODULE_NAME} DEE44715: Resend report to CORE in ${sleepBeforeResend} ms`);
369 371  
370 372 setTimeout(
371 373 function() {
... ... @@ -404,8 +406,12 @@ function getRemoteProduct(product) {
404 406  
405 407 initMatrix();
406 408  
407   -setInterval(pullTask, config.pull_interval_ms || 1000);
408   -logger.verbose('Pull task every ' + (config.pull_interval_ms || 1000) + ' ms');
  409 +if (!global.KOMODO_SDK_DISABLE_PULL) {
  410 + setInterval(pullTask, config.pull_interval_ms || 1000);
  411 + logger.verbose(`${MODULE_NAME} B6AEB920: Pull task every ${config.pull_interval_ms || 1000} ms`);
  412 +} else {
  413 + logger.info(`${MODULE_NAME} DEC80C55: Pull task disabled because of global.KOMODO_SDK_DISABLE_PULL flag`);
  414 +}
409 415  
410 416 exports.setPartner = setPartner;
411 417 exports.isPaused = isPaused;