resend-delay.js 2.97 KB
var LRU = require('lru-cache');
var winston = require('winston');

var logger;
var topupRequest;
var resendHandlers = LRU({max: 2000, maxAge: 1000 * 3600 * 36});

function init(options) {
    if (!options) {
        console.log('Undefined options, terminating....');
        process.exit(1);
    }

    if (options.config) {
        config = options.config;
    } else {
        console.log('Undefined options.config, terminating....')
        process.exit(1);
    }

    if (options.topupRequest) {
        topupRequest = options.topupRequest;
    } else {
        console.log('Undefined options.topupRequest, terminating....')
        process.exit(1);
    }

    if (options.logger) {
        logger = options.logger;
    } else {
        logger = new winston.Logger({
            transports: [
              new (winston.transports.Console)()
            ]
        });
    }
}

function cancel(task) {
    var requestId;

    if (typeof task === 'string') {
        requestId = task;
    } else {
        requestId = task.requestId;
    }

    if (!requestId) {
        logger.warn('Undefined request id for resend delay, something wrong');
        return;
    }

    var oldHandler = resendHandlers.get(requestId);
    if (!oldHandler) {
        logger.verbose('Undefined old handler for resend delay, skip', {request_id: requestId});
        return;
    }

    logger.verbose('Canceling resend delay', {task: oldHandler.task});

    try {
        if (oldHandler.handler) {
            clearTimeout(oldHandler.handler);
        }
        else {
            logger.warn('Undefined oldhandler.handler on resend delay', {request_id: requestId});
        }
    }
    catch(e) {
        logger.warn('Resend delay got exception on clearTimeout', {request_id: requestId, err: e});
    };

    try {
        resendHandlers.del(requestId);
    }
    catch(e) {
        logger.warn('Resend relay got exception on resendHandlers.del', {request_id: requestId, err: e});
    };
}

function register(task) {
    if (!task.requestId) {
        logger.warn('Invalid task on resendDelay')
        return;
    }

    if (!config || !config.globals || !Number(config.globals.auto_resend_delay_secs) || !Number(config.globals.auto_resend_delay_max)) {
        return;
    }

    var retry = Number(config.globals.auto_resend_delay_max);
    var oldHandler = resendHandlers.get(task.requestId);
    if (oldHandler) {
        retry = oldHandler.retry - 1;
        cancel(task);
    }

    if (retry <= 0) {
        logger.verbose('Resend delay retry exceeded', {task: task});
        cancel(task);
        return;
    }

    logger.verbose('Registering resend delay task request', {task: task, delay: config.globals.auto_resend_delay_secs, retry: retry});
    var handlerData = {
        handler: setTimeout(topupRequest, Number(config.globals.auto_resend_delay_secs) * 1000, task),
        task: task,
        retry: retry
    }

    resendHandlers.set(task.requestId, handlerData);
}


exports.init = init;
exports.cancel = cancel;
exports.register = register;