resend-delay.js
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const LRU = require('lru-cache');
const moment = require('moment');
const logger = require('tektrans-logger');
const config = require('../config');
const resendHandlers = LRU({
max: (( config && config.auto_resend && config.auto_resend.max_handler ) ? Number(config.auto_resend.max_handler) : 0) || 5000,
maxAge: 1000 * 3600 * 24
});
function isEnabled() {
return config && config.auto_resend && Number(config.auto_resend.delay_ms) && Number(config.auto_resend.max_retry);
}
function _resend(task, request) {
const trx_date = moment(task.created).format('YYYYMMDD');
if (trx_date !== moment().format('YYYYMMDD')) {
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});
return;
}
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});
request(task);
}
function cancel(_task) {
config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Preparing', {task: _task, typeof_task: typeof _task});
const trx_id = ( typeof _task === 'object' ) ? _task.trx_id : _task;
if (!trx_id) {
logger.warn('SDK-RESEND-DELAY: Skipping cancel because of undefined trx_id');
return;
}
const oldHandler = resendHandlers.get('TASK_' + trx_id);
if (!oldHandler) {
config && config.auto_resend && config.auto_resend.debug && logger.verbose('SDK-RESEND-DELAY: Skipping cancel because of undefined oldHandler', {trx_id: trx_id});
return;
}
const task = oldHandler.task;
logger.verbose('SDK-RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
if (oldHandler.handler) { clearTimeout(oldHandler.handler); }
resendHandlers.del('TASK_' + trx_id);
}
function register(task, request) {
if (!task.trx_id) {
logger.warn('SDK-RESEND-DELAY: Invalid task on register')
return;
}
if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) {
return;
}
let retry = config.auto_resend.max_retry;
const oldHandler = resendHandlers.get('TASK_' + task.trx_id);
if (oldHandler) {
retry = oldHandler.retry - 1;
cancel(task);
}
if (retry <= 0) {
logger.verbose('SDK-RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
cancel(task);
return;
}
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});
const handlerData = {
handler: setTimeout(
function() { _resend(task, request); },
config.auto_resend.delay_ms
),
task: task,
retry: retry
}
resendHandlers.set('TASK_' + task.trx_id, handlerData);
}
setInterval(
function() {
resendHandlers.prune();
logger.verbose('SDK-RESEND-DELAY: pruned');
},
24 * 3600 * 1000
)
exports.cancel = cancel;
exports.register = register;
exports.isEnabled = isEnabled;