我正在使用Rails ActiveJob的delay_job适配器,但仍希望能够在工作出错或失败时访问delay_job的error and failure hooks来触发通知邮件。
我想我已经找到了我需要扩展的ActiveJob code片段,以便合并delay_job的错误和失败方法。
我该如何扩展下面的JobWrapper类以包括delay_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
最佳答案
我相信这就是你想要的
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
关于ruby-on-rails-4 - 如何扩展ActiveJob来访问delay_job的失败/错误 Hook ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41234752/