GithubHelp home page GithubHelp logo

Reload is not worked with roda. about zeitwerk HOT 12 CLOSED

zw963 avatar zw963 commented on August 28, 2024
Reload is not worked with roda.

from zeitwerk.

Comments (12)

zw963 avatar zw963 commented on August 28, 2024 9

This is the best auto reload solution among the nearly ten solutions I tried, thank you!

To make it easier for other people to read or copy it, following is this awesome solution.

# config.ru
require 'roda'
require 'zeitwerk'

loader = Zeitwerk::Loader.new
# loader.logger = method(:puts)
loader.push_dir(__dir__)
loader.setup

if ENV['RACK_ENV'] == 'production'
  Zeitwerk::Loader.eager_load_all
  run App
else
  run ->(env) {
    loader.reload
    App.call(env)
  }
end

from zeitwerk.

fxn avatar fxn commented on August 28, 2024 2

Followup (still on my phone). If you want to reload on each request, the reload call would go at the beginning of the request, something like (untested)

# config.ru
loader = Zeitwerk::Loader.new
loader.push_dir(__dir__)
loader.setup

run ->(env) {
  loader.reload
  App.call(env)
}

You have to remove then loader calls in the routes.

from zeitwerk.

fxn avatar fxn commented on August 28, 2024 1

Probably negligible, but this alternative would be πŸ‘Œ (untested):

if ENV['RACK_ENV'] == 'production'
  Zeitwerk::Loader.eager_load_all
  run App
else
  run ->(env) {
    loader.reload
    App.call(env)
  }
end

See? Since you know at boot which is the environment, you can fine tune exactly which application you want to run, the lambda wrapper, or App. Also, this gives you a natural place for eager loading.

from zeitwerk.

zw963 avatar zw963 commented on August 28, 2024 1

Hi, the above idea was extract into a ruby gem since one years ago, with extra bonus: autoload automatically if some file was changed. Please check: https://github.com/zw963/hot_reloader.

A dead simple ruby code hot reloader wrap around zeitwerk and listen.

hope can help someone develop with Roda more easiler, anyway, we don't want missing autoload feature from rails anymore!

from zeitwerk.

fxn avatar fxn commented on August 28, 2024

On my phone.

You don’t see the changes because Rack evaluated App in config.ru at boot and therefore it is using always the same class object.

For it to work, you need to add an indirection: It could be as easy as running a lambda run ->(env) { App.call(env) } (untested, off the top of my head).

Please let me know if that helps.

from zeitwerk.

zw963 avatar zw963 commented on August 28, 2024

It worked now like a charm! Thanks.

from zeitwerk.

zw963 avatar zw963 commented on August 28, 2024

Still have a question about performance.

for roda, we don't need hot reloader in production,
if we don't need eager_load too, for this case,
just comment loader.reload as this:

# config.ru
require 'roda'
require 'zeitwerk'

loader = Zeitwerk::Loader.new
loader.logger = method(:puts)
loader.push_dir(__dir__)
loader.setup

run ->(env) {
  loader.reload unless ENV['RACK_ENV'] == 'production'
  App.call(env)
}

is there have any performance penalty?

Thanks

from zeitwerk.

will avatar will commented on August 28, 2024

Thanks, I also ran into this problem, and this issue helped me.

from zeitwerk.

will avatar will commented on August 28, 2024

Actually, I ran into a problem where Zeitwerk was saying that a file didn't define a constant, even though it did, while using this as my config.ru with puma and Roda.

# frozen_string_literal: true

require_relative "config/environment" # not a rails app, but using the same conventions

if Config.allow_code_reloading?
  run ->(env) do
    RELOAD.call # this is defined in a zeitwerk initializer
    Web.call(env)
  end
else
  run Web.freeze.app
end

in the initializer

# ...
if Config.allow_code_reloading?
  loader.enable_reloading

  RELOAD = -> { loader.reload }
end

Using puma with a max of one thread -t 1:1 fixed the problem, and also wrapping the reload and call in a mutex also fixed it.

if Config.allow_code_reloading?
  mutex = Mutex.new
  run ->(env) do
    mutex.synchronize do
      RELOAD.call
      Web.call(env)
    end
  end
else
  run Web.freeze.app
end

which was confusing to me, because I was under the impression that Zeitwerk was thread safe. Maybe not on reloading?

from zeitwerk.

fxn avatar fxn commented on August 28, 2024

Exactly:

Zeitwerk is also able to reload code, which may be handy while developing web applications. Coordination is needed to reload in a thread-safe manner. The documentation below explains how to do this.

See also https://github.com/fxn/zeitwerk#reloading.

from zeitwerk.

will avatar will commented on August 28, 2024

Thanks! I swear I read through the readme a few times while putting this into my project, but must have had that part slip out of my memory :)

from zeitwerk.

fxn avatar fxn commented on August 28, 2024

No prob, and great to know you're using the lib with Roda :).

from zeitwerk.

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.