Commit 62fcc778ca0ccd958f7687cbfa312dce8cf4bae4

Authored by Adhidarma Hadiwinoto
1 parent 485b7af4bb
Exists in master

Debug

Showing 1 changed file with 18 additions and 10 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) => { 25 const sleepMs = (ms) => new Promise((resolve) => {
26 setTimeout(() => { 26 setTimeout(() => {
27 resolve(); 27 resolve();
28 }, ms); 28 }, ms);
29 }); 29 });
30 30
31 const dumper = async (xid, webhookType, body) => { 31 const dumper = async (xid, webhookType, body) => {
32 if (!config.webhook || !config.webhook.dump) { 32 if (!config.webhook || !config.webhook.dump) {
33 return; 33 return;
34 } 34 }
35 35
36 await fs.promises.writeFile( 36 try {
37 path.join(baseDumpDir, [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_')), 37 await fs.promises.writeFile(
38 stringify({ webhookType, body }), 38 path.join(baseDumpDir, [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_')),
39 ); 39 stringify({ webhookType, body }),
40 40 );
41 await fs.promises.writeFile( 41
42 lastDumpFileName, 42 await fs.promises.writeFile(
43 stringify({ webhookType, body }), 43 lastDumpFileName,
44 ); 44 stringify({ webhookType, body }),
45 );
46 } catch (e) {
47 logger.warn(`${MODULE_NAME} D3EF00D9: Exception on dumper`, {
48 xid,
49 eCode: e.code,
50 eMessage: e.message || e.toString(),
51 });
52 }
45 }; 53 };
46 54
47 const sender = async (xid, webhookType, body, retry) => { 55 const sender = async (xid, webhookType, body, retry) => {
48 if (!config.webhook || !config.webhook.url) { 56 if (!config.webhook || !config.webhook.url) {
49 return; 57 return;
50 } 58 }
51 59
52 try { 60 try {
53 logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, { 61 logger.verbose(`${MODULE_NAME} 2CA59ED3: Sending webhook`, {
54 xid, 62 xid,
55 webhookType, 63 webhookType,
56 partner: config.webhook.url, 64 partner: config.webhook.url,
57 trxId: body.transaction_id, 65 trxId: body.transaction_id,
58 request_id: body.request_id, 66 request_id: body.request_id,
59 }); 67 });
60 68
61 axios.post(config.listener.partner.webhook, { 69 axios.post(config.listener.partner.webhook, {
62 webhookType, 70 webhookType,
63 body, 71 body,
64 }); 72 });
65 73
66 await dumper(xid, webhookType, body); 74 await dumper(xid, webhookType, body);
67 75
68 logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, { 76 logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, {
69 xid, 77 xid,
70 webhookType, 78 webhookType,
71 partner: config.listener.partner.webhook, 79 partner: config.listener.partner.webhook,
72 }); 80 });
73 } catch (e) { 81 } catch (e) {
74 logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, { 82 logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, {
75 xid, 83 xid,
76 httpStatusCode: e.response && e.response.status, 84 httpStatusCode: e.response && e.response.status,
77 eCode: e.code, 85 eCode: e.code,
78 eMessage: e.message || e.toString(), 86 eMessage: e.message || e.toString(),
79 retried: retry || 0, 87 retried: retry || 0,
80 maxRetry, 88 maxRetry,
81 }); 89 });
82 90
83 if ((retry || 0) >= maxRetry) { 91 if ((retry || 0) >= maxRetry) {
84 logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, { 92 logger.warn(`${MODULE_NAME} 4A60B406: Max retry exceeded`, {
85 xid, 93 xid,
86 }); 94 });
87 95
88 return; 96 return;
89 } 97 }
90 98
91 await sleepMs(sleepBeforeRetryMs); 99 await sleepMs(sleepBeforeRetryMs);
92 sender(xid, webhookType, body, (retry || 0) + 1); 100 await sender(xid, webhookType, body, (retry || 0) + 1);
93 } 101 }
94 }; 102 };
95 module.exports = sender; 103 module.exports = sender;
96 104