#249 Notifications in Rails 3
Jan 17, 2011 | 10 minutes | Rails 3.0
Rails 3 comes with instrument support through the ActiveSupport::Notifications class. Call "subscribe" to listen to notifications, and "instrument" to send them.
- source codeProject Files in Zip (128 KB)
- mp4Full Size H.264 Video (20.8 MB)
- m4vSmaller H.264 Video (12.6 MB)
- webmFull Size VP8 Video (28.6 MB)
- ogvFull Size Theora Video (30.1 MB)
Can i use this, for clear cache?
Yes. I think the convention is to use sweepers/observers though. Assuming you want it async that is, otherwise just call your cache clear method directly and make it more obvious what you're trying to do.
Cool! Many many thanks.
Do you think that notifications could be suitable for pub/sub like jcoglan faye for nodejs and rack does?
Thanks for the screencast, Ryan.
Holding this logic in an initializer seems odd. I'm going to assume you could place that block almost anywhere else.
I have seen a presentation on Domain Driven Design with Rails by Pat Maddox where he plays around with the notion of generating and consuming events as a means to decouple concerns. Makes me wonder if you might do it with these. I know you advise against this, but am wondering how that would feel.
Of course, with the above, you have to consider how it would be tested.
I am also interested in your opinion on a suitable mechanism to implement a model focused pub/sub system for simple events.
I am currently using Observers and custom events (ActiveModel::Observing#notify_observers) but the simplicity of this approach and the RegExp matching capability seems to be a better approach to me.
Since I am only interested in my custom events the Observers are pretty much overkill and the presented method would work quite well. Do you see any other major drawbacks than scattering the code (because I need to extract some core business logic into external places)
Hey Ryan do you believe that the Notifications is a good place to catch exception or Rack is better? Ideally I would to track all exceptions not just the ones that happen during a request for example catch exception when using Runner or when an asyncronous function is executed in a task queue.
Very nice, so it's a bit like objective-c/cocoa does it.
How you unsubscribe of one Notification? I have a very annoying Notification and I really want to remove it, I've try with
$> ActiveSupport::Notifications.unsubscribe( ActiveSupport::LogSubscriber.flushable_loggers.first )
but nothing of this works.
The notifications seem to be of active_record, here is one of the infinity notification that I see
Got notification: #<ActiveSupport::Notifications::Event:0xba9bdd8 @name="sql.active_record ......
I don't know how the notification were activated but I can't disable them, please help
How can I render page_duration time in my layout?
See http://blog.locomotivellc.com/post/4544746167/query-trace for an ActiveRecord subscriber that shows where a query is called from.
it's interesting to hear you say why you suggest not using notifications for app logic. the reasons you listed for not using it are the exact reasons that i want to use it for my app logic. :)
i've got a blog post from yesterday that talks about the scenario that I'm trying to solve and how I would have used the event aggregator pattern if i were building a .net / winforms application. the notifications system is an event aggregator built right into rails, and is perfect for what I need.
if you're interested, here's that blog post: How do you handle simple pub-sub, evented architecture in rails apps?
This speakerdeck presentation by Matt Sanders dives even deeper into
ActiveSupport::Notifications, and is an excellent companion to this Railscast.
I have a problem with rails notifications described here
Doesn't work if I use respond_with (db_runtime is always 0 after POST request)
Not bad actually.