Compare View
Commits (5)
Changes
Showing 3 changed files Side-by-side Diff
gateway/cancel-push-server.js
... | ... | @@ -0,0 +1,58 @@ |
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); |
gateway/resend-delay.js
1 | 1 | "use strict"; |
2 | 2 | |
3 | -var LRU = require('lru-cache'); | |
3 | +const LRU = require('lru-cache'); | |
4 | +const moment = require('moment'); | |
4 | 5 | |
5 | 6 | const config = require('./config'); |
6 | 7 | const logger = require('./logger'); |
7 | 8 | |
8 | -const resendHandlers = LRU({max: 2000, maxAge: 1000 * 3600 * 36}); | |
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 | 13 | |
10 | -function cancel(task) { | |
11 | - const trx_id = ( typeof task === 'string' ) ? task : task.trx_id; | |
14 | +function _resend(task, request) { | |
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 | 29 | const oldHandler = resendHandlers.get(trx_id); |
15 | 30 | if (!oldHandler) { return; } |
16 | 31 | |
17 | 32 | const task = oldHandler.task; |
18 | - logger.verbose('Canceling resend delay', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | |
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 | 36 | resendHandlers.del(trx_id); |
... | ... | @@ -23,7 +38,7 @@ function cancel(task) { |
23 | 38 | |
24 | 39 | function register(task, request) { |
25 | 40 | if (!task.trx_id) { |
26 | - logger.warn('Invalid task on resendDelay') | |
41 | + logger.warn('RESEND-DELAY: Invalid task on register') | |
27 | 42 | return; |
28 | 43 | } |
29 | 44 | |
... | ... | @@ -39,14 +54,17 @@ function register(task, request) { |
39 | 54 | } |
40 | 55 | |
41 | 56 | if (retry <= 0) { |
42 | - logger.verbose('Resend delay retry exceeded', {trx_id: task.trx_id, destination: task.destination, product: task.product, remote_product: task.remote_product}); | |
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 | 60 | } |
46 | 61 | |
47 | - logger.verbose('Registering resend delay 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}); | |
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 | - handler: setTimeout(request, config.auto_resend.delay_ms, task), | |
64 | + handler: setTimeout( | |
65 | + function() { _resend(task, request); }, | |
66 | + config.auto_resend.delay_ms | |
67 | + ), | |
50 | 68 | task: task, |
51 | 69 | retry: retry |
52 | 70 | } |
... | ... | @@ -54,5 +72,13 @@ function register(task, request) { |
54 | 72 | resendHandlers.set(task.trx_id, handlerData); |
55 | 73 | } |
56 | 74 | |
75 | +setInterval( | |
76 | + function() { | |
77 | + resendHandlers.prune(); | |
78 | + logger.verbose('RESEND-DELAY: pruned'); | |
79 | + }, | |
80 | + 24 * 3600 * 1000 | |
81 | +) | |
82 | + | |
57 | 83 | exports.cancel = cancel; |
58 | 84 | exports.register = register; |