Commit ba57a43bf6d04e74ed5220d16dc089609ce83dd7

Authored by Adhidarma Hadiwinoto
1 parent 94007c6eff
Exists in master

rapihkan beberapa hal

Showing 2 changed files with 7 additions and 1 deletions Inline Diff

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