Compare View

switch
from
...
to
 
Commits (5)

Changes

Showing 3 changed files Inline Diff

gateway/cancel-push-server.js
File was created 1 "use strict";
2
3 const express = require('express');
4 const bodyParser = require('body-parser');
5
6 const pull = require('./pull');
7 const config = require('../config');
8 const logger = require('../logger');
9 const matrix = require('../matrix');
10
11 const resendDelay = require('./resend-delay');
12
13 const app = express();
14
15 function init() {
16 if (!config || !config.push_server || !config.push_server.apikey || !config.push_server.cancel || !config.push_server.cancel.url || !config.push_server.cancel.port) {
17 return;
18 }
19
20 app.listen(config.push_server.cancel.port, function () {
21 logger.info('Cancel server listening', {port: config.push_server.cancel.port});
22 });
23 }
24 init();
25
26 function isValidApikey(req, res, next) {
27 if (config.push_server && config.push_server.apikey && (req.params.apikey === config.push_server.apikey)) {
28 next();
29 }
30 else {
31 res.end('INVALID_APIKEY');
32 }
33 }
34
35 function cancelHandler(req, res, next) {
36
37 if (!partner) {
38 logger.warn('PUSH-CANCEL: Undefined partner, skipped');
39 res.end('UNDEFINED_PARTNER');
40 return;
41 }
42
43 let task = req.body;
44
45 if (!task || !task.trx_id) {
46 logger.warn('PUSH-CANCEL: Invalid task');
47 res.end('INVALID_TASK');
48 return;
49 }
50
51 logger.verbose('PUSH-CANCEL: Got cancel request', {trx_id: task.trx_id});
52
53 resendDelay.cancel(task.trx_id);
54 }
55
56 app.use(bodyParser.json());
57 app.use('/apikey/:apikey', isValidApikey);
58 app.use('/apikey/:apikey/cancel', cancelHandler);
1 "use strict"; 59
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({
10 max: (( config && config.auto_resend && config.auto_resend.max_handler ) ? Number(config.auto_resend.max_handler) : 0) || 5000,
11 maxAge: 1000 * 3600 * 24
12 });
9 const resendHandlers = LRU({ 13
10 max: (( config && config.auto_resend && config.auto_resend.max_handler ) ? Number(config.auto_resend.max_handler) : 0) || 5000, 14 function _resend(task, request) {
11 maxAge: 1000 * 3600 * 24 15 const trx_date = moment(task.created).format('YYYYMMDD');
16 if (trx_date !== moment().format('YYYYMMDD')) {
17 logger.info('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});
18 return;
19 }
20
21 logger.verbose('RESEND-DELAY: Resending trx', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created});
22 request(task);
23 }
24
25 function cancel(_task) {
26 const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id;
12 }); 27 if (!trx_id) { return; }
13 28
14 function _resend(task, request) { 29 const oldHandler = resendHandlers.get(trx_id);
15 const trx_date = moment(task.created).format('YYYYMMDD'); 30 if (!oldHandler) { return; }
16 if (trx_date !== moment().format('YYYYMMDD')) { 31
17 logger.info('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}); 32 const task = oldHandler.task;
18 return; 33 logger.verbose('RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
19 } 34
20 35 if (oldHandler.handler) { clearTimeout(oldHandler.handler); }
21 logger.verbose('RESEND-DELAY: Resending trx', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product, created: task.created}); 36 resendHandlers.del(trx_id);
22 request(task); 37 }
23 } 38
24 39 function register(task, request) {
25 function cancel(_task) { 40 if (!task.trx_id) {
26 const trx_id = ( typeof _task === 'string' ) ? _task : _task.trx_id; 41 logger.warn('RESEND-DELAY: Invalid task on register')
27 if (!trx_id) { return; } 42 return;
28 43 }
29 const oldHandler = resendHandlers.get(trx_id); 44
30 if (!oldHandler) { return; } 45 if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) {
31 46 return;
32 const task = oldHandler.task; 47 }
33 logger.verbose('RESEND-DELAY: Canceling task', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); 48
34 49 let retry = config.auto_resend.max_retry;
35 if (oldHandler.handler) { clearTimeout(oldHandler.handler); } 50 const oldHandler = resendHandlers.get(task.trx_id);
36 resendHandlers.del(trx_id); 51 if (oldHandler) {
37 } 52 retry = oldHandler.retry - 1;
38 53 cancel(task);
39 function register(task, request) { 54 }
40 if (!task.trx_id) { 55
41 logger.warn('RESEND-DELAY: Invalid task on register') 56 if (retry <= 0) {
42 return; 57 logger.verbose('RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product});
43 } 58 cancel(task);
44 59 return;
45 if (!request || !config || !config.auto_resend || !Number(config.auto_resend.delay_ms) || !Number(config.auto_resend.max_retry)) { 60 }
46 return; 61
47 } 62 logger.verbose('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});
48 63 const handlerData = {
49 let retry = config.auto_resend.max_retry; 64 handler: setTimeout(
65 function() { _resend(task, request); },
66 config.auto_resend.delay_ms
67 ),
50 const oldHandler = resendHandlers.get(task.trx_id); 68 task: task,
51 if (oldHandler) { 69 retry: retry
52 retry = oldHandler.retry - 1; 70 }
53 cancel(task); 71
54 } 72 resendHandlers.set(task.trx_id, handlerData);
55 73 }
56 if (retry <= 0) { 74
75 setInterval(
76 function() {
77 resendHandlers.prune();
78 logger.verbose('RESEND-DELAY: pruned');
79 },
80 24 * 3600 * 1000
81 )
82
57 logger.verbose('RESEND-DELAY: Retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); 83 exports.cancel = cancel;
58 cancel(task); 84 exports.register = register;
59 return; 85
1 { 1 {
2 "name": "komodo-sdk", 2 "name": "komodo-sdk",
3 "version": "1.21.0", 3 "version": "1.22.0",
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 "express": "^4.16.3", 24 "express": "^4.16.3",
25 "express-session": "^1.15.6", 25 "express-session": "^1.15.6",
26 "lru-cache": "^4.1.1", 26 "lru-cache": "^4.1.1",
27 "macaddress": "^0.2.8", 27 "macaddress": "^0.2.8",
28 "moment": "^2.19.1", 28 "moment": "^2.19.1",
29 "node-machine-id": "^1.1.10", 29 "node-machine-id": "^1.1.10",
30 "node-natural-sort": "^0.8.6", 30 "node-natural-sort": "^0.8.6",
31 "numeral": "^2.0.6", 31 "numeral": "^2.0.6",
32 "nunjucks": "^3.0.1", 32 "nunjucks": "^3.0.1",
33 "redis": "^2.8.0", 33 "redis": "^2.8.0",
34 "request": "^2.81.0", 34 "request": "^2.81.0",
35 "sha1": "^1.1.1", 35 "sha1": "^1.1.1",
36 "simple-git": "^1.80.1", 36 "simple-git": "^1.80.1",
37 "strftime": "^0.10.0", 37 "strftime": "^0.10.0",
38 "uniqid": "^4.1.1", 38 "uniqid": "^4.1.1",
39 "uuid": "^3.1.0", 39 "uuid": "^3.1.0",
40 "winston": "^2.3.1", 40 "winston": "^2.3.1",
41 "winston-circular-buffer": "^1.0.0", 41 "winston-circular-buffer": "^1.0.0",
42 "winston-daily-rotate-file": "^1.4.6" 42 "winston-daily-rotate-file": "^1.4.6"
43 } 43 }
44 } 44 }
45 45