I've been working on getting our Rails 2.3.8 app running correctly under JRuby. Everything works great until I enable config.threadsafe! in order to achieve the concurrency that JRuby offers. This caused modules and classes in lib/ to no longer autoload.
with config.threadsafe! enabled:
$ ruby script/runner -e production 'p Sim::Sim200Provisioner'
/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in `const_missing': uninitialized constant Sim::Sim200Provisioner (NameError)
from (eval):1
with config.threadsafe! disabled:
$ ruby script/runner -e production 'p Sim::Sim200Provisioner'
Sim::Sim200Provisioner
The file in question is lib/sim/sim200_provisioner.rb where Sim is app/models/sim.rb. Rails normally has no trouble finding and loading the file.
Do I need to manually require all of our libs, or is there a more Rails-like way to handle it that I'm missing?
The documentation of threadsafe!
mentions that it disables automatic dependency loading. The reason is that there might be race conditions during the loading of files if two or more threads both decide they are still missing a certain class.
Instead, you should manually require
all files you need in an initializer
.
Change config.autoload_paths
to config.eager_load_paths
(based on Rails issue #6850 and Force reload! from lib directory in rails 3.2 console, Adding lib to 'config.autoload_paths' in Rails 3 does not autoload my module)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With