GithubHelp home page GithubHelp logo

Comments (10)

rafaelfranca avatar rafaelfranca commented on June 2, 2024 1

Subscribers, including LogSubscribers, like most other registries in the Rails frameworks, are not expected to handle reloads.

Now that lib can be autoloaded, makes this situation difficult. Either we change most of those registries to be reload aware, perhaps making them less performant or we document what can't be reloaded, which is probably everything that was made to be used in libraries, like the mentioned feature.

from rails.

fxn avatar fxn commented on June 2, 2024 1

Stuff that is cached cannot be reloadable. Middleware is an example, this section of the autoloading guide explains this (but I see there is an obsolete mention to lib).

If you still want lib to be reloadable, one way to organize this would be to create lib/log_subscribers and ignore that subdirectory in the autoload_lib call.

from rails.

fxn avatar fxn commented on June 2, 2024

@josegomezr where in the application is the log subscriber loaded?

from rails.

josegomezr avatar josegomezr commented on June 2, 2024

I had something like this:

module Namespace
  extend ActiveSupport::Autoload

  autoload :MyService
end

and in the soruce of namespace/my_service.rb.

require_relative ./log_subscriber.rb

Then I saw the repeating messages and blamed the #attach_to, and with the
results of this research I ended up with:

module Namespace
  extend ActiveSupport::Autoload

  autoload :MyService # this is the class emitting `xxx.namespace` events.

  eager_autoload do
    autoload :LogSubscriber
  end

  # LogSubscriber.attach_to :namespace
  LogSubscriber.reaload_capable_attach_to :namespace
end

from rails.

fxn avatar fxn commented on June 2, 2024

And namespace/my_service.rb, where was it stored? How was Namespace loaded?

Subscribers are typically defined while the application boots, because you want them to be ready, when is yours effectively defined?

Are any of those files in the autoload paths?

from rails.

josegomezr avatar josegomezr commented on June 2, 2024

Are any of those files in the autoload paths?

I believe that's the case. Files are in rails-root/lib/, and application.rb has:

config.autoload_lib(ignore: %w[tasks])

from rails.

fxn avatar fxn commented on June 2, 2024

But, who loads these files, and when? Is loading them triggered from config/initializers?

from rails.

josegomezr avatar josegomezr commented on June 2, 2024

I triggered the behavior from the console by:

Namespace::MyService.new().call()

That should be Zeitwerk loading them (?). I didn't do any require call other than what application.rb already had.

The way I was intending to use the service [and in turn the subscriber] was in a rake task like:

namespace :ns do
  task import_all: :environment do
    Namespace::MyService.new().call()
  end
end

and just bin/rake ns:import_all

p.s: This is a bit far away from my ruby understanding, I apologize if I can't answer clearly 😅

from rails.

fxn avatar fxn commented on June 2, 2024

No prob at all :D, I believe I have an idea of your setup now.

About to fly to RailsConf right now, I'll followup if there's anything else worth sharing. Thank you!

from rails.

fxn avatar fxn commented on June 2, 2024

Stuff that is cached cannot be reloadable. Middleware is an example, this section of the autoloading guide explains this (but I see there is an obsolete mention to lib).

I revised that obsolete paragraph in dcbd6b0. That patch has been backported to 7-1-stable too.

from rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.