Commit 5e27716d8e786b828fdcca57bc8500211391b19a

Authored by Adhidarma Hadiwinoto
1 parent 5326735c91
Exists in master

sleepMs promise

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

lib/webhook-sender.js
1 const MODULE_NAME = 'WEBHOOK-SENDER'; 1 const MODULE_NAME = 'WEBHOOK-SENDER';
2 2
3 const axios = require('axios'); 3 const axios = require('axios');
4 const moment = require('moment'); 4 const moment = require('moment');
5 const fs = require('fs'); 5 const fs = require('fs');
6 const path = require('path'); 6 const path = require('path');
7 const stringify = require('json-stringify-pretty-compact'); 7 const stringify = require('json-stringify-pretty-compact');
8 const config = require('komodo-sdk/config'); 8 const config = require('komodo-sdk/config');
9 const logger = require('tektrans-logger'); 9 const logger = require('tektrans-logger');
10 10
11 const DEFAULT_MAX_RETRY = 10; 11 const DEFAULT_MAX_RETRY = 10;
12 const DEFAULT_SLEEP_BEFORE_RETRY_MS = 10 * 1000; 12 const DEFAULT_SLEEP_BEFORE_RETRY_MS = 10 * 1000;
13 13
14 const maxRetry = Number(config.webhook && config.webhook.max_retry) 14 const maxRetry = Number(config.webhook && config.webhook.max_retry)
15 || DEFAULT_MAX_RETRY; 15 || DEFAULT_MAX_RETRY;
16 const sleepBeforeRetryMs = Number(config.webhook && config.webhook.sleep_before_retry_ms) 16 const sleepBeforeRetryMs = Number(config.webhook && config.webhook.sleep_before_retry_ms)
17 || DEFAULT_SLEEP_BEFORE_RETRY_MS; 17 || DEFAULT_SLEEP_BEFORE_RETRY_MS;
18 18
19 const baseDumpDir = path.join('dump', 'webhook-sender'); 19 const baseDumpDir = path.join('dump', 'webhook-sender');
20 if (!fs.existsSync(baseDumpDir)) { 20 if (!fs.existsSync(baseDumpDir)) {
21 fs.mkdirSync(baseDumpDir, { recursive: true }); 21 fs.mkdirSync(baseDumpDir, { recursive: true });
22 } 22 }
23 const lastDumpFileName = path.join(baseDumpDir, 'last'); 23 const lastDumpFileName = path.join(baseDumpDir, 'last');
24 24
25 const sleepMs = (ms) => new Promise((resolve) => {
26 setTimeout(() => {
27 resolve();
28 }, ms);
29 });
30
25 const dumper = async (xid, webhookType, body) => { 31 const dumper = async (xid, webhookType, body) => {
26 if (!config.webhook || !config.webhook.dump) { 32 if (!config.webhook || !config.webhook.dump) {
27 return; 33 return;
28 } 34 }
29 35
30 await fs.promises.writeFile( 36 await fs.promises.writeFile(
31 path.join(baseDumpDir, [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_')), 37 path.join(baseDumpDir, [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_')),
32 stringify({ webhookType, body }), 38 stringify({ webhookType, body }),
33 ); 39 );
34 40
35 await fs.promises.writeFile( 41 await fs.promises.writeFile(
36 lastDumpFileName, 42 lastDumpFileName,
37 stringify({ webhookType, body }), 43 stringify({ webhookType, body }),
38 ); 44 );
39 }; 45 };
40 46
41 const sender = async (xid, webhookType, body, retry) => { 47 const sender = async (xid, webhookType, body, retry) => {
42 if (!config.webhook || !config.webhook.url) { 48 if (!config.webhook || !config.webhook.url) {
43 return; 49 return;
44 } 50 }
45 51
46 try { 52 try {
47 logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, { 53 logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, {
48 xid, 54 xid,
49 webhookType, 55 webhookType,
50 partner: config.webhook.url, 56 partner: config.webhook.url,
51 trxId: body.transaction_id, 57 trxId: body.transaction_id,
52 request_id: body.request_id, 58 request_id: body.request_id,
53 }); 59 });
54 60
55 axios.post(config.listener.partner.webhook, { 61 axios.post(config.listener.partner.webhook, {
56 webhookType, 62 webhookType,
57 body, 63 body,
58 }); 64 });
59 65
60 await dumper(xid, webhookType, body); 66 await dumper(xid, webhookType, body);
61 67
62 logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, { 68 logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, {
63 xid, 69 xid,
64 webhookType, 70 webhookType,
65 partner: config.listener.partner.webhook, 71 partner: config.listener.partner.webhook,
66 }); 72 });
67 } catch (e) { 73 } catch (e) {
68 logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, { 74 logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, {
69 xid, 75 xid,
70 eCode: e.code, 76 eCode: e.code,
71 eMessage: e.message || e.toString(), 77 eMessage: e.message || e.toString(),
72 retried: retry || 0, 78 retried: retry || 0,
73 maxRetry, 79 maxRetry,
74 }); 80 });
75 81
76 if ((retry || 0) >= maxRetry) { 82 if ((retry || 0) >= maxRetry) {
77 logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, { 83 logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, {
78 xid, 84 xid,
79 }); 85 });
80 86
81 return; 87 return;
82 } 88 }
83 89
84 setTimeout(() => { 90 await sleepMs(sleepBeforeRetryMs);
85 sender(xid, webhookType, body, (retry || 0) + 1); 91 sender(xid, webhookType, body, (retry || 0) + 1);
86 }, sleepBeforeRetryMs);
87 } 92 }
88 }; 93 };
89 module.exports = sender; 94 module.exports = sender;