Commit aed5668ab6908113544415c74d98109fee101af8

Authored by Adhidarma Hadiwinoto
1 parent 770100cf15
Exists in master

Change DEFAULT_CORE_REQUEST_TIMEOUT to 15s

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

1 const DEFAULT_CORE_REQUEST_TIMEOUT = 5 * 1000; 1 const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000;
2 const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000; 2 const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000;
3 const MAX_REPORT_RETRY = 30; 3 const MAX_REPORT_RETRY = 30;
4 const REPORT_RETRY_SLEEP_MS = 2000; 4 const REPORT_RETRY_SLEEP_MS = 2000;
5 5
6 const axios = require('axios').default; 6 const axios = require('axios').default;
7 7
8 const config = require('komodo-sdk/config'); 8 const config = require('komodo-sdk/config');
9 const coreUrl = require('komodo-sdk/core-url'); 9 const coreUrl = require('komodo-sdk/core-url');
10 const logger = require('tektrans-logger'); 10 const logger = require('tektrans-logger');
11 11
12 const sleep = require('./lib/sleep'); 12 const sleep = require('./lib/sleep');
13 13
14 let partner; 14 let partner;
15 let first = true; 15 let first = true;
16 let coreIsHealthy = null; 16 let coreIsHealthy = null;
17 // let skipNext = 0; 17 // let skipNext = 0;
18 18
19 const DEFAULT_CORE_AXIOS_CONFIG = { 19 const DEFAULT_CORE_AXIOS_CONFIG = {
20 headers: { 'Content-Type': 'application/json' }, 20 headers: { 'Content-Type': 'application/json' },
21 timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, 21 timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT,
22 }; 22 };
23 23
24 const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; 24 const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG;
25 const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; 25 const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG;
26 26
27 function detectCoreStatusChange(isHealthy, url, err) { 27 function detectCoreStatusChange(isHealthy, url, err) {
28 if (coreIsHealthy !== isHealthy) { 28 if (coreIsHealthy !== isHealthy) {
29 coreIsHealthy = isHealthy; 29 coreIsHealthy = isHealthy;
30 30
31 if (isHealthy) { 31 if (isHealthy) {
32 logger.info('POSTPAID2-SDK: CORE is healthy now', { url, err: err && err.message }); 32 logger.info('POSTPAID2-SDK: CORE is healthy now', { url, err: err && err.message });
33 } else { 33 } else {
34 logger.warn('POSTPAID2-SDK: CORE is unhealthy now', { url, err: err && err.message }); 34 logger.warn('POSTPAID2-SDK: CORE is unhealthy now', { url, err: err && err.message });
35 } 35 }
36 } 36 }
37 } 37 }
38 38
39 async function getTask() { 39 async function getTask() {
40 if (!partner) return null; 40 if (!partner) return null;
41 41
42 // if (skipNext > 0) { 42 // if (skipNext > 0) {
43 // skipNext -= 1; 43 // skipNext -= 1;
44 // return null; 44 // return null;
45 // } 45 // }
46 46
47 if (first) { 47 if (first) {
48 first = false; 48 first = false;
49 logger.verbose('POSTPAID2-SDK: first pull request to CORE'); 49 logger.verbose('POSTPAID2-SDK: first pull request to CORE');
50 } 50 }
51 51
52 const coreEndpoint = `${coreUrl}/postpaid2/task`; 52 const coreEndpoint = `${coreUrl}/postpaid2/task`;
53 53
54 const payload = { 54 const payload = {
55 handler: config.handler || config.handler_name, 55 handler: config.handler || config.handler_name,
56 products: config.products || [], 56 products: config.products || [],
57 }; 57 };
58 58
59 let task; 59 let task;
60 try { 60 try {
61 const result = await axios.post( 61 const result = await axios.post(
62 coreEndpoint, payload, GET_TASK_AXIOS_CONFIG, 62 coreEndpoint, payload, GET_TASK_AXIOS_CONFIG,
63 ); 63 );
64 64
65 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6'); 65 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6');
66 if (result && result.data && result.data.task && !result.data.task.postpaid) throw new Error('POSTPAID2-SDK: CORE returning non postpaid task on pulling task. MARK-B338CEF8'); 66 if (result && result.data && result.data.task && !result.data.task.postpaid) throw new Error('POSTPAID2-SDK: CORE returning non postpaid task on pulling task. MARK-B338CEF8');
67 67
68 detectCoreStatusChange(true, coreEndpoint); 68 detectCoreStatusChange(true, coreEndpoint);
69 task = result.data.task; 69 task = result.data.task;
70 } catch (e) { 70 } catch (e) {
71 // skipNext = 3; 71 // skipNext = 3;
72 detectCoreStatusChange(false, coreEndpoint, e); 72 detectCoreStatusChange(false, coreEndpoint, e);
73 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS); 73 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS);
74 } 74 }
75 75
76 if (!task) return null; 76 if (!task) return null;
77 77
78 task.remote_product = ((config.remote_products || {})[task.product]) || task.product; 78 task.remote_product = ((config.remote_products || {})[task.product]) || task.product;
79 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0); 79 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0);
80 80
81 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task }); 81 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task });
82 82
83 if (task.inquiry_only) { 83 if (task.inquiry_only) {
84 if (typeof partner.inquiry === 'function') partner.inquiry(task); 84 if (typeof partner.inquiry === 'function') partner.inquiry(task);
85 return task; 85 return task;
86 } 86 }
87 87
88 if (typeof partner.pay === 'function') { 88 if (typeof partner.pay === 'function') {
89 partner.pay(task); 89 partner.pay(task);
90 return task; 90 return task;
91 } 91 }
92 92
93 return null; 93 return null;
94 } 94 }
95 95
96 const getTaskLooper = async () => { 96 const getTaskLooper = async () => {
97 await getTask(); 97 await getTask();
98 98
99 setTimeout(() => { 99 setTimeout(() => {
100 getTaskLooper(); 100 getTaskLooper();
101 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000); 101 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000);
102 }; 102 };
103 103
104 exports.setPartner = (val) => { 104 exports.setPartner = (val) => {
105 partner = val; 105 partner = val;
106 // setInterval(getTaskLooper, config.pull_interval_ms || 1000); 106 // setInterval(getTaskLooper, config.pull_interval_ms || 1000);
107 getTaskLooper(); 107 getTaskLooper();
108 }; 108 };
109 109
110 exports.report = async (data, xid, retry) => { 110 exports.report = async (data, xid, retry) => {
111 if (!data.trx_id) { 111 if (!data.trx_id) {
112 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data }); 112 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data });
113 return; 113 return;
114 } 114 }
115 115
116 if (!data.rc || typeof data.rc !== 'string') { 116 if (!data.rc || typeof data.rc !== 'string') {
117 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', { 117 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', {
118 xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc, 118 xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc,
119 }); 119 });
120 } 120 }
121 121
122 const dataToReport = { 122 const dataToReport = {
123 handler: config.handler || config.handler_name, 123 handler: config.handler || config.handler_name,
124 command: data.command, 124 command: data.command,
125 trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), 125 trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0),
126 rc: data.rc, 126 rc: data.rc,
127 sn: data.sn, 127 sn: data.sn,
128 amount: data.amount, 128 amount: data.amount,
129 base_bill_amount: data.base_bill_amount, 129 base_bill_amount: data.base_bill_amount,
130 bill_count: data.bill_count, 130 bill_count: data.bill_count,
131 message: data.message, 131 message: data.message,
132 info: data.info, 132 info: data.info,
133 detail: data.detail, 133 detail: data.detail,
134 data: data.data, 134 data: data.data,
135 struk: (typeof data.struk === 'string' && data.struk) 135 struk: (typeof data.struk === 'string' && data.struk)
136 || (data.struk && JSON.stringify(data.struk)) 136 || (data.struk && JSON.stringify(data.struk))
137 || null, 137 || null,
138 }; 138 };
139 139
140 const coreEndpoint = `${coreUrl}/postpaid2/report`; 140 const coreEndpoint = `${coreUrl}/postpaid2/report`;
141 141
142 logger.verbose('POSTPAID2-SDK: Going to report to CORE', { 142 logger.verbose('POSTPAID2-SDK: Going to report to CORE', {
143 xid, 143 xid,
144 trxId: data.trx_id, 144 trxId: data.trx_id,
145 rc: data.rc, 145 rc: data.rc,
146 sn: data.sn, 146 sn: data.sn,
147 amount: data.amount, 147 amount: data.amount,
148 baseBillAmount: data.base_bill_amount, 148 baseBillAmount: data.base_bill_amount,
149 coreEndpoint, 149 coreEndpoint,
150 }); 150 });
151 151
152 try { 152 try {
153 const reportResult = await axios.post( 153 const reportResult = await axios.post(
154 coreEndpoint, JSON.stringify(dataToReport), REPORT_AXIOS_CONFIG, 154 coreEndpoint, JSON.stringify(dataToReport), REPORT_AXIOS_CONFIG,
155 ); 155 );
156 156
157 if (!reportResult) { 157 if (!reportResult) {
158 logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB'); 158 logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB');
159 } 159 }
160 } catch (e) { 160 } catch (e) {
161 const newRetry = (retry || 0) + 1; 161 const newRetry = (retry || 0) + 1;
162 logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', { 162 logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', {
163 xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, 163 xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY,
164 }); 164 });
165 165
166 if (!this || !this.report) { 166 if (!this || !this.report) {
167 logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!'); 167 logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!');
168 return; 168 return;
169 } 169 }
170 170
171 if (newRetry < MAX_REPORT_RETRY) { 171 if (newRetry < MAX_REPORT_RETRY) {
172 setTimeout(() => { 172 setTimeout(() => {
173 logger.info('POSTPAID2-SDK: Retrying to send report to CORE', { 173 logger.info('POSTPAID2-SDK: Retrying to send report to CORE', {
174 xid, 174 xid,
175 trxId: data.trx_id, 175 trxId: data.trx_id,
176 rc: data.rc, 176 rc: data.rc,
177 sn: data.sn, 177 sn: data.sn,
178 amount: data.amount, 178 amount: data.amount,
179 baseBillAmount: data.base_bill_amount, 179 baseBillAmount: data.base_bill_amount,
180 }); 180 });
181 181
182 this.report(data, xid, newRetry); 182 this.report(data, xid, newRetry);
183 }, REPORT_RETRY_SLEEP_MS); 183 }, REPORT_RETRY_SLEEP_MS);
184 } 184 }
185 } 185 }
186 }; 186 };
187 187