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

move plugin initializer code into Vmdb::Plugins #13941

Closed
wants to merge 1 commit into from

Conversation

durandom
Copy link
Member

the reasoning behind this is, that in a rails console reload! will
reset all classes and also this Vmdb::Plugins singleton

Moving the code that registers all plugins to the initializer makes
sure the all plugins are registered even after a reload.

I think it will save future developers some frustration, when doing stuff in the console or even development mode, when more and more parts of the app rely on the arrays @vmdb_plugins and @registered_automate_domains to be filled.

Its also safe to assume the singleton is accessed after all rails initializers are run - if there is a way to check this, I could add it to the initializer of the singleton class.

@miq-bot add_labels developer, enhacement
@miq-bot assign @bdunne
cc @Fryguy

the reasoning behind this is, that in a rails console `reload!` will
reset all classes and also this Vmdb::Plugins singleton

Moving the code that registers all plugins to the initializer makes
sure the all plugins are registered even after a reload.
@miq-bot
Copy link
Member

miq-bot commented Feb 16, 2017

@durandom Cannot apply the following label because they are not recognized: enhacement

@durandom
Copy link
Member Author

@miq-bot add_labels enhancement

@miq-bot
Copy link
Member

miq-bot commented Feb 16, 2017

Checked commit durandom@de3d2ed with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0
2 files checked, 0 offenses detected
Everything looks good. 🍪

initializer :register_vmdb_plugins, :before => :load_vmdb_settings do
Rails.application.railties.each do |railtie|
next unless railtie.class.name.start_with?("ManageIQ::Providers::") || railtie.try(:vmdb_plugin?)
Vmdb::Plugins.instance.register_vmdb_plugin(railtie)
Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with moving the logic to Vmdb::Plugins#initialize, but I think we would minimally need to call Vmdb::Plugins.instance in this initializer. Otherwise, I don't think the plugins will have registered by the time Vmdb::Settings.init is called, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

calling Vmdb::Plugins.instance will register all plugins. So whoever calls the .instance first, will initialize it.
I think thats the nice improvement, that we dont need an initializer...

--> #0  Vmdb::Plugins.initialize at /Users/hild/src/manageiq/lib/vmdb/plugins.rb:13
    ͱ-- #1  Class.new(*args) at /Users/hild/.rbenv/versions/2.3.1/lib/ruby/2.3.0/singleton.rb:142
    #2  block in #<Class:Vmdb::Plugins>.block in instance at /Users/hild/.rbenv/versions/2.3.1/lib/ruby/2.3.0/singleton.rb:142
    ͱ-- #3  Thread::Mutex.synchronize at /Users/hild/.rbenv/versions/2.3.1/lib/ruby/2.3.0/singleton.rb:140
    #4  #<Class:Vmdb::Plugins>.instance at /Users/hild/.rbenv/versions/2.3.1/lib/ruby/2.3.0/singleton.rb:140
    #5  #<Class:Vmdb::Settings>.template_roots at /Users/hild/src/manageiq/lib/vmdb/settings.rb:118
    #6  #<Class:Vmdb::Settings>.template_sources at /Users/hild/src/manageiq/lib/vmdb/settings.rb:125
    #7  block in #<Class:Vmdb::Settings>.block in build_template at /Users/hild/src/manageiq/lib/vmdb/settings.rb:82
    ͱ-- #8  Kernel.tap at /Users/hild/src/manageiq/lib/vmdb/settings.rb:81
    #9  #<Class:Vmdb::Settings>.build_template at /Users/hild/src/manageiq/lib/vmdb/settings.rb:81
    #10 #<Class:Vmdb::Settings>.build_without_local(resource#Symbol) at /Users/hild/src/manageiq/lib/vmdb/settings.rb:109
    #11 #<Class:Vmdb::Settings>.build(resource#Symbol) at /Users/hild/src/manageiq/lib/vmdb/settings.rb:88
    #12 #<Class:Vmdb::Settings>.for_resource(resource#Symbol) at /Users/hild/src/manageiq/lib/vmdb/settings.rb:57
    #13 #<Class:Vmdb::Settings>.init at /Users/hild/src/manageiq/lib/vmdb/settings.rb:16
    #14 block in block in <class:Application> at /Users/hild/src/manageiq/config/application.rb:118

Copy link
Member

@Fryguy Fryguy Feb 17, 2017

Choose a reason for hiding this comment

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

I don't think this answers the question, but it's difficult to tell. The :load_vmdb_settings initializer creates the toplevel Settings instance and won't be reloaded in production unless something changes the actual setting values. If the plugin is providing its own settings.yml, those have to be in place before the :load_vmdb_settings initializer is called. This is why initializer :register_vmdb_plugins, :before => :load_vmdb_settings

Copy link
Member Author

Choose a reason for hiding this comment

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

reloaded in production

Why would anything be reloaded in production? And even if, by moving the logic into the Plugins#initialize it is always initiliazed correctly vs having it being setup in an initializer, which only is executed at boot time.

providing its own settings.yml, those have to be in place before the :load_vmdb_settings initializer is called

But they are. Because they are in Plugins#initialize - which ultimately makes sure, the plugins are registered before something queries them...

So, I think my response answers the question 🤷‍♂️

@durandom
Copy link
Member Author

@bdunne does my reply make sense? I don't see a reason for keeping an initializer... merge?

Rails.application.railties.each do |railtie|
next unless railtie.class.name.start_with?("ManageIQ::Providers::") || railtie.try(:vmdb_plugin?)
register_vmdb_plugin(railtie)
end
Copy link
Member

Choose a reason for hiding this comment

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

If reload! is reinstantiating this class, then won't it reregister things like settings paths, automate domains and such? This seems dangerous.

Copy link
Member Author

@durandom durandom Feb 17, 2017

Choose a reason for hiding this comment

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

it doesnt? I thought thats whats done in register_vmdb_plugin ?!

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, sorry, I mis-read your comment.

Indeed it would add the engine root again to DescendantLoader.instance.descendants_paths here - I can fix that by making it a proper method that does not allow double entries. But it makes does no real harm for now...

register_automate_domain only adds new AutomateDomain to a newly created array @registered_automate_domains

@durandom
Copy link
Member Author

The initial reason for having this initializer ordering was to make sure the settings initializer had access to Vmdb::Plugins (see #13138)
Which is probably ok if we have only have this one dependency.

What if the : load_inflections initializer suddenly needs Vmdb::Plugins setup before running? Or another one, we dont have yet?
Now, we would have to fine tune the ordering of the :register_vmdb_plugins initializer.

By moving it to def initialize of Vmdb::Plugins we make sure it's properly setup before anyone calls it.

Maybe I'm missing something very subtle here, but for me this reduces dependencies

@durandom
Copy link
Member Author

@Fryguy @bdunne I leave it up to you to merge or close this.

I still think this makes sense because it prevents developer confusion and bug hunting when they reload

Maybe even without a console reload, but also in rails auto class reloading in devel mode

Maybe even in production if a initializer dependency sneaks in (se my last comment)

@durandom
Copy link
Member Author

durandom commented May 9, 2017

@bdunne sorry for resurrecting an old PR. I still think this is a good improvement. If you dont feel like it, just close the PR, no offence taken

@miq-bot
Copy link
Member

miq-bot commented May 10, 2017

This pull request is not mergeable. Please rebase and repush.

@miq-bot
Copy link
Member

miq-bot commented Nov 11, 2017

This pull request has been automatically closed because it has not been updated for at least 6 months.

Feel free to reopen this pull request if these changes are still valid.

Thank you for all your contributions!

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

Successfully merging this pull request may close these issues.

4 participants