8

I've got an app that has been working well for a long time. I'm turning on threadsafe!, and now Delayed Job is not working, saying it can't find one of my model files.

Job failed to load: undefined class/module Foo

This isn't a custom job class I have defined in /lib, this is a model file in app/models

what could be causing this class to not be loaded?

John Bachir
  • 22,495
  • 29
  • 154
  • 227

3 Answers3

12

As posted on the issue you logged, in case others find this SO post first:

I just hit this problem as well... and here's what I found: Rails does not eager load classes if the app is loaded via a rake task (and that's how DJ does its thing).

So what I've done is this snippet of code in my production.rb:

# Enable threaded mode, unless a rake task (likely Delayed Job) is running:
config.threadsafe! unless defined?($rails_rake_task) && $rails_rake_task

Rails sets that global variable when it's loaded by a rake task. Ugly, but seems to be working for me fine now... Of course, if you have rake tasks that are multi-threaded, then this is not ideal, and you should probably invoke Rails.application.eager_load! for those tasks. I'm guessing multi-threaded rake tasks are rare though.

pat
  • 16,116
  • 5
  • 40
  • 46
  • 2
    Just had a similar issue again and discovered something new-- `$rails_rake_task` isn't set in application.rb, so the above line of code has to go into the respective environment configs. – John Bachir Apr 17 '13 at 15:47
  • This worked for me. So when not running in threadsafe mode, it preloads the models? – Joshua Pinter Apr 24 '13 at 23:03
  • This was working for me, except everything is preloading now. Seems rails_rake_task is broken as of rails 3.2, but they fixed it later: https://github.com/rails/rails/issues/4591 . – Colby Blair Sep 28 '13 at 16:17
  • Disclaimer : this comment is not meant to be useful. You made my day ! – Fabrice Carrega Apr 30 '15 at 09:14
3

My jobs were still failing even after using pat's method of not using threadsafe for rake jobs.

I ended up having to use the following manual loading technique found on the GitHub Wiki page:

# file: config/initializers/custom.rb
require 'my_custom_class'
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
0

not really related but i had today exact same error message with delayed job, without enabling threadsafe.

reason and solution was much more trivial - there was another ruby product on our server using delayed job and it's workers didnt have named queue, so they were grabbing random jobs from the queue i was setting up.

Pavel K.
  • 6,697
  • 8
  • 49
  • 80