EventBus in a Sinatra app

I had a sinatra app (the Be My Eyes app), that I thought had a bit too much code in the route file.
So I investigated how to make that better.

What I came up with was that it would be great to have some kind of EventBus, where I could send events out and respond in a very decoupled manner.
It would be nice to be able to run the event handlers asynchronous, but that was not a hard requiriment from the beginning.
I searched on github and came up with eventbus by Kevin Rutherford.
I asked on the lrug mail-list about peoples experience with event
bus. Not a lot of people had tried it out, be at least one person could recommend Kevins work, so I took the plunge and installed the gem. (Furthermore an I could see that a "sister" gem exists that will handle events in sidekick, so the asynchronous part got a checkmark as well)

I can highly recommend the gem, I have had zero problems with it.

I setup all the events in a method, when the app starts:
[App setup] (https://github.com/bemyeyes/bemyeyes-server/blob/master/app helpers/app_setup.rb#L8)

And I placed all event handlers in their own directory.
These two things gives a brilliant overview of what is going on in the app. It is very easy to find out what is going on when an event fires, and the code that handles the event is very simple - usually a few lines.

This EventBus setup has been running in production for a month or so now and we have had zero problems with it.

Now the business is starting to ask questions about what is happening in the app business wise. This should come as no surprise.
So I started to think about how I could gather the data needed to answer their questions. I thought about lots of clever solutions, until it dawned on me, that if I could just save the events send by the event bus, I would have all the data needed by the business.
This turned out to be a very simple task.
I simply made an event handler that response to all events, and set it up in the app_setup file. I then had to make a mongomapper model, that can save these events to the database and everything is fine.
I can now query the event log documents about what happens in the app. And the core objects does not have to be polluted with business event data.

I think this goes to show that using an EventBus is a good asset in your architecture.

Image Courtesey of Dave Conner

comments powered by Disqus