Multithreading problem, uninitialized constant in rake

Hi everyone,

I am having a problem with running rake tasks that involve threads.
Everytime I run it, I get an uninitialized constant with one of my
models (changes models occasionally) seems to be uninitialized.

I have tried to fix this by adding config.threadsafe! in the
application.rb. That did not work. So I tried to add
Rails.application.eager.load! and that did not work. I have tried to put
config.dependency_loading = true if $rails_rake_task but that does not
work. I have tried config.threadsafe! unless $rails_rake_task but that
did not work also.

I was wondering if anyone has come across this problem in which you are
using threads in a rake file and you get the uninitialized constant on
one of your models. Please help! I went through all the
stackoverflow pages related to this and nothing works :frowning: I have ruby
1.9.3-p429, rails 3.2.13, rubygem 2.0.6

Hi everyone,

I am having a problem with running rake tasks that involve threads.

Everytime I run it, I get an uninitialized constant with one of my

models (changes models occasionally) seems to be uninitialized.

I have tried to fix this by adding config.threadsafe! in the

application.rb. That did not work. So I tried to add

Rails.application.eager.load! and that did not work. I have tried to put

config.dependency_loading = true if $rails_rake_task but that does not

work. I have tried config.threadsafe! unless $rails_rake_task but that

did not work also.

Loading classes is inherently a thread unsafe task - you run into all sorts of race conditions. While rails has mechanisms for loading all code upfront (it does that in production by default), and this is one of the things that config.threadsafe! does. My recollection is that this disabled for rake tasks though - I don't recall the precise reasons though. If all else fails, you could use require_dependency to load all of the classes you'll need ahead of time.

Fred.