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