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