Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use after_initialize instead of to_prepare to prevent subscribing multiple times to events in development environment #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

benjaminwols
Copy link
Contributor

Context

The current config uses .to_prepare which is run after every code reload in development. This is annoying as subscribers get subscribed multiple times to the save event.

One can easily reproduce the problem in development when using the rails console.
Call reload! from the console and publish an event. After reloading once, the subscriber is executed 3 times.

Related tickets

I think this also addresses issue #6

What's inside

Replace .to_prepare with .after_initialize

Checklist:

  • I have added tests
  • I have made corresponding changes to the documentation

I had a look to create a failing test, but the current tests don't test the ActiveSupport.on_load("downstream-events") initializer and I'm unsure how to add it.

…tiple times to events in development environment
@benjaminwols
Copy link
Contributor Author

@palkan Sorry for tagging you, but during Rails World you mentioned that you probably have disabled notifications for this repository.

@@ -6,7 +6,7 @@ module Downstream
class Engine < ::Rails::Engine
config.downstream = Downstream.config

config.to_prepare do
config.after_initialize do
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we only setup subscribers during the app initialization, we lose code updates on reload—the store would still references previous versions of subscribers.

I think, what we should do here is to reset the store and then trigger the hook to let the app to register fresh versions of the subscribers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that would be a better solution. I did some digging, but its quite hard to unsubscribe to all downstream-events since we don't keep a list of subscribers. I was able to make it work, but I'm accessing private methods, so it's very fragile.

config.to_prepare do
  ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers).each_key do |key|
    next unless key.is_a?(String) && key.starts_with?("downstream-events")

    ActiveSupport::Notifications.unsubscribe(key)
  end
  ActiveSupport.run_load_hooks("downstream-events", Downstream)
end

@palkan Do you have any suggestions to improve this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, dealing with AS internals doesn't look stable. I guess, a better solution would be to add some reset functionality to Downstream (extend the Pubsub interface: https://github.com/palkan/downstream/blob/master/lib/downstream/pubsub_adapters/stateless/pubsub.rb).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants