rails-observers
rails-observers copied to clipboard
disable observers without cross-thread side effects
The current implementation of enabling/disabling observers has side effects into other threads. I understand that the set of observers per model should be global, but disabling an observer is usually done for a specific reason and a specific scope.
When doing this:
Heisenberg.observers.disable :uncle_frank do
#code with observer disabled
end
Other threads using the Heisenberg model will have the observer disabled until the block is done with it.
This pull requests doesn't change the global behaviour of observers, only the disabling/enabling is per thread.
Thank you. Since we are using Rails 4 could you change your code to use ActiveSupport::PerThreadRegistry
Hi @rafaelfranca ,
I didn't know about that Registry class, but I implemented it in my last commit.
Since these PerThreadRegistries are singletons, I had to use 1 registry for all instances of observer_array. That's why it's disabled_observers_per_class[]
Any updates?
:+1: this is a problem for us in production and our current solution is to fork whenever we need to disable callbacks. :(
If you still would like to see changes to this and @markmeeus isn't available or doesn't have time to make them I would be glad to take over the PR.
As an update, we have been running this code in production for many many months now with no problems.
awesome!