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