为节点微服务编写重试逻辑的最佳方法是什么?

时间:2018-03-06 06:00:20

标签: javascript node.js publish-subscribe

如果Redis pubsub失败,我想写一些登录信息。我想完成以下任务:

  1. 记录错误
  2. 最多重试3次
  3. 如果3次尝试后发送失败
    • 记录错误
    • 回复错误消息(serviceUnavailable)
  4. 这是我的代码:

    'use strict';
    
    const Boom = require('boom');
    const resolver = require('debug-resolve')(__filename, process.cwd());
    const Debug = require('debug')(resolver);
    const {
        auto,
        map,
        mapSeries
    } = require('async');
    
    const emitEvent = ({
        eventName,
        log,
        payload,
        pubSub,
        sqs,
        cb
    
    }) => {
    
        auto({
            redisEventEmitter (autoCb) {
    
                try {
    
                    Debug('Trying to emit new event received: %s', eventName);
                    payload.eventName = eventName;
                    pubSub.emit(`event-system:${eventName}`, payload);
    
                    // @TODO: Add retry logic
                    autoCb();
    
                } catch (error) {
    
                    Debug(
                        'There was an error emitting new event (%s): %s',
                        eventName,
                        JSON.stringify(error)
                    );
                    Debug('Error %s', error);
                    log(
                        ['RedisPubSub', 'error'],
                        `There was an error emitting new event (${eventName}): ${error.message}`
                    );
    
                    // @TODO: Add retry logic
                    autoCb();
    
                }
    
            },
            eventSqs (autoCb) {
    
                try {
    
                    Debug('Trying to publish new event received: %s', eventName);
                    payload.eventName = eventName;
                    sqs.publish({
                        messages: {
                            body: JSON.stringify(payload)
                        },
                        cb: autoCb
                    });
    
                } catch (error) {
    
                    Debug(
                        'There was an error publish new event (%s): %s',
                        eventName,
                        JSON.stringify(error)
                    );
                    Debug('Error %s', error);
                    log(
                        ['EventSQS', 'error'],
                        `There was an error publish new event (${eventName}): ${error.message}`
                    );
    
                    // @TODO: Add retry logic
                    autoCb();
    
                }
    
            }
        }, (err) => {
    
            if (err) {
    
                Debug('Error sending event %s', JSON.stringify(err));
                Debug('Error %s', err);
                return cb(
                    Boom.badImplementation(err.message, {
                        status: 'failed',
                        code: err.code,
                        message: err.message,
                        ts: Date.now()
                    })
                );
    
            } else {
    
                Debug('Submission successful');
                return cb(null, {
                    eventName,
                    status: 'submitted',
                    ts: Date.now()
                });
    
            }
    
        });
    
    };

    此逻辑在Node JS v8上运行。如果我能够为RedisAWS-SQS提供合理的逻辑,那就太棒了。

1 个答案:

答案 0 :(得分:2)

您可以查找以下代码示例

var retryCount = 0;
var errorMessage = []
redisEventEmitter(autoCb) {

    if (retryCount => 3)
        return errorMessage
    else {
        try {
            payload.eventName = eventName;
            pubSub.emit(`event-system:${eventName}`, payload);
            // @TODO: Add retry logic
            autoCb();
        } catch (error) {
            retryCount ++;
            log(
                ['RedisPubSub', 'error'],
                `There was an error emitting new event (${eventName}): ${error.message}`
            );
            errorMessage = ['RedisPubSub', 'error']
            redisEventEmitter(autoCb)
            autoCb();
        }
    }

}