Compare View

switch
from
...
to
 
Commits (2)

Changes

Showing 2 changed files Inline Diff

gateway/resend-delay.js
1 "use strict"; 1 "use strict";
2 2
3 const LRU = require('lru-cache'); 3 const LRU = require('lru-cache');
4 const moment = require('moment'); 4 const moment = require('moment');
5 5
6 const config = require('../config'); 6 const config = require('../config');
7 const logger = require('../logger'); 7 const logger = require('../logger');
8 8
9 const resendHandlers = LRU({ 9 const resendHandlers = LRU({
10 max: (( config && config.auto_resend && config.auto_resend.max_handler ) ? Number(config.auto_resend.max_handler) : 0) || 5000, 10 max: (( config && config.auto_resend && config.auto_resend.max_handler ) ? Number(config.auto_resend.max_handler) : 0) || 5000,
11 maxAge: 1000 * 3600 * 24 11 maxAge: 1000 * 3600 * 24
12 }); 12 });
13 13
14 function isEnabled() { 14 function isEnabled() {
15 return config && config.auto_resend && Number(config.auto_resend.delay_ms) && Number(config.auto_resend.max_retry); 15 return config && config.auto_resend && Number(config.auto_resend.delay_ms) && Number(config.auto_resend.max_retry);
16 } 16 }
17 17
18 function _resend(task, request) { 18 function _resend(task, request) {
19 const trx_date = moment(task.created).format('YYYYMMDD'); 19 const trx_date = moment(task.created).format('YYYYMMDD');
20 if (trx_date !== moment().format('YYYYMMDD')) { 20 if (trx_date !== moment().format('YYYYMMDD')) {
21 logger.info('SDK-RESEND-DELAY: skip resend because of different trx date', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created}); 21 logger.info('SDK-RESEND-DELAY: skip resend because of different trx date', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created});
22 return; 22 return;
23 } 23 }
24 24
25 logger.verbose('SDK-RESEND-DELAY: Resending trx', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created}); 25 logger.verbose('SDK-RESEND-DELAY: Resending trx', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created});
26 request(task); 26 request(task);
27 } 27 }
28 28
29 function cancel(_task) { 29 function cancel(_task) {
30 const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id; 30 const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id;
31 if (!trx_id) { 31 if (!trx_id) {
32 logger.warn('SDK-RESEND-DELAY: Skipping cancel because of undefined trx_id'); 32 logger.warn('SDK-RESEND-DELAY: Skipping cancel because of undefined trx_id');
33 return; 33 return;
34 } 34 }
35 35
36 const oldHandler = resendHandlers.get(trx_id); 36 const oldHandler = resendHandlers.get(trx_id);
37 if (!oldHandler) { 37 if (!oldHandler) {
38 config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Skipping cancel because of undefined oldHandler', {trx_id: trx_id}); 38 config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Skipping cancel because of undefined oldHandler', {trx_id: trx_id});
39 return; 39 return;
40 } 40 }
41 41
42 const task = oldHandler.task; 42 const task = oldHandler.task;
43 logger.verbose('SDK-RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); 43 logger.verbose('SDK-RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
44 44
45 if (oldHandler.handler) { clearTimeout(oldHandler.handler); } 45 if (oldHandler.handler) { clearTimeout(oldHandler.handler); }
46 resendHandlers.del(trx_id); 46 resendHandlers.del(trx_id);
47 } 47 }
48 48
49 function register(task, request) { 49 function register(task, request) {
50 if (!task.trx_id) { 50 if (!task.trx_id) {
51 logger.warn('SDK-RESEND-DELAY: Invalid task on register') 51 logger.warn('SDK-RESEND-DELAY: Invalid task on register')
52 return; 52 return;
53 } 53 }
54 54
55 if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { 55 if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) {
56 return; 56 return;
57 } 57 }
58 58
59 let retry = config.auto_resend.max_retry; 59 let retry = config.auto_resend.max_retry;
60 const oldHandler = resendHandlers.get(task.trx_id); 60 const oldHandler = resendHandlers.get(task.trx_id);
61 if (oldHandler) { 61 if (oldHandler) {
62 retry = oldHandler.retry - 1; 62 retry = oldHandler.retry - 1;
63 cancel(task); 63 cancel(task);
64 } 64 }
65 65
66 if (retry <= 0) { 66 if (retry <= 0) {
67 logger.verbose('SDK-RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); 67 logger.verbose('SDK-RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
68 cancel(task); 68 cancel(task);
69 return; 69 return;
70 } 70 }
71 71
72 logger.verbose('SDK-RESEND-DELAY: Registering task request', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, delay_ms: config.auto_resend.delay_ms, retry: retry}); 72 logger.verbose('SDK-RESEND-DELAY: Registering task request', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, delay_ms: config.auto_resend.delay_ms, retry: retry});
73 const handlerData = { 73 const handlerData = {
74 handler: setTimeout( 74 handler: setTimeout(
75 function() { _resend(task, request); }, 75 function() { _resend(task, request); },
76 config.auto_resend.delay_ms 76 config.auto_resend.delay_ms
77 ), 77 ),
78 task: task, 78 task: task,
79 retry: retry 79 retry: retry
80 } 80 }
81 81
82 resendHandlers.set(task.trx_id, handlerData); 82 resendHandlers.set(task.trx_id, handlerData);
83 } 83 }
84 84
85 setInterval( 85 setInterval(
86 function() { 86 function() {
87 resendHandlers.prune(); 87 resendHandlers.prune();
88 logger.verbose('SDK-RESEND-DELAY: pruned'); 88 logger.verbose('SDK-RESEND-DELAY: pruned');
89 }, 89 },
90 24 * 3600 * 1000 90 24 * 3600 * 1000
91 ) 91 )
92 92
93 exports.cancel = cancel; 93 exports.cancel = cancel;
94 exports.register = register; 94 exports.register = register;
95 exports.isEnabled = isEnabled; 95 exports.isEnabled = isEnabled;
96 96
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.26.4", 3 "version": "1.26.5",
4 "description": "SDK for Komodo", 4 "description": "SDK for Komodo",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 "test": "mocha", 7 "test": "mocha",
8 "postversion": "git push && git push --tags" 8 "postversion": "git push && git push --tags"
9 }, 9 },
10 "repository": { 10 "repository": {
11 "type": "git", 11 "type": "git",
12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git" 12 "url": "git@gitlab.kodesumber.com:komodo/komodo-sdk.git"
13 }, 13 },
14 "keywords": [ 14 "keywords": [
15 "ppob", 15 "ppob",
16 "payment", 16 "payment",
17 "komodo" 17 "komodo"
18 ], 18 ],
19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>", 19 "author": "Adhidarma Hadiwinoto <gua@adhisimon.org>",
20 "license": "ISC", 20 "license": "ISC",
21 "dependencies": { 21 "dependencies": {
22 "basic-auth": "^2.0.0", 22 "basic-auth": "^2.0.0",
23 "body-parser": "^1.18.2", 23 "body-parser": "^1.18.2",
24 "dot-object": "^1.7.0", 24 "dot-object": "^1.7.0",
25 "express": "^4.16.3", 25 "express": "^4.16.3",
26 "express-session": "^1.15.6", 26 "express-session": "^1.15.6",
27 "json-query": "^2.2.2", 27 "json-query": "^2.2.2",
28 "lru-cache": "^4.1.1", 28 "lru-cache": "^4.1.1",
29 "macaddress": "^0.2.8", 29 "macaddress": "^0.2.8",
30 "moment": "^2.19.1", 30 "moment": "^2.19.1",
31 "node-machine-id": "^1.1.10", 31 "node-machine-id": "^1.1.10",
32 "node-natural-sort": "^0.8.6", 32 "node-natural-sort": "^0.8.6",
33 "numeral": "^2.0.6", 33 "numeral": "^2.0.6",
34 "nunjucks": "^3.0.1", 34 "nunjucks": "^3.0.1",
35 "redis": "^2.8.0", 35 "redis": "^2.8.0",
36 "request": "^2.81.0", 36 "request": "^2.81.0",
37 "sha1": "^1.1.1", 37 "sha1": "^1.1.1",
38 "simple-git": "^1.80.1", 38 "simple-git": "^1.80.1",
39 "strftime": "^0.10.0", 39 "strftime": "^0.10.0",
40 "uniqid": "^4.1.1", 40 "uniqid": "^4.1.1",
41 "uuid": "^3.1.0", 41 "uuid": "^3.1.0",
42 "winston": "^2.3.1", 42 "winston": "^2.3.1",
43 "winston-circular-buffer": "^1.0.0", 43 "winston-circular-buffer": "^1.0.0",
44 "winston-daily-rotate-file": "^1.4.6" 44 "winston-daily-rotate-file": "^1.4.6"
45 } 45 }
46 } 46 }
47 47