我的问题很简单
使用自定义类,我可以做到这一点:
class Job
def message=(message)
@message = message
end
def perform
puts @message
end
end
job = Job.new
job.message = 'hello'
Delayed::Job.enqueue job
Delayed::Job.last.invoke_job
# hello
# => nil
使用ActiveJob::Base
类时,我不能:
job = MyJob.new
class MyJob < ApplicationJob
def message= (message)
@message = message
end
def perform(*args)
puts @message
end
end
job = MyJob.new
job.message = 'hello'
job.enqueue
Delayed::Job.last.invoke_job
# Performed MyJob (Job ID: a8cadf34-f7f7-406d-abcf-cd96589acdf2) from DelayedJob(default) in 3.58ms
# => nil
原因是JobWrapper
(不包含自定义属性)被插入表中而不是作业实例本身:
Delayed::Backend::ActiveRecord::Job Create (0.5ms) INSERT INTO "delayed_jobs" ("handler", "run_at", "queue", "created_at", "updated_at") VALUES ('--- !ruby/object:ActiveJob::QueueAdapters::DelayedJobAdapter::JobWrapper
job_data:
job_class: MyJob
job_id: a8cadf34-f7f7-406d-abcf-cd96589acdf2
provider_job_id:
queue_name: default
priority:
arguments: []
executions: 0
locale: en
', '2018-10-23 16:34:58.363428', 'default', '2018-10-23 16:34:58.363570', '2018-10-23 16:34:58.363570') RETURNING "id"
我知道perform_later
语法和其他相关详细信息,请不要只是告诉我“您使用错了” 。
这是预期的行为还是被禁止?如果没有,有什么解决方法吗?