我正在使用rails ActiveJob的delayed_job适配器,但仍希望能够访问delayed_job的error and failure hooks以在作业发生错误或失败时触发通知邮件程序。
我想我已经跟踪了我需要扩展的ActiveJob code部分,以便合并delayed_job的错误和失败方法。
如何扩展下面的JobWrapper类以包含delayed_job的错误和失败方法,以便我可以逐个使用自定义错误/失败代码?
require "delayed_job"
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
def enqueue(job)
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority)
job.provider_job_id = delayed_job.id
delayed_job
end
def enqueue_at(job, timestamp)
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority, run_at: Time.at(timestamp))
job.provider_job_id = delayed_job.id
delayed_job
end
class JobWrapper
attr_accessor :job_data
def initialize(job_data)
@job_data = job_data
end
def perform
Base.execute(job_data)
end
end
end
end
end
答案 0 :(得分:1)
我相信这就是你想要的
module ActiveJob
module QueueAdapters
class DelayedJobAdapter
class JobWrapper
def failure(job)
job_data = Base.deserialize(job.payload_object.job_data)
job_data.failure(job) if job_data.respond_to?(:failure)
end
end
end
end
end
我已将此问题/补丁发布到rails问题 https://github.com/rails/rails/issues/30205
答案 1 :(得分:0)
ActiveJob实现ActiveSupport::Rescuable,这意味着您可以使用rescue_from。
class MyJob < ActiveJob::Base
rescue_from Exception do |e|
# do something with e
end
# rest of your job
end