【问题标题】:Can't find Delayed Job record in database在数据库中找不到延迟作业记录
【发布时间】:2016-04-27 01:27:20
【问题描述】:

我是 Delayed Jobs 的新手,我不确定自己是否正确。

工人

class BillingFailedEmailWorker < BaseWorker

  attr_accessor :car

  def initialize(car)
     @car = car 
  end

  def billing_failed_for_subscriptions
    StripeMailer::billing_failed_for_subscriptions(car.owner.email).deliver
  end

  def perform
    self.class.new(car).delay(
      run_at: DateTime.now + 1.week,
      queue: "Failed Subscription"
    ).billing_failed_for_subscriptions
  end  
end

邮件程序

class StripeMailer < ActionMailer::Base

  default from: Settings.default_from_email

  def billing_failed_for_subscriptions(email)
    mail to: email, subject: 'Autobrain Subscription Renewal Failed'
  end
end

billing_failed_for_subscriptions 显然也对应正确的邮件视图。

我的问题是,当我运行 BillingFailedEmailWorker.new(Car.last).perform 时,我看到命令在 Delayed::Backend::ActiveRecord::Job 中正确执行,但是当我运行 Delayed::Backend::ActiveRecord::Job.lastDelayed::Job.last 时,我没有看到我刚刚创建的作业。

这可能是我的 Rails 环境正在开发中的问题,还是我缺少什么?

【问题讨论】:

  • 看起来如果我在self.class.new(car).delay(...).billing_failed_for_subscriptions 的末尾添加一个.save 它将在数据库中保存一条记录。
  • 也许您禁用了 Delayed::Worker.delay_jobs = false 出于开发目的而在全球范围内延迟工作?在这种情况下,作业不会保存,而是立即调用。
  • 原来是这样。它在开发中设置为 false。谢谢

标签: ruby-on-rails activerecord delayed-job


【解决方案1】:

而不是使用

  • StripeMailer::billing_failed_for_subscriptions(car.owner.email).deliver

使用

  • StripeMailer::billing_failed_for_subscriptions(car.owner.email).deliver_later 然后在控制台中您可以将其检查为 Delayed::Job.last

【讨论】:

  • 我收到NoMethodError: undefined method 'deliver_later' 错误
  • 尝试使用 StripeMailer.billing_failed_for_subscription(car.owner.email).deliver_later 在你的 gemfile 中添加这个 gem 并安装它github.com/collectiveidea/delayed_job 安装后运行迁移,它会正常工作。
  • @Fakhir_Shad 看起来这需要 Rails 4.2 (github.com/collectiveidea/delayed_job#rails-42),我们目前仍在 4.0 中。顺便说一句,我有 delay_job gem。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 2017-06-30
  • 2014-11-04
  • 2016-03-29
  • 2019-02-09
  • 2016-03-29
  • 1970-01-01
相关资源
最近更新 更多