【问题标题】:Argument not passing on Delayed::Job method参数未传递 Delayed::Job 方法
【发布时间】:2023-03-09 03:47:01
【问题描述】:

我正在使用 Delayed::Job 从 Rails 应用程序发送短信。我在 Gemfile 中包含如下 gem。

gem 'delayed_job', "~> 4.1.1"

我还添加了一个自定义延迟作业作为

class SendSmsJob < Struct.new(:person_id, :content)

  include DelayedAirbrakeNotification


  def perform
    person = Person.find(person_id)
    phone = person.phone_number
    ....
    ....
    ....

  end

end

我正在从控制器触发这项工作,如下所示,

Delayed::Job.enqueue(SendSmsJob.new(@person.id, "Test message"))

但是当我尝试在perform 方法下使用person_idcontent 的值时,我分别得到nil""。 我对其他工作采用相同的方法,并且这些工作完美无缺, 我不知道我是否在这里遗漏了什么。

任何建议将不胜感激。

【问题讨论】:

  • 您是否尝试传递不带 id 的 person 对象?像这样Delayed::Job.enqueue(SendSmsJob.new(@person, "Test message")),然后直接调用@person.phone_number等
  • @AlexanderLuna 是的,我试过了,同样的问题。

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


【解决方案1】:

首先,您应该安装 DelayedJob 作为 ActiveJob 的后端,如 here 所述。 然后通过运行以下命令生成 ActiveJob 作业:

rails generate job NAME [options]

运行rails generate job --help 以获得帮助或查看documentation page

最后但同样重要的是,在perform 方法中,您应该定义您需要的任何参数。

def perform(person_id)
  ...
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    相关资源
    最近更新 更多