Skip to content

Instantly share code, notes, and snippets.

@ssebro
Forked from kristofsajdak/complex1.js
Last active November 10, 2015 15:57
Show Gist options
  • Save ssebro/b7bf9d018aa3f0bd0754 to your computer and use it in GitHub Desktop.
Save ssebro/b7bf9d018aa3f0bd0754 to your computer and use it in GitHub Desktop.
Generic template for event consumer
//This is a representation of what would be generated by the generic-event-consumer
var rabbit = require('rabbitConnect');
//Data consumer can be anything that depends on a queue - it's just a placeholder.
var dataConsumer = require('dataConsumer');
//The dataConsumer will dispatch messages to the change handler functions appropriately.
var exchange = rabbit.topic('change.events');
exchange
.queue({ name: 'es.sync', 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();
})
}
);
var queueConsumer = require('generic-event-consumer');
//keys are generated from the collection names.
var orderedAlarmsChangeConsumer = queueConsumer({exchange:"change.events", "name":"es.sync", exclusive:false}).handle(
'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 ...
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment