Compare View
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 | config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Preparing', {task: _task, typeof_task: typeof _task}); | 30 | config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Preparing', {task: _task, typeof_task: typeof _task}); |
31 | 31 | ||
32 | const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id; | 32 | const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id; |
33 | if (!trx_id) { | 33 | if (!trx_id) { |
34 | logger.warn('SDK-RESEND-DELAY: Skipping cancel because of undefined trx_id'); | 34 | logger.warn('SDK-RESEND-DELAY: Skipping cancel because of undefined trx_id'); |
35 | return; | 35 | return; |
36 | } | 36 | } |
37 | 37 | ||
38 | const oldHandler = resendHandlers.get(trx_id); | 38 | const oldHandler = resendHandlers.get(trx_id); |
39 | if (!oldHandler) { | 39 | if (!oldHandler) { |
40 | config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Skipping cancel because of undefined oldHandler', {trx_id: trx_id}); | 40 | config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Skipping cancel because of undefined oldHandler', {trx_id: trx_id}); |
41 | return; | 41 | return; |
42 | } | 42 | } |
43 | 43 | ||
44 | const task = oldHandler.task; | 44 | const task = oldHandler.task; |
45 | logger.verbose('SDK-RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | 45 | logger.verbose('SDK-RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); |
46 | 46 | ||
47 | if (oldHandler.handler) { clearTimeout(oldHandler.handler); } | 47 | if (oldHandler.handler) { clearTimeout(oldHandler.handler); } |
48 | resendHandlers.del(trx_id); | 48 | resendHandlers.del(trx_id); |
49 | } | 49 | } |
50 | 50 | ||
51 | function register(task, request) { | 51 | function register(task, request) { |
52 | if (!task.trx_id) { | 52 | if (!task.trx_id) { |
53 | logger.warn('SDK-RESEND-DELAY: Invalid task on register') | 53 | logger.warn('SDK-RESEND-DELAY: Invalid task on register') |
54 | return; | 54 | return; |
55 | } | 55 | } |
56 | 56 | ||
57 | if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { | 57 | if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { |
58 | return; | 58 | return; |
59 | } | 59 | } |
60 | 60 | ||
61 | let retry = config.auto_resend.max_retry; | 61 | let retry = config.auto_resend.max_retry; |
62 | const oldHandler = resendHandlers.get(task.trx_id); | 62 | const oldHandler = resendHandlers.get(task.trx_id); |
63 | if (oldHandler) { | 63 | if (oldHandler) { |
64 | retry = oldHandler.retry - 1; | 64 | retry = oldHandler.retry - 1; |
65 | cancel(task); | 65 | cancel(task); |
66 | } | 66 | } |
67 | 67 | ||
68 | if (retry <= 0) { | 68 | if (retry <= 0) { |
69 | logger.verbose('SDK-RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | 69 | logger.verbose('SDK-RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); |
70 | cancel(task); | 70 | cancel(task); |
71 | return; | 71 | return; |
72 | } | 72 | } |
73 | 73 | ||
74 | 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}); | 74 | 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}); |
75 | const handlerData = { | 75 | const handlerData = { |
76 | handler: setTimeout( | 76 | handler: setTimeout( |
77 | function() { _resend(task, request); }, | 77 | function() { _resend(task, request); }, |
78 | config.auto_resend.delay_ms | 78 | config.auto_resend.delay_ms |
79 | ), | 79 | ), |
80 | task: task, | 80 | task: task, |
81 | retry: retry | 81 | retry: retry |
82 | } | 82 | } |
83 | 83 | ||
84 | resendHandlers.set(task.trx_id, handlerData); | 84 | resendHandlers.set(task.trx_id, handlerData); |
85 | } | 85 | } |
86 | 86 | ||
87 | setInterval( | 87 | setInterval( |
88 | function() { | 88 | function() { |
89 | resendHandlers.prune(); | 89 | resendHandlers.prune(); |
90 | logger.verbose('SDK-RESEND-DELAY: pruned'); | 90 | logger.verbose('SDK-RESEND-DELAY: pruned'); |
91 | }, | 91 | }, |
92 | 24 * 3600 * 1000 | 92 | 24 * 3600 * 1000 |
93 | ) | 93 | ) |
94 | 94 | ||
95 | exports.cancel = cancel; | 95 | exports.cancel = cancel; |
96 | exports.register = register; | 96 | exports.register = register; |
97 | exports.isEnabled = isEnabled; | 97 | exports.isEnabled = isEnabled; |
98 | 98 |
package.json
1 | { | 1 | { |
2 | "name": "komodo-sdk", | 2 | "name": "komodo-sdk", |
3 | "version": "1.26.6", | 3 | "version": "1.26.7", |
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 |