Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

model classes not loading in Delayed Job when using threadsafe

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?

like image 948
John Bachir Avatar asked Sep 20 '12 18:09

John Bachir


2 Answers

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.

like image 175
pat Avatar answered Nov 11 '22 20:11

pat


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'
like image 25
Joshua Pinter Avatar answered Nov 11 '22 19:11

Joshua Pinter