Commit e7f5d2a50dc9c561a293c20181937338346e968b
1 parent
b403448adb
Exists in
master
and in
1 other branch
active sending
Showing 2 changed files with 14 additions and 0 deletions Inline Diff
lib/core-callback/sender.js
1 | const MODULE_NAME = 'CORE-CALLBACK.SENDER'; | 1 | const MODULE_NAME = 'CORE-CALLBACK.SENDER'; |
2 | 2 | ||
3 | const axios = require('axios').default; | 3 | const axios = require('axios').default; |
4 | const config = require('komodo-sdk/config'); | 4 | const config = require('komodo-sdk/config'); |
5 | const logger = require('komodo-sdk/logger'); | 5 | const logger = require('komodo-sdk/logger'); |
6 | 6 | ||
7 | const matrix = require('../matrix'); | 7 | const matrix = require('../matrix'); |
8 | 8 | ||
9 | const HTTP_TIMEOUT = Number( | 9 | const HTTP_TIMEOUT = Number( |
10 | config.callback_sender && config.callback_sender.http_timeout_ms, | 10 | config.callback_sender && config.callback_sender.http_timeout_ms, |
11 | ) || 30 * 1000; | 11 | ) || 30 * 1000; |
12 | 12 | ||
13 | const SLEEP_BEFORE_RETRY_MS = Number( | 13 | const SLEEP_BEFORE_RETRY_MS = Number( |
14 | config.callback_sender && config.callback_sender.sleep_before_retry_ms, | 14 | config.callback_sender && config.callback_sender.sleep_before_retry_ms, |
15 | ) || 10 * 1000; | 15 | ) || 10 * 1000; |
16 | 16 | ||
17 | const MAX_RETRY = Number( | 17 | const MAX_RETRY = Number( |
18 | config.callback_sender && config.callback_sender.max_retry, | 18 | config.callback_sender && config.callback_sender.max_retry, |
19 | ) || 10; | 19 | ) || 10; |
20 | 20 | ||
21 | logger.verbose(`${MODULE_NAME} 848B9104: Initialized`, { | 21 | logger.verbose(`${MODULE_NAME} 848B9104: Initialized`, { |
22 | HTTP_TIMEOUT, | 22 | HTTP_TIMEOUT, |
23 | SLEEP_BEFORE_RETRY_MS, | 23 | SLEEP_BEFORE_RETRY_MS, |
24 | MAX_RETRY, | 24 | MAX_RETRY, |
25 | }); | 25 | }); |
26 | 26 | ||
27 | const axiosHeaders = { | 27 | const axiosHeaders = { |
28 | 'Content-Type': 'application/json', | 28 | 'Content-Type': 'application/json', |
29 | 'User-Agent': 'KOMODO-HTTPGETX callback sender', | 29 | 'User-Agent': 'KOMODO-HTTPGETX callback sender', |
30 | }; | 30 | }; |
31 | 31 | ||
32 | const sleep = require('../sleep'); | 32 | const sleep = require('../sleep'); |
33 | const urlConcatQs = require('../url-concat-qs'); | 33 | const urlConcatQs = require('../url-concat-qs'); |
34 | 34 | ||
35 | const sender = async (data, xid, retry) => { | 35 | const sender = async (data, xid, retry) => { |
36 | if (!data.reverse_url) { | 36 | if (!data.reverse_url) { |
37 | logger.verbose(`${MODULE_NAME} C4FF18FB: Ignoring missing reverse url`, { | 37 | logger.verbose(`${MODULE_NAME} C4FF18FB: Ignoring missing reverse url`, { |
38 | xid, | 38 | xid, |
39 | dataFromCore: data, | 39 | dataFromCore: data, |
40 | }); | 40 | }); |
41 | 41 | ||
42 | return; | 42 | return; |
43 | } | 43 | } |
44 | 44 | ||
45 | const params = { | 45 | const params = { |
46 | httpgetx_xid: xid, | 46 | httpgetx_xid: xid, |
47 | command: data.command, | 47 | command: data.command, |
48 | 48 | ||
49 | request_id: data.request_id && data.request_id.toString(), | 49 | request_id: data.request_id && data.request_id.toString(), |
50 | transaction_id: data.transaction_id && data.transaction_id.toString(), | 50 | transaction_id: data.transaction_id && data.transaction_id.toString(), |
51 | transaction_date: data.transaction_date, | 51 | transaction_date: data.transaction_date, |
52 | 52 | ||
53 | store_name: data.store_name, | 53 | store_name: data.store_name, |
54 | terminal_name: data.terminal_name, | 54 | terminal_name: data.terminal_name, |
55 | 55 | ||
56 | product_name: data.product_name, | 56 | product_name: data.product_name, |
57 | destination: data.destination, | 57 | destination: data.destination, |
58 | 58 | ||
59 | rc: data.rc, | 59 | rc: data.rc, |
60 | sn: data.sn || undefined, | 60 | sn: data.sn || undefined, |
61 | amount: Number(data.amount) || undefined, | 61 | amount: Number(data.amount) || undefined, |
62 | ending_balance: Number(data.ending_balance) || undefined, | 62 | ending_balance: Number(data.ending_balance) || undefined, |
63 | 63 | ||
64 | message: data.message, | 64 | message: data.message, |
65 | 65 | ||
66 | bill_count: Number(data.bill_count) || undefined, | 66 | bill_count: Number(data.bill_count) || undefined, |
67 | bill_amount: Number(data.bill_amount) || undefined, | 67 | bill_amount: Number(data.bill_amount) || undefined, |
68 | fee_per_bill: Number(data.fee) || undefined, | 68 | fee_per_bill: Number(data.fee) || undefined, |
69 | fee_total: Number(data.fee_total) || undefined, | 69 | fee_total: Number(data.fee_total) || undefined, |
70 | 70 | ||
71 | bill_detail: data.bill_detail || undefined, | 71 | bill_detail: data.bill_detail || undefined, |
72 | }; | 72 | }; |
73 | 73 | ||
74 | if (data.command === 'INQUIRY' && data.amount_to_charge) { | 74 | if (data.command === 'INQUIRY' && data.amount_to_charge) { |
75 | params.amount_to_charge = data.amount_to_charge; | 75 | params.amount_to_charge = data.amount_to_charge; |
76 | } | 76 | } |
77 | 77 | ||
78 | const isPostpaid = ['INQUIRY', 'PAY'].indexOf(data.command) >= 0; | 78 | const isPostpaid = ['INQUIRY', 'PAY'].indexOf(data.command) >= 0; |
79 | const isHttpPost = isPostpaid; | 79 | const isHttpPost = isPostpaid; |
80 | 80 | ||
81 | const endpointUrl = isHttpPost ? data.reverse_url : urlConcatQs(data.reverse_url, params); | 81 | const endpointUrl = isHttpPost ? data.reverse_url : urlConcatQs(data.reverse_url, params); |
82 | 82 | ||
83 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending to PARTNER`, { | 83 | logger.info(`${MODULE_NAME} 8B6A4CEC: Sending to PARTNER`, { |
84 | xid, | 84 | xid, |
85 | retry, | 85 | retry, |
86 | isPostpaid, | 86 | isPostpaid, |
87 | isHttpPost, | 87 | isHttpPost, |
88 | endpointUrl, | 88 | endpointUrl, |
89 | }); | 89 | }); |
90 | 90 | ||
91 | try { | 91 | try { |
92 | const response = isHttpPost | 92 | const response = isHttpPost |
93 | ? await axios.post(data.reverse_url, params, { | 93 | ? await axios.post(data.reverse_url, params, { |
94 | timeout: HTTP_TIMEOUT, | 94 | timeout: HTTP_TIMEOUT, |
95 | headers: axiosHeaders, | 95 | headers: axiosHeaders, |
96 | }) | 96 | }) |
97 | : await axios.get(data.reverse_url, { | 97 | : await axios.get(data.reverse_url, { |
98 | params, | 98 | params, |
99 | timeout: HTTP_TIMEOUT, | 99 | timeout: HTTP_TIMEOUT, |
100 | headers: axiosHeaders, | 100 | headers: axiosHeaders, |
101 | }); | 101 | }); |
102 | 102 | ||
103 | matrix.callback_sender.sent += 1; | 103 | matrix.callback_sender.sent += 1; |
104 | matrix.callback_sender.active_count += 1; | ||
105 | matrix.callback_sender.active_sending[xid] = { | ||
106 | ts: new Date(), | ||
107 | trxId: data.trx_id, | ||
108 | reverseUrl: data.reverse_url, | ||
109 | }; | ||
104 | 110 | ||
105 | if (isPostpaid) { | 111 | if (isPostpaid) { |
106 | matrix.callback_sender.sent_using_post += 1; | 112 | matrix.callback_sender.sent_using_post += 1; |
107 | } else { | 113 | } else { |
108 | matrix.callback_sender.sent_using_get += 1; | 114 | matrix.callback_sender.sent_using_get += 1; |
109 | } | 115 | } |
110 | 116 | ||
111 | logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, { | 117 | logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, { |
112 | xid, | 118 | xid, |
113 | retry, | 119 | retry, |
114 | httpStatus: response.status, | 120 | httpStatus: response.status, |
115 | responseBody: response && response.data, | 121 | responseBody: response && response.data, |
116 | }); | 122 | }); |
117 | } catch (e) { | 123 | } catch (e) { |
118 | matrix.callback_sender.sent_failed += 1; | 124 | matrix.callback_sender.sent_failed += 1; |
119 | matrix.callback_sender.last_error = { | 125 | matrix.callback_sender.last_error = { |
120 | xid, | 126 | xid, |
121 | ts: new Date(), | 127 | ts: new Date(), |
122 | eCode: e.code, | 128 | eCode: e.code, |
123 | eMessage: e.message, | 129 | eMessage: e.message, |
130 | trxId: data.trx_id, | ||
124 | reverseUrl: data.reverse_url, | 131 | reverseUrl: data.reverse_url, |
125 | httpStatus: e.response && e.response.status, | 132 | httpStatus: e.response && e.response.status, |
126 | responseBody: e.response && e.response.data, | 133 | responseBody: e.response && e.response.data, |
127 | }; | 134 | }; |
128 | 135 | ||
129 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, { | 136 | logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, { |
130 | xid, | 137 | xid, |
131 | retry, | 138 | retry, |
132 | maxRetry: MAX_RETRY, | 139 | maxRetry: MAX_RETRY, |
133 | errCode: e.code, | 140 | errCode: e.code, |
134 | errMessage: e.message, | 141 | errMessage: e.message, |
135 | reverseUrl: data.reverse_url, | 142 | reverseUrl: data.reverse_url, |
136 | endpointUrl, | 143 | endpointUrl, |
137 | httpStatus: e.response && e.response.status, | 144 | httpStatus: e.response && e.response.status, |
138 | responseBody: e.response && e.response.data, | 145 | responseBody: e.response && e.response.data, |
139 | }); | 146 | }); |
140 | 147 | ||
141 | if ((retry || 0) < MAX_RETRY) { | 148 | if ((retry || 0) < MAX_RETRY) { |
142 | await sleep(SLEEP_BEFORE_RETRY_MS); | 149 | await sleep(SLEEP_BEFORE_RETRY_MS); |
143 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { | 150 | logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { |
144 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, | 151 | xid, sleepTime: SLEEP_BEFORE_RETRY_MS, |
145 | }); | 152 | }); |
146 | sender(data, xid, (retry || 0) + 1); | 153 | sender(data, xid, (retry || 0) + 1); |
147 | } | 154 | } |
155 | } finally { | ||
156 | matrix.callback_sender.active_count -= 1; | ||
157 | if (matrix.callback_sender.active_sending[xid]) { | ||
158 | delete matrix.callback_sender.active_sending[xid]; | ||
159 | } | ||
148 | } | 160 | } |
149 | }; | 161 | }; |
150 | 162 | ||
151 | module.exports = sender; | 163 | module.exports = sender; |
152 | 164 |
lib/matrix.js
1 | const os = require('os'); | 1 | const os = require('os'); |
2 | // const config = require('komodo-sdk/config'); | 2 | // const config = require('komodo-sdk/config'); |
3 | 3 | ||
4 | module.exports = { | 4 | module.exports = { |
5 | pid: process.pid, | 5 | pid: process.pid, |
6 | ts: new Date(), | 6 | ts: new Date(), |
7 | start_time: new Date(), | 7 | start_time: new Date(), |
8 | start_time_from_now: null, | 8 | start_time_from_now: null, |
9 | uptime_secs: process.uptime(), | 9 | uptime_secs: process.uptime(), |
10 | hostname: os.hostname(), | 10 | hostname: os.hostname(), |
11 | loadavg: os.loadavg(), | 11 | loadavg: os.loadavg(), |
12 | workdir: process.cwd(), | 12 | workdir: process.cwd(), |
13 | memory_usage: process.memoryUsage(), | 13 | memory_usage: process.memoryUsage(), |
14 | nodejs_versions: process.versions, | 14 | nodejs_versions: process.versions, |
15 | core: { | 15 | core: { |
16 | received: 0, | 16 | received: 0, |
17 | sent: 0, | 17 | sent: 0, |
18 | sent_failed: 0, | 18 | sent_failed: 0, |
19 | last_error: null, | 19 | last_error: null, |
20 | }, | 20 | }, |
21 | callback_sender: { | 21 | callback_sender: { |
22 | sent: 0, | 22 | sent: 0, |
23 | sent_failed: 0, | 23 | sent_failed: 0, |
24 | sent_using_get: 0, | 24 | sent_using_get: 0, |
25 | sent_using_post: 0, | 25 | sent_using_post: 0, |
26 | last_error: null, | 26 | last_error: null, |
27 | active_count: 0, | ||
28 | active_sending: {}, | ||
27 | }, | 29 | }, |
28 | }; | 30 | }; |
29 | 31 |