Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed::Jobs Running Twice on Heroku?

I have a problem running Delayed::Job on Heroku. Most of the jobs that are queued for delayed execution run twice (occassionally just the once). So most of my Sendgrid emails are sent out in duplicate, and most of my ActiveMerchant transactions attempt to settle themselves twice!

We have 2 workers running on our Heroku instance. Coincidence? I think we need two because we also have a cron for backups and stuff.

The jobs that occur twice occur right on top of each other (immeasurably close). It's a race condition, but caused by what?

The specifics are:

$ heroku stack
  aspen-mri-1.8.6
  bamboo-mri-1.9.2
* bamboo-ree-1.8.7
  cedar (beta)

We run 10 web dynos, and have a Ronin database. Plugin-wise we have:

Basic Release Management FREE
Cron Daily Cron FREE
Custom Domains FREE
Expanded Logging FREE
Hostname SSL 20.00 PER MONTH
PG Backups Basic FREE
Sendgrid Pro 20.00 PER MONTH
Shared Database 5MB FREE

In Gemfile:

gem 'rails', '3.0.6'
gem 'delayed_job', :git => 'git://github.com/collectiveidea/delayed_job.git', :tag => 'v2.1.2' # 3.x doesn't seem to work with Heroku

My handlers look like:

class OrderEmailJob < Struct.new(:order_id, :email_type)
  def perform
    OrderMailer.send(email_type, Order.find(order_id)).deliver
  end
end

and are queued like:

Delayed::Job.enqueue OrderEmailJob.new(self.id, :order_confirmation)

I tried lots of different versions of DJ but this was the only one I could get to work, such as it is, on Heroku.

Would really appreciate any suggestions. This is very bad for our site.

like image 842
user874500 Avatar asked Aug 12 '11 13:08

user874500


1 Answers

If you are using Heroku Schedulers, you don't need more workers because of it. Heroku will charge its cron hour independently.

Are you sure the delayed job perform code is not raising exception? If it is, the job will be queued again according to retry variable (Delayed::Worker.max_attempts), so that could cause a reschedule.

like image 197
brunoghisi Avatar answered Oct 13 '22 03:10

brunoghisi