简体   繁体   中英

Delayed Job on Heroku intermittently fails with 'Uninitialized constant'

I have a Rails 3.2.6 app on Heroku with a worker dyno for Delayed Job 2.1.4. When I run a worker locally, no problems occur. But when I run it on Heroku, it seems like it will behave erratically and fail some jobs. At first I thought the pattern had to do with the worker dyno spinning down and coming back up, because after a few tries, the jobs start working again. If you wait too long, the jobs start failing. But now I'm not sure it has to do with spinning down, since just 5 minutes in between jobs caused the error again. However, keeping the jobs close to each other seems to increase the chances of successful execution.

The error is as follows:

irb(main):052:0> Delayed::Job.last.last_error
  Delayed::Backend::ActiveRecord::Job Load (9.9ms)  SELECT `delayed_jobs`.* FROM `delayed_jobs` ORDER BY `delayed_jobs`.`id` DESC LIMIT 1
=> "{Job failed to load: uninitialized constant FacebookTimelinePublisher. ...

In this case FacebookTimelinePublisher is a PORO class in the app/publishers directory which I've included in the autoload path for safe measure.

Thanks for any suggestions you might have.

[UPDATE]

I've changed the way I set up the Delayed Job to use Delayed::Job.enqueue instead of #delay . This has now moved the problem forward in that Delayed Job correctly finds the Job class, but now when it's initializing itself, it is not finding Rails models. Still, the "intermittent" nature of the problem persists, as sometimes the job executes without a hitch.

"{Job failed to load: uninitialized constant ShareObject. Handler: \"--- !ruby/struct:FacebookTimelinePublisher \\nshare_object: !ruby/ActiveRecord:ShareObject \\n  attributes: \\n  ...."

Not sure about the Heroku environment, but I solved a similar problem by requiring a custom class in an initializer (I used the delayed_job config initializer):

require "#{Rails.root}/app/publishers/facebook_timeline_publisher.rb"

See also: Delayed::Job with modules

It seems like the problem was related to the gem version. I upgraded to delayed_job 3.0.4 and also installed delayed_job_active_record (0.3.3), so I'm not sure which of these two fixed the problem, but now all jobs get executed successfully.

Hope this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM