Skip to content

Instantly share code, notes, and snippets.

@ssebro
Last active November 10, 2015 13:31
Show Gist options
  • Save ssebro/01c9ea56603861b7b9de to your computer and use it in GitHub Desktop.
Save ssebro/01c9ea56603861b7b9de to your computer and use it in GitHub Desktop.
Generic template for event consumer
//Please ignore this for now; instead, look at the mixed event consumer. This will be updated to match that format later.
var onChange = require('onChange');
onChange(‘canAlarms’).add({
“insert”:function(data){
if(JSON.parse(data).canAlarms[0].archiveRequested){
return createAndSendCanAlarms(data).catch(function(err){
console.error(err);
throw err;
});
}
},
"delete":function ...
"update":function ...
}
).queueOptions({"key":"canAlarms.*","name":"topcon.canAlarms", exclusive:false})
//This would be the to https://gist.github.com/kristofsajdak/d4708d98791365bb2796#file-4-event_consumer-js
=> Uses a wrapper to nack & ack based on promise.
var onMixedChange = require('onMixedChange');
//keys are generated from the collection names.
var orderedAlarmsChangeConsumer = onMixedChange({"name":"ordered.alarms", exclusive:false}).add(
'canAlarms':{
'insert':function(data){
if(JSON.parse(data).canAlarms[0].archiveRequested){
return createAndSendCanAlarms(data).catch(function(err){
console.error(err);
throw err;
});
}
},
'delete':function ...
'update':function ...
},
'trackingData': {
'insert':function(data){
},
'delete':function ...
'update':function ...
},
'equipment': {
'insert':function(data){
},
'delete':function ...
'update':function ...
},
)
//This is a representation of what would be generated by the generic-mixed-event-consumer
var rabbit = require('rabbitConnect');
//Data consumer can be anything that depends on a queue with mixed events - it's just a placeholder.
var dataConsumer = require('dataConsumer');
//In the case of mixedEventConsumer, the dataConsumer will dispatch messages to the change handler functions appropriately.
var exchange = rabbit.topic('change.events');
exchange
.queue({ name: 'ordered.alarms', keys: ['canAlarms.*','trackingData.*', 'equipment.*'] })
.consume(function fwd(data, ack, nack, msg) {
var resource, action;
// extract routingKey from msg, parse and populate resource and action
dataConsumer.push(data,resource,action).then(function(){
ack();
}).catch(function() {
nack();
})
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment