Commit c79d437bb96c173cbcd5e8ca8e64b16dbd35154b

Authored by Adhidarma Hadiwinoto
1 parent d2d9093e7f
Exists in master

Ubah delay after core error dari skipNext menjadi sleep

Showing 2 changed files with 32 additions and 11 deletions Inline Diff

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