Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to load modules/classes from lib/ when using the config.threadsafe! option?

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?

like image 523
Alex Avatar asked Jul 26 '10 16:07

Alex


2 Answers

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.

like image 180
molf Avatar answered Sep 21 '22 05:09

molf


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)

like image 30
E. Sambo Avatar answered Sep 24 '22 05:09

E. Sambo