Commit dc2d0f90ece4b4e3c67040c682ef221d87a53f55

Authored by Adhidarma Hadiwinoto
1 parent 1e4359f18f
Exists in master and in 1 other branch dev

MATRIX.callback_sender.last_error

Showing 2 changed files with 10 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.message_sent += 1; 103 matrix.callback_sender.message_sent += 1;
104 104
105 if (isPostpaid) { 105 if (isPostpaid) {
106 matrix.callback_sender.message_sent_using_post_method += 1; 106 matrix.callback_sender.message_sent_using_post_method += 1;
107 } else { 107 } else {
108 matrix.callback_sender.message_sent_using_get_method += 1; 108 matrix.callback_sender.message_sent_using_get_method += 1;
109 } 109 }
110 110
111 logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, { 111 logger.info(`${MODULE_NAME} 3641FBD7: Has been sent to PARTNER successfully`, {
112 xid, 112 xid,
113 retry, 113 retry,
114 httpStatus: response.status, 114 httpStatus: response.status,
115 responseBody: response && response.data, 115 responseBody: response && response.data,
116 }); 116 });
117 } catch (e) { 117 } catch (e) {
118 matrix.callback_sender.message_sent_failed += 1; 118 matrix.callback_sender.message_sent_failed += 1;
119 matrix.callback_sender.last_error = {
120 xid,
121 ts: new Date(),
122 eCode: e.code,
123 eMessage: e.message,
124 reverseUrl: data.reverse_url,
125 httpStatus: e.response && e.response.status,
126 responseBody: e.response && e.response.data,
127 };
119 128
120 logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, { 129 logger.warn(`${MODULE_NAME} A1EC9E70: Failed on sending to PARTNER`, {
121 xid, 130 xid,
122 retry, 131 retry,
123 maxRetry: MAX_RETRY, 132 maxRetry: MAX_RETRY,
124 errCode: e.code, 133 errCode: e.code,
125 errMessage: e.message, 134 errMessage: e.message,
126 reverseUrl: data.reverse_url, 135 reverseUrl: data.reverse_url,
127 endpointUrl, 136 endpointUrl,
128 httpStatus: e.response && e.response.status, 137 httpStatus: e.response && e.response.status,
129 responseBody: e.response && e.response.data, 138 responseBody: e.response && e.response.data,
130 }); 139 });
131 140
132 if ((retry || 0) < MAX_RETRY) { 141 if ((retry || 0) < MAX_RETRY) {
133 await sleep(SLEEP_BEFORE_RETRY_MS); 142 await sleep(SLEEP_BEFORE_RETRY_MS);
134 logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, { 143 logger.verbose(`${MODULE_NAME} D8958695: Going to retry sending CORE-CALLBACK TO PARTNER`, {
135 xid, sleepTime: SLEEP_BEFORE_RETRY_MS, 144 xid, sleepTime: SLEEP_BEFORE_RETRY_MS,
136 }); 145 });
137 sender(data, xid, (retry || 0) + 1); 146 sender(data, xid, (retry || 0) + 1);
138 } 147 }
139 } 148 }
140 }; 149 };
141 150
142 module.exports = sender; 151 module.exports = sender;
143 152
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 messages_from_core: 0, 15 messages_from_core: 0,
16 messages_to_core: 0, 16 messages_to_core: 0,
17 callback_sender: { 17 callback_sender: {
18 message_sent: 0, 18 message_sent: 0,
19 message_sent_failed: 0, 19 message_sent_failed: 0,
20 message_sent_using_get_method: 0, 20 message_sent_using_get_method: 0,
21 message_sent_using_post_method: 0, 21 message_sent_using_post_method: 0,
22 last_error: null,
22 }, 23 },
23 }; 24 };
24 25