rubyruby-on-rails-4delayed-jobrails-activejob

Rails 4 Delayed_job error - Job failed to load: undefined class/module CustomJob


I've spent several days on this and about 100 hours but can't get the fix.

Here's my setup (using Rails 4.2.8)

class CustomJob < ActiveJob::Base
  def perform(*args)
    filename = args.first
    data = File.read(filename)
    #  process that data
  end
end

When I run Delayed::Job.enqueue CustomJob.new('filename'), I get the error mentioned in the subject. The job is created and added to the db, but the error message is "Job failed..."

I have this line:

require 'custom_job'

in several places including script/delayed_job.rb, config/initializers/delayed_jobs.rb, config/initializers/custom_job.rb and the file in which I'm calling the job.

I also added this: config.autoload_paths+=Dir[Rails.root.join('app','jobs')] config.active_job.queue_adapter = :delayed_job to my config/application.rb file

And this:

config.threadsafe! unless defined? ($rails_rake_task) && $rails_rake_task

I've also restarted my server after every change. And verified that delayed_job was running using:

dir$ RAILS_ENV=development script/delayed_job status
delayed_job: running [pid 64503]

Sources:


Solution

  • I always feel like the answer is obvious...AFTER I figure it out.

    The problem was that I was using a shared database and there were existing workers accessing this DB. Though I was restarting and refreshing my local instance of the server, the other instances were trying to run my jobs and the OTHER workers were causing the error, not my local instance.

    Solution: Ensure that other instances of delayed_job are using the same table as the code you're testing/building/using. If so, use another DB if possible.