rails-observers icon indicating copy to clipboard operation
rails-observers copied to clipboard

disable observers without cross-thread side effects

Open markmeeus opened this issue 11 years ago • 6 comments

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.

markmeeus avatar Sep 30 '13 19:09 markmeeus

Thank you. Since we are using Rails 4 could you change your code to use ActiveSupport::PerThreadRegistry

rafaelfranca avatar Sep 30 '13 23:09 rafaelfranca

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[]

markmeeus avatar Oct 02 '13 07:10 markmeeus

Any updates?

simonoff avatar May 30 '14 17:05 simonoff

:+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.

stormsilver avatar May 26 '15 17:05 stormsilver

As an update, we have been running this code in production for many many months now with no problems.

stormsilver avatar Jun 28 '16 17:06 stormsilver

awesome!

markmeeus avatar Jul 04 '16 19:07 markmeeus