延迟::作业在 Heroku 上运行两次?
我在 Heroku 上运行 Delayed::Job 时遇到问题。大多数排队等待延迟执行的作业都会运行两次(有时只运行一次)。因此,我的大多数 Sendgrid 电子邮件都是重复发送的,而我的大多数 ActiveMerchant 交易都尝试自行结算两次!
我们有 2 个工作线程在 Heroku 实例上运行。巧合?我认为我们需要两个,因为我们还有一个用于备份和其他内容的 cron。
发生两次的作业恰好重叠发生(非常接近)。这是一种竞争条件,但是由什么引起的呢?
具体细节是:
$ heroku stack
aspen-mri-1.8.6
bamboo-mri-1.9.2
* bamboo-ree-1.8.7
cedar (beta)
我们运行 10 个网络测功机,并拥有一个 Ronin 数据库。就插件而言,我们有:
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
在 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
我的处理程序看起来像:
class OrderEmailJob < Struct.new(:order_id, :email_type)
def perform
OrderMailer.send(email_type, Order.find(order_id)).deliver
end
end
并且排队像:
Delayed::Job.enqueue OrderEmailJob.new(self.id, :order_confirmation)
我尝试了很多不同版本的 DJ,但这是我唯一可以在 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Heroku Schedulers,则不需要因此而需要更多工作人员。 Heroku 将独立对其 cron 小时进行计费。
您确定延迟的作业执行代码没有引发异常吗?如果是,作业将根据重试变量(Delayed::Worker.max_attempts)再次排队,因此可能会导致重新安排。
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.