Blame view

lib/webhook-sender.js 2.63 KB
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  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');
  if (!fs.existsSync(baseDumpDir)) {
      fs.mkdirSync(baseDumpDir, { recursive: true });
  }
  const lastDumpFileName = path.join(baseDumpDir, 'last');
5e27716d8   Adhidarma Hadiwinoto   sleepMs promise
24
25
26
27
28
  const sleepMs = (ms) => new Promise((resolve) => {
      setTimeout(() => {
          resolve();
      }, ms);
  });
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  const dumper = async (xid, webhookType, body) => {
      if (!config.webhook || !config.webhook.dump) {
          return;
      }
  
      await fs.promises.writeFile(
          path.join(baseDumpDir, [moment().format('YYYYMMDD-HHmmssSSS'), xid].join('_')),
          stringify({ webhookType, body }),
      );
  
      await fs.promises.writeFile(
          lastDumpFileName,
          stringify({ webhookType, body }),
      );
  };
  
  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,
          });
  
          axios.post(config.listener.partner.webhook, {
              webhookType,
              body,
          });
  
          await dumper(xid, webhookType, body);
  
          logger.verbose(`${MODULE_NAME} 50BE8D98: Webhook sent`, {
              xid,
              webhookType,
              partner: config.listener.partner.webhook,
          });
      } catch (e) {
          logger.warn(`${MODULE_NAME} ECC37ECA: Exception on calling webhook`, {
              xid,
485b7af4b   Adhidarma Hadiwinoto   Debug webhooksend...
74
              httpStatusCode: e.response && e.response.status,
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
75
76
77
78
79
80
81
82
83
84
85
86
87
              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   Adhidarma Hadiwinoto   sleepMs promise
88
89
          await sleepMs(sleepBeforeRetryMs);
          sender(xid, webhookType, body, (retry || 0) + 1);
a9aef2698   Adhidarma Hadiwinoto   Separate webhook ...
90
91
92
      }
  };
  module.exports = sender;