Comments (12)
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.
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.
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.
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.
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.
It worked now like a charm! Thanks.
from zeitwerk.
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.
Thanks, I also ran into this problem, and this issue helped me.
from zeitwerk.
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.
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.
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.
No prob, and great to know you're using the lib with Roda :).
from zeitwerk.
Related Issues (20)
- Zeitwerk not loading children of struct namespaces HOT 5
- Zeitwerk not autoload the middleware directory HOT 2
- Zeitwerk, omniauth and multiple DBs (connects_to) HOT 9
- (Re-)Loading modules defined as a root namespace HOT 2
- Ruby 3.3.0-rc1 warning issues HOT 3
- Gemfile/gemspec csv HOT 2
- Getting the source location of a constant while loading it HOT 12
- Gem's loader is being setup multiple times HOT 24
- Autoloading not working in rake task HOT 4
- Zeitwerk::NameError on calling Zeitwerk::Loader.eager_load_all HOT 11
- Deprecation message coming from zeitwerk HOT 2
- Reloading of local gems HOT 27
- drb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.4.0 HOT 3
- Ruby 3.4.0 no longer includes "abbrev" and it suggests zeitwerk to include it HOT 2
- Error when using in Ractor HOT 3
- Namespaced concerns don't load HOT 33
- Zeitwerk::NameError for gem VERSION HOT 8
- Add CHANGELOG.md to gem HOT 3
- `eager_load` clobers previous `eager_load_dir` and `load_file` calls? HOT 6
- In Rails, `app/jobs/seeder/` directory breaks `db:seed` task HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from zeitwerk.