Compare View
Commits (2)
Changes
Showing 2 changed files Inline Diff
gateway/pull.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const request = require('request'); | 3 | const request = require('request'); |
4 | 4 | ||
5 | const config = require('../config'); | 5 | const config = require('../config'); |
6 | const logger = require('../logger'); | 6 | const logger = require('../logger'); |
7 | const matrix = require('../matrix'); | 7 | const matrix = require('../matrix'); |
8 | const controlPanel = require('../control-panel'); | 8 | const controlPanel = require('../control-panel'); |
9 | const heartbeat = require('../heartbeat'); | 9 | const heartbeat = require('../heartbeat'); |
10 | 10 | ||
11 | const taskArchive = require('./task-archive'); | 11 | const taskArchive = require('./task-archive'); |
12 | 12 | ||
13 | const MAX_SLEEP_BEFORE_RESEND_MS = 500; | 13 | const MAX_SLEEP_BEFORE_RESEND_MS = 500; |
14 | 14 | ||
15 | if (config.handler_name) { | 15 | if (config.handler_name) { |
16 | process.title = "KOMODO-GW@" + config.handler_name; | 16 | process.title = "KOMODO-GW@" + config.handler_name; |
17 | } | 17 | } |
18 | 18 | ||
19 | if (!matrix.pending_tasks) { | ||
20 | matrix.sdk_pending_tasks = []; | ||
21 | } | ||
22 | |||
23 | if (!matrix.active_tasks) { | ||
24 | matrix.sdk_unresponsed_tasks = []; | ||
25 | } | ||
26 | |||
19 | if (!matrix.pending_tasks) { | 27 | heartbeat.setModuleType('gateway'); |
20 | matrix.sdk_pending_tasks = []; | 28 | |
21 | } | 29 | var partner; |
22 | 30 | ||
23 | if (!matrix.active_tasks) { | 31 | function setPartner(_partner) { |
24 | matrix.sdk_unresponsed_tasks = []; | 32 | partner = _partner; |
25 | } | 33 | } |
26 | 34 | ||
27 | heartbeat.setModuleType('gateway'); | 35 | function pullTask() { |
28 | 36 | if (!partner) { | |
29 | var partner; | 37 | return; |
30 | 38 | } | |
31 | function setPartner(_partner) { | 39 | |
32 | partner = _partner; | 40 | let core_pull_task_url; |
33 | } | 41 | |
34 | 42 | if (config.core_url) { | |
35 | function pullTask() { | 43 | core_pull_task_url = config.core_url + '/pull/task'; |
36 | if (!partner) { | 44 | } else if (config.pull_url.task) { |
37 | return; | 45 | core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); |
38 | } | 46 | } |
39 | 47 | ||
40 | let core_pull_task_url; | 48 | if (!core_pull_task_url) { |
41 | 49 | logger.warn('Unknown CORE task url'); | |
42 | if (config.core_url) { | 50 | return; |
43 | core_pull_task_url = config.core_url + '/pull/task'; | 51 | } |
44 | } else if (config.pull_url.task) { | 52 | |
45 | core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); | 53 | let options = { |
46 | } | 54 | url: core_pull_task_url, |
47 | 55 | qs: { | |
48 | if (!core_pull_task_url) { | 56 | handler: config.handler_name, |
49 | logger.warn('Unknown CORE task url'); | 57 | products: config.products.join(',') |
50 | return; | 58 | } |
51 | } | 59 | } |
52 | 60 | ||
53 | let options = { | 61 | request(options, function(error, response, body) { |
54 | url: core_pull_task_url, | 62 | if (error) { |
55 | qs: { | 63 | if (matrix.core_is_healthy) { |
56 | handler: config.handler_name, | 64 | logger.warn('Error pulling task from CORE', {error: error}); |
57 | products: config.products.join(',') | 65 | } |
58 | } | 66 | matrix.core_is_healthy = false; |
59 | } | 67 | return; |
60 | 68 | } | |
61 | request(options, function(error, response, body) { | 69 | |
62 | if (error) { | 70 | if (response.statusCode != 200) { |
63 | if (matrix.core_is_healthy) { | 71 | if (matrix.core_is_healthy) { |
64 | logger.warn('Error pulling task from CORE', {error: error}); | 72 | logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); |
65 | } | 73 | } |
66 | matrix.core_is_healthy = false; | 74 | matrix.core_is_healthy = false; |
67 | return; | 75 | return; |
68 | } | 76 | } |
69 | 77 | ||
70 | if (response.statusCode != 200) { | 78 | if (!matrix.core_is_healthy) { |
71 | if (matrix.core_is_healthy) { | 79 | logger.verbose('CORE is healthy'); |
72 | logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); | 80 | } |
73 | } | 81 | matrix.core_is_healthy = true; |
74 | matrix.core_is_healthy = false; | 82 | |
75 | return; | 83 | if (body == 'NONE') { |
76 | } | 84 | return; |
77 | 85 | } | |
78 | if (!matrix.core_is_healthy) { | 86 | |
79 | logger.verbose('CORE is healthy'); | 87 | forwardCoreTaskToPartner(body); |
80 | } | 88 | }); |
81 | matrix.core_is_healthy = true; | 89 | } |
82 | 90 | ||
91 | function putTaskToMatrix(task) { | ||
92 | if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) { | ||
93 | matrix.sdk_unresponsed_tasks.push(task.trix_id); | ||
94 | } | ||
95 | |||
96 | if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) { | ||
97 | matrix.sdk_pending_tasks.push(task.trx_id); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | function updateTaskOnMatrix(trx_id, rc) { | ||
102 | const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id); | ||
103 | if (unresponsed_task_idx >= 0) { | ||
104 | matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1); | ||
105 | } | ||
106 | |||
107 | if (rc !== '68') { | ||
108 | return; | ||
109 | } | ||
110 | |||
111 | const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id); | ||
112 | if (pending_task_idx >= 0) { | ||
113 | matrix.sdk_pending_tasks.splice(pending_task_idx, 1); | ||
114 | } | ||
115 | } | ||
116 | |||
83 | if (body == 'NONE') { | 117 | function forwardCoreTaskToPartner(coreMessage) { |
84 | return; | 118 | let task; |
85 | } | 119 | |
86 | 120 | try { | |
87 | forwardCoreTaskToPartner(body); | 121 | task = JSON.parse(coreMessage); |
88 | }); | 122 | } |
89 | } | 123 | catch(e) { |
90 | 124 | logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); | |
91 | function putTaskToMatrix(task) { | 125 | } |
92 | if (matrix.sdk_unresponsed_tasks.indexOf(task.trx_id) < 0) { | 126 | |
93 | matrix.sdk_unresponsed_tasks.push(task.trix_id); | 127 | incrementCounterTrx(); |
94 | } | 128 | |
95 | 129 | task.remote_product = getRemoteProduct(task.product); | |
96 | if (matrix.sdk_pending_tasks.indexOf(task.trx_id) < 0) { | 130 | |
131 | putTaskToMatrix(task); | ||
132 | |||
97 | matrix.sdk_pending_tasks.push(task.trx_id); | 133 | taskArchive.get(task, function(res) { |
98 | } | 134 | if (res && partner.advice) { |
99 | } | 135 | partner.advice(task); |
100 | 136 | } | |
101 | function updateTaskOnMatrix(trx_id, rc) { | 137 | else { |
102 | const unresponsed_task_idx = matrix.sdk_unresponsed_tasks.indexOf(trx_id); | 138 | partner.buy(task); |
103 | if (unresponsed_task_idx >= 0) { | 139 | } |
104 | matrix.sdk_unresponsed_tasks.splice(unresponsed_task_idx, 1); | 140 | }); |
105 | } | ||
106 | |||
107 | if (rc !== '68') { | 141 | } |
108 | return; | 142 | |
109 | } | 143 | function replaceRc(original_rc) { |
110 | 144 | if (!config || !config.replace_rc || !config.replace_rc.length) { | |
111 | const pending_task_idx = matrix.sdk_pending_tasks.indexOf(trx_id); | 145 | return original_rc; |
112 | if (pending_task_idx >= 0) { | 146 | } |
113 | matrix.sdk_pending_tasks.splice(pending_task_idx, 1); | 147 | |
114 | } | 148 | return config.replace_rc[original_rc] || original_rc; |
115 | } | 149 | } |
116 | 150 | ||
117 | function forwardCoreTaskToPartner(coreMessage) { | 151 | function report(data) { |
118 | let task; | 152 | |
119 | 153 | let core_pull_report_url; | |
120 | try { | 154 | |
155 | if (data && data.trx_id && data.rc) { | ||
156 | updateTaskOnMatrix(data.trx_id, data.rc); | ||
157 | } | ||
158 | |||
121 | task = JSON.parse(coreMessage); | 159 | if (config.core_url) { |
122 | } | 160 | core_pull_report_url = config.core_url + '/pull/report'; |
123 | catch(e) { | 161 | } else if (config.pull_url.report) { |
124 | logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); | 162 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); |
125 | } | 163 | } |
126 | 164 | ||
127 | incrementCounterTrx(); | 165 | if (!core_pull_report_url) { |
128 | 166 | logger.warn('Unknown CORE report url'); | |
129 | task.remote_product = getRemoteProduct(task.product); | 167 | return; |
130 | 168 | } | |
131 | putTaskToMatrix(task); | 169 | |
132 | 170 | if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) { | |
133 | taskArchive.get(task, function(res) { | 171 | if (!data.misc) { |
134 | if (res && partner.advice) { | 172 | data.misc = {}; |
135 | partner.advice(task); | 173 | } |
136 | } | 174 | |
137 | else { | 175 | logger.verbose('Including advice url on report'); |
138 | partner.buy(task); | 176 | |
139 | } | 177 | data.misc.advice_url = config.push_server.advice.url; |
140 | }); | 178 | } |
141 | } | 179 | |
142 | 180 | let options = { | |
143 | function replaceRc(original_rc) { | 181 | url: core_pull_report_url, |
144 | if (!config || !config.replace_rc || !config.replace_rc.length) { | 182 | form: { |
145 | return original_rc; | 183 | trx_id: data.trx_id, |
146 | } | 184 | rc: replaceRc(data.rc), |
147 | 185 | message: data.message, | |
148 | return config.replace_rc[original_rc] || original_rc; | 186 | handler: config.handler_name, |
149 | } | 187 | sn: data.sn, |
150 | 188 | amount: data.amount, | |
151 | function report(data) { | 189 | raw: data.raw, |
152 | 190 | misc: data.misc | |
153 | let core_pull_report_url; | 191 | } |
154 | 192 | } | |
155 | if (data && data.trx_id && data.rc) { | 193 | |
156 | updateTaskOnMatrix(data.trx_id, data.rc); | 194 | if (!config.do_not_verbose_log_report) { |
157 | } | 195 | logger.verbose('Report to CORE using HTTP POST'); |
158 | 196 | } | |
159 | if (config.core_url) { | 197 | |
160 | core_pull_report_url = config.core_url + '/pull/report'; | 198 | request.post(options, function(error, response, body) { |
161 | } else if (config.pull_url.report) { | 199 | if (error) { |
162 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); | 200 | logger.warn('Error reporting to CORE', {error: error}); |
163 | } | 201 | resendReport(data); |
164 | 202 | } | |
165 | if (!core_pull_report_url) { | 203 | else if (response.statusCode != 200) { |
166 | logger.warn('Unknown CORE report url'); | 204 | logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); |
167 | return; | 205 | resendReport(data); |
168 | } | 206 | } |
169 | 207 | else if (!config.do_not_verbose_log_report) { | |
170 | if (config && config.push_server && config.push_server.apikey && config.push_server.advice && config.push_server.advice.url && config.push_server.advice.port) { | 208 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); |
171 | if (!data.misc) { | 209 | } |
172 | data.misc = {}; | 210 | }); |
173 | } | 211 | } |
174 | 212 | ||
175 | logger.verbose('Including advice url on report'); | 213 | function resendReport(data) { |
176 | 214 | const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS) | |
177 | data.misc.advice_url = config.push_server.advice.url; | 215 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') |
178 | } | 216 | |
179 | 217 | setTimeout( | |
180 | let options = { | 218 | function() { |
181 | url: core_pull_report_url, | 219 | report(data); |
182 | form: { | 220 | }, |
183 | trx_id: data.trx_id, | 221 | sleepBeforeResend |
184 | rc: replaceRc(data.rc), | 222 | ) |
185 | message: data.message, | 223 | } |
186 | handler: config.handler_name, | 224 | |
187 | sn: data.sn, | 225 | function isPaused() { |
188 | amount: data.amount, | 226 | return matrix.paused; |
189 | raw: data.raw, | 227 | } |
190 | misc: data.misc | 228 | |
191 | } | 229 | function pause() { |
192 | } | 230 | matrix.paused = true; |
193 | 231 | } | |
194 | if (!config.do_not_verbose_log_report) { | 232 | |
195 | logger.verbose('Report to CORE using HTTP POST'); | 233 | function resume() { |
196 | } | 234 | matrix.pause = false; |
197 | 235 | } | |
198 | request.post(options, function(error, response, body) { | 236 | |
199 | if (error) { | 237 | function initMatrix() { |
200 | logger.warn('Error reporting to CORE', {error: error}); | 238 | if (!matrix) { |
201 | resendReport(data); | 239 | matrix = {}; |
202 | } | 240 | } |
203 | else if (response.statusCode != 200) { | 241 | |
204 | logger.warn('Error reporting to CORE, http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); | 242 | matrix.counter = { |
205 | resendReport(data); | 243 | trx: 0 |
206 | } | 244 | } |
207 | else if (!config.do_not_verbose_log_report) { | 245 | } |
208 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); | 246 | |
209 | } | 247 | function incrementCounterTrx() { |
210 | }); | 248 | matrix.counter.trx++; |
211 | } | 249 | } |
212 | 250 | ||
213 | function resendReport(data) { | 251 | function getRemoteProduct(product) { |
214 | const sleepBeforeResend = Math.round(Math.random() * MAX_SLEEP_BEFORE_RESEND_MS) | 252 | let remoteProduct = config.remote_products[product]; |
215 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') | 253 | return remoteProduct || product; |
216 | 254 | } | |
217 | setTimeout( | 255 | |
218 | function() { | 256 | initMatrix(); |
219 | report(data); | 257 | setInterval(pullTask, config.pull_interval_ms || 1000); |
220 | }, | 258 | |
221 | sleepBeforeResend | 259 | exports.setPartner = setPartner; |
222 | ) | 260 | exports.isPaused = isPaused; |
223 | } | 261 | exports.pause = pause; |
224 | 262 | exports.resume = resume; | |
225 | function isPaused() { | 263 | exports.report = report; |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.19.4", | 3 | "version": "1.20.0", |
4 | "description": "SDK for Komodo", | 4 | "description": "SDK for Komodo", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "mocha", | 7 | "test": "mocha", |
8 | "postversion": "git push && git push --tags" | 8 | "postversion": "git push && git push --tags" |
9 | }, | 9 | }, |
10 | "repository": { | 10 | "repository": { |
11 | "type": "git", | 11 | "type": "git", |
12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" | 12 | "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" |
13 | }, | 13 | }, |
14 | "keywords": [ | 14 | "keywords": [ |
15 | "ppob", | 15 | "ppob", |
16 | "payment", | 16 | "payment", |
17 | "komodo" | 17 | "komodo" |
18 | ], | 18 | ], |
19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", | 19 | "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", |
20 | "license": "ISC", | 20 | "license": "ISC", |
21 | "dependencies": { | 21 | "dependencies": { |
22 | "basic-auth": "^2.0.0", | 22 | "basic-auth": "^2.0.0", |
23 | "body-parser": "^1.18.2", | 23 | "body-parser": "^1.18.2", |
24 | "express": "^4.16.3", | 24 | "express": "^4.16.3", |
25 | "express-session": "^1.15.6", | 25 | "express-session": "^1.15.6", |
26 | "lru-cache": "^4.1.1", | 26 | "lru-cache": "^4.1.1", |
27 | "macaddress": "^0.2.8", | 27 | "macaddress": "^0.2.8", |
28 | "moment": "^2.19.1", | 28 | "moment": "^2.19.1", |
29 | "node-machine-id": "^1.1.10", | 29 | "node-machine-id": "^1.1.10", |
30 | "node-natural-sort": "^0.8.6", | 30 | "node-natural-sort": "^0.8.6", |
31 | "numeral": "^2.0.6", | 31 | "numeral": "^2.0.6", |
32 | "nunjucks": "^3.0.1", | 32 | "nunjucks": "^3.0.1", |
33 | "redis": "^2.8.0", | 33 | "redis": "^2.8.0", |
34 | "request": "^2.81.0", | 34 | "request": "^2.81.0", |
35 | "sha1": "^1.1.1", | 35 | "sha1": "^1.1.1", |
36 | "simple-git": "^1.80.1", | 36 | "simple-git": "^1.80.1", |
37 | "strftime": "^0.10.0", | 37 | "strftime": "^0.10.0", |
38 | "uniqid": "^4.1.1", | 38 | "uniqid": "^4.1.1", |
39 | "uuid": "^3.1.0", | 39 | "uuid": "^3.1.0", |
40 | "winston": "^2.3.1", | 40 | "winston": "^2.3.1", |
41 | "winston-circular-buffer": "^1.0.0", | 41 | "winston-circular-buffer": "^1.0.0", |
42 | "winston-daily-rotate-file": "^1.4.6" | 42 | "winston-daily-rotate-file": "^1.4.6" |
43 | } | 43 | } |
44 | } | 44 | } |
45 | 45 |