Compare View
Commits (5)
Changes
Showing 3 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 | heartbeat.setModuleType('gateway'); | ||
12 | |||
11 | heartbeat.setModuleType('gateway'); | 13 | var partner; |
12 | 14 | ||
13 | var partner; | 15 | function setPartner(_partner) { |
14 | 16 | partner = _partner; | |
15 | function setPartner(_partner) { | 17 | } |
16 | partner = _partner; | 18 | |
17 | } | 19 | function pullTask() { |
18 | 20 | if (!partner) { | |
19 | function pullTask() { | 21 | return; |
20 | if (!partner) { | 22 | } |
21 | return; | 23 | |
22 | } | 24 | let core_pull_task_url; |
23 | 25 | ||
24 | let core_pull_task_url; | 26 | if (config.core_url) { |
25 | 27 | core_pull_task_url = config.core_url + '/pull/task'; | |
26 | if (config.core_url) { | 28 | } else if (config.pull_url.task) { |
27 | core_pull_task_url = config.core_url + '/pull/task'; | 29 | core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); |
28 | } else if (config.pull_url.task) { | 30 | } |
29 | core_pull_task_url = config.pull_url.task.replace('<CORE_APIKEY>', config.core_apikey); | 31 | |
30 | } | 32 | if (!core_pull_task_url) { |
31 | 33 | logger.warn('Unknown CORE task url'); | |
32 | if (!core_pull_task_url) { | 34 | return; |
33 | logger.warn('Unknown CORE task url'); | 35 | } |
34 | return; | 36 | |
35 | } | 37 | let options = { |
36 | 38 | url: core_pull_task_url, | |
37 | let options = { | 39 | qs: { |
38 | url: core_pull_task_url, | 40 | handler: config.handler_name, |
39 | qs: { | 41 | products: config.products.join(',') |
40 | handler: config.handler_name, | 42 | } |
41 | products: config.products.join(',') | 43 | } |
42 | } | 44 | |
43 | } | 45 | request(options, function(error, response, body) { |
44 | 46 | if (error) { | |
45 | request(options, function(error, response, body) { | 47 | if (matrix.core_is_healthy) { |
46 | if (error) { | 48 | logger.warn('Error pulling task from CORE', {error: error}); |
47 | if (matrix.core_is_healthy) { | 49 | } |
48 | logger.warn('Error pulling task from CORE', {error: error}); | 50 | matrix.core_is_healthy = false; |
49 | } | 51 | return; |
50 | matrix.core_is_healthy = false; | 52 | } |
51 | return; | 53 | |
52 | } | 54 | if (response.statusCode != 200) { |
53 | 55 | if (matrix.core_is_healthy) { | |
54 | if (response.statusCode != 200) { | 56 | logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); |
55 | if (matrix.core_is_healthy) { | 57 | } |
56 | logger.warn('CORE http response status code for pull task is not 200', {http_response_status: response.statusCode}); | 58 | matrix.core_is_healthy = false; |
57 | } | 59 | return; |
58 | matrix.core_is_healthy = false; | 60 | } |
59 | return; | 61 | |
60 | } | 62 | if (!matrix.core_is_healthy) { |
61 | 63 | logger.verbose('CORE is healthy'); | |
62 | if (!matrix.core_is_healthy) { | 64 | } |
63 | logger.verbose('CORE is healthy'); | 65 | matrix.core_is_healthy = true; |
64 | } | 66 | |
65 | matrix.core_is_healthy = true; | 67 | if (body == 'NONE') { |
66 | 68 | return; | |
67 | if (body == 'NONE') { | 69 | } |
68 | return; | 70 | |
69 | } | 71 | forwardCoreTaskToPartner(body); |
70 | 72 | }); | |
71 | forwardCoreTaskToPartner(body); | 73 | } |
72 | }); | 74 | |
73 | } | 75 | function forwardCoreTaskToPartner(coreMessage) { |
74 | 76 | let task; | |
75 | function forwardCoreTaskToPartner(coreMessage) { | 77 | |
76 | let task; | 78 | try { |
77 | 79 | task = JSON.parse(coreMessage); | |
78 | try { | 80 | } |
79 | task = JSON.parse(coreMessage); | 81 | catch(e) { |
80 | } | 82 | logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); |
81 | catch(e) { | 83 | } |
82 | logger.warn('Exception on parsing CORE pull task response', {coreMessage: coreMessage, error: e}); | 84 | |
83 | } | 85 | task.remote_product = getRemoteProduct(task.product); |
84 | 86 | ||
85 | task.remote_product = getRemoteProduct(task.product); | 87 | partner.buy(task); |
86 | 88 | } | |
87 | partner.buy(task); | 89 | |
88 | } | 90 | function report(data) { |
89 | 91 | reportUsingHttpPost(data); | |
90 | function report(data) { | 92 | } |
91 | reportUsingHttpPost(data); | 93 | |
92 | } | 94 | function reportUsingHttpPost(data) { |
93 | 95 | ||
94 | function reportUsingHttpPost(data) { | 96 | let core_pull_report_url; |
95 | 97 | ||
96 | let core_pull_report_url; | 98 | if (config.core_url) { |
97 | 99 | core_pull_report_url = config.core_url + '/pull/report'; | |
98 | if (config.core_url) { | 100 | } else if (config.pull_url.report) { |
99 | core_pull_report_url = config.core_url + '/pull/report'; | 101 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); |
100 | } else if (config.pull_url.report) { | 102 | } |
101 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); | 103 | |
102 | } | 104 | if (!core_pull_report_url) { |
103 | 105 | logger.warn('Unknown CORE report url'); | |
104 | if (!core_pull_report_url) { | 106 | return; |
105 | logger.warn('Unknown CORE report url'); | 107 | } |
106 | return; | 108 | |
107 | } | 109 | let options = { |
108 | 110 | url: core_pull_report_url, | |
109 | let options = { | 111 | form: { |
110 | url: core_pull_report_url, | 112 | trx_id: data.trx_id, |
111 | form: { | 113 | rc: data.rc, |
112 | trx_id: data.trx_id, | 114 | message: data.message, |
113 | rc: data.rc, | 115 | handler: config.handler_name, |
114 | message: data.message, | 116 | sn: data.sn, |
115 | handler: config.handler_name, | 117 | amount: data.amount, |
116 | sn: data.sn, | 118 | raw: data.raw, |
117 | amount: data.amount, | 119 | combined: { |
118 | raw: data.raw, | 120 | raw: data.raw |
119 | combined: { | 121 | } |
120 | raw: data.raw | 122 | } |
121 | } | 123 | } |
122 | } | 124 | |
123 | } | 125 | logger.verbose('Report to CORE using HTTP POST'); |
124 | 126 | request.post(options, function(error, response, body) { | |
125 | logger.verbose('Report to CORE using HTTP POST'); | 127 | if (error) { |
126 | request.post(options, function(error, response, body) { | 128 | logger.warn('Error reporting to CORE', {error: error}); |
127 | if (error) { | 129 | } |
128 | logger.warn('Error reporting to CORE', {error: error}); | 130 | else if (response.statusCode != 200) { |
129 | } | 131 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); |
130 | else if (response.statusCode != 200) { | 132 | } |
131 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); | 133 | else { |
132 | } | 134 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); |
133 | else { | 135 | } |
134 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); | 136 | }); |
135 | } | 137 | } |
136 | }); | 138 | |
137 | } | 139 | function reportUsingHttpGet(data) { |
138 | 140 | let core_pull_report_url; | |
139 | function reportUsingHttpGet(data) { | 141 | |
140 | let core_pull_report_url; | 142 | if (config.core_url) { |
141 | 143 | core_pull_report_url = config.core_url + '/pull/report'; | |
142 | if (config.core_url) { | 144 | } else if (config.pull_url.report) { |
143 | core_pull_report_url = config.core_url + '/pull/report'; | 145 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); |
144 | } else if (config.pull_url.report) { | 146 | } |
145 | core_pull_report_url = config.pull_url.report.replace('<CORE_APIKEY>', config.core_apikey); | 147 | |
146 | } | 148 | if (!core_pull_report_url) { |
147 | 149 | logger.warn('Unknown CORE report url'); | |
148 | if (!core_pull_report_url) { | 150 | return; |
149 | logger.warn('Unknown CORE report url'); | 151 | } |
150 | return; | 152 | |
151 | } | 153 | let options = { |
152 | 154 | url: core_pull_report_url, | |
153 | let options = { | 155 | qs: { |
154 | url: core_pull_report_url, | 156 | trx_id: data.trx_id, |
155 | qs: { | 157 | rc: data.rc, |
156 | trx_id: data.trx_id, | 158 | message: data.message, |
157 | rc: data.rc, | 159 | handler: config.handler_name, |
158 | message: data.message, | 160 | sn: data.sn, |
159 | handler: config.handler_name, | 161 | amount: data.amount |
160 | sn: data.sn, | 162 | } |
161 | amount: data.amount | 163 | } |
162 | } | 164 | |
163 | } | 165 | logger.verbose('Report to CORE using HTTP GET'); |
164 | 166 | request(options, function(error, response, body) { | |
165 | logger.verbose('Report to CORE using HTTP GET'); | 167 | if (error) { |
166 | request(options, function(error, response, body) { | 168 | logger.warn('Error reporting to CORE', {error: error}); |
167 | if (error) { | 169 | } |
168 | logger.warn('Error reporting to CORE', {error: error}); | 170 | else if (response.statusCode != 200) { |
169 | } | 171 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); |
170 | else if (response.statusCode != 200) { | 172 | } |
171 | logger.warn('CORE http response status is not 200', {requestOptions: options, http_response_status: response.statusCode}); | 173 | else { |
172 | } | 174 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); |
173 | else { | 175 | } |
174 | logger.verbose('Report has been sent to CORE', {requestOptions: options}); | 176 | }); |
175 | } | 177 | } |
176 | }); | 178 | |
177 | } | 179 | function resendReport(data) { |
178 | 180 | let sleepBeforeResend = 1000; | |
179 | function resendReport(data) { | 181 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') |
180 | let sleepBeforeResend = 1000; | 182 | |
181 | logger.verbose('Resend report to CORE in ' + sleepBeforeResend + 'ms') | 183 | setTimeout( |
182 | 184 | function() { | |
183 | setTimeout( | 185 | report(data); |
184 | function() { | 186 | }, |
185 | report(data); | 187 | sleepBeforeResend |
186 | }, | 188 | ) |
187 | sleepBeforeResend | 189 | } |
188 | ) | 190 | |
189 | } | 191 | function isPaused() { |
190 | 192 | return matrix.paused; | |
191 | function isPaused() { | 193 | } |
192 | return matrix.paused; | 194 | |
193 | } | 195 | function pause() { |
194 | 196 | matrix.paused = true; | |
195 | function pause() { | 197 | } |
196 | matrix.paused = true; | 198 | |
197 | } | 199 | function resume() { |
198 | 200 | matrix.pause = false; | |
199 | function resume() { | 201 | } |
200 | matrix.pause = false; | 202 | |
201 | } | 203 | function initMatrix() { |
202 | 204 | if (!matrix) { | |
203 | function initMatrix() { | 205 | matrix = {}; |
204 | if (!matrix) { | 206 | } |
205 | matrix = {}; | 207 | |
206 | } | 208 | matrix.counter = { |
207 | 209 | trx: 0 | |
208 | matrix.counter = { | 210 | } |
209 | trx: 0 | 211 | } |
210 | } | 212 | |
211 | } | 213 | function incrementCounterTrx() { |
212 | 214 | matrix.counter.trx++; | |
213 | function incrementCounterTrx() { | 215 | } |
214 | matrix.counter.trx++; | 216 | |
215 | } | 217 | function getRemoteProduct(product) { |
216 | 218 | let remoteProduct = config.remote_products[product]; | |
217 | function getRemoteProduct(product) { | 219 | return remoteProduct || product; |
218 | let remoteProduct = config.remote_products[product]; | 220 | } |
219 | return remoteProduct || product; | 221 | |
220 | } | 222 | initMatrix(); |
221 | 223 | setInterval(pullTask, config.pull_interval_ms || 1000); | |
222 | initMatrix(); | 224 | |
223 | setInterval(pullTask, config.pull_interval_ms || 1000); | 225 | exports.setPartner = setPartner; |
224 | 226 | exports.isPaused = isPaused; | |
225 | exports.setPartner = setPartner; | 227 | exports.pause = pause; |
226 | exports.isPaused = isPaused; | 228 | exports.resume = resume; |
227 | exports.pause = pause; | 229 | exports.report = report; |
228 | exports.resume = resume; | 230 |
heartbeat.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 | 8 | ||
9 | let module_type; | ||
10 | |||
9 | let module_type; | 11 | function sendHeartbeat() { |
10 | 12 | if (!config || !config.core_url || !module_type) { return; } | |
11 | function sendHeartbeat() { | 13 | |
12 | if (!config || !config.core_url || !module_type) { return; } | 14 | const requestOptions = { |
13 | 15 | uri: config.core_url + '/heartbeats', | |
14 | const requestOptions = { | 16 | method: 'POST', |
17 | json: { | ||
15 | uri: config.core_url + '/heartbeats', | 18 | name: config.handler_name, |
16 | method: 'POST', | 19 | module_type: module_type, |
17 | json: { | 20 | config: config, |
18 | name: config.handler_name, | 21 | matrix: matrix |
19 | module_type: module_type, | 22 | } |
20 | config: config, | 23 | } |
21 | matrix: matrix | 24 | |
22 | } | 25 | request.post(requestOptions); |
23 | } | 26 | } |
24 | 27 | ||
25 | request.post(requestOptions); | 28 | sendHeartbeat(); |
26 | } | 29 | setInterval( |
27 | 30 | sendHeartbeat, | |
28 | sendHeartbeat(); | 31 | 60 * 1000 |
29 | setInterval( | 32 | ) |
33 | |||
34 | function setModuleType(value) { | ||
35 | module_type = value; | ||
36 | } | ||
37 | |||
38 | exports.setModuleType = setModuleType; | ||
30 | sendHeartbeat, | 39 |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.12.0", | 3 | "version": "1.12.1", |
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.2", | 24 | "express": "^4.16.2", |
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 | "moment": "^2.19.1", | 27 | "moment": "^2.19.1", |
28 | "numeral": "^2.0.6", | 28 | "numeral": "^2.0.6", |
29 | "nunjucks": "^3.0.1", | 29 | "nunjucks": "^3.0.1", |
30 | "request": "^2.81.0", | 30 | "request": "^2.81.0", |
31 | "simple-git": "^1.80.1", | 31 | "simple-git": "^1.80.1", |
32 | "strftime": "^0.10.0", | 32 | "strftime": "^0.10.0", |
33 | "uniqid": "^4.1.1", | 33 | "uniqid": "^4.1.1", |
34 | "uuid": "^3.1.0", | 34 | "uuid": "^3.1.0", |
35 | "winston": "^2.3.1", | 35 | "winston": "^2.3.1", |
36 | "winston-circular-buffer": "^1.0.0", | 36 | "winston-circular-buffer": "^1.0.0", |
37 | "winston-daily-rotate-file": "^1.4.6" | 37 | "winston-daily-rotate-file": "^1.4.6" |
38 | } | 38 | } |
39 | } | 39 | } |
40 | 40 |