Open
Description
I'm writing a publish task for a rascal and need to respond to 'success' and 'error' events. However it's possible that I may get multiple events. In this circumstance it would make sense to emit a workflow exception rather than execute the done callback twice.
var _ = require('lodash')
module.exports = function(node) {
return function (context) {
execute.annotations = { inject: ['broker', 'publication', 'payload', 'routingKey', 'options']}
function execute(broker, publicationId, payload, routingKey, options, done) {
var self = this
broker = broker || context.broker
options = _.defaults({}, { routingKey: routingKey }, options)
broker.publish(publicationId, payload, options, function(err, publication) {
if (err) return done(err)
publication.on('success', onSuccess).on('error', onError)
})
function onSuccess(messageId) {
respond(null, messageId)
respond = function() {
emitError(new Error('Success called after the task has already completed'))
}
}
function onError(err) {
respond(err)
respond = emitError
}
function emitError(err) {
self.workflow.emit('error', err)
}
function respond(err, messageId) {
done(err, messageId)
}
}
return execute
}
}
Metadata
Assignees
Labels
No labels
Activity