如果Redis pubsub
失败,我想写一些登录信息。我想完成以下任务:
这是我的代码:
'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上运行。如果我能够为Redis
和AWS-SQS
提供合理的逻辑,那就太棒了。
答案 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();
}
}
}