Question: How to extend ActiveJob to access delayed_job's failure/error hooks?

Question

How to extend ActiveJob to access delayed_job's failure/error hooks?

Answers 0
Added at 2016-12-20 05:12
Tags
Question

I'm using rails ActiveJob's delayed_job adaptor, but would like to still be able to access delayed_job's error and failure hooks to trigger notification mailers when a job errors or fails.

I think I've tracked down the piece of ActiveJob code I need to extend in order to incorporate delayed_job's error and failure methods.

How do I go about extending the JobWrapper class below to include delayed_job's error and failure methods, so I can then use custom error/failure code on a job-by-job basis?

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
Answers
Source Show
◀ Wstecz