Commit d2d9093e7fc4439132127cc3044176fc80cd972b

Authored by Adhidarma Hadiwinoto
1 parent c6455cb8c3
Exists in master

config,postpaid_pull_interval_ms

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