2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
2016-08-06 12:40:07 -04:00
|
|
|
require "delayed_job"
|
2014-05-18 17:47:35 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-09-21 16:20:23 -04:00
|
|
|
# == Delayed Job adapter for Active Job
|
|
|
|
#
|
|
|
|
# Delayed::Job (or DJ) encapsulates the common pattern of asynchronously
|
|
|
|
# executing longer tasks in the background. Although DJ can have many
|
2014-10-31 20:43:17 -04:00
|
|
|
# storage backends, one of the most used is based on Active Record.
|
2014-09-21 16:20:23 -04:00
|
|
|
# Read more about Delayed Job {here}[https://github.com/collectiveidea/delayed_job].
|
|
|
|
#
|
2014-10-31 20:43:17 -04:00
|
|
|
# To use Delayed Job, set the queue_adapter config to +:delayed_job+.
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :delayed_job
|
2014-05-18 17:47:35 -04:00
|
|
|
class DelayedJobAdapter
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2015-03-18 05:48:26 -04:00
|
|
|
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority)
|
2015-04-26 13:05:08 -04:00
|
|
|
job.provider_job_id = delayed_job.id
|
|
|
|
delayed_job
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
2015-03-18 05:48:26 -04:00
|
|
|
delayed_job = Delayed::Job.enqueue(JobWrapper.new(job.serialize), queue: job.queue_name, priority: job.priority, run_at: Time.at(timestamp))
|
2015-04-26 13:05:08 -04:00
|
|
|
job.provider_job_id = delayed_job.id
|
|
|
|
delayed_job
|
2014-05-19 06:34:27 -04:00
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
class JobWrapper #:nodoc:
|
2014-09-18 15:52:30 -04:00
|
|
|
attr_accessor :job_data
|
|
|
|
|
|
|
|
def initialize(job_data)
|
|
|
|
@job_data = job_data
|
|
|
|
end
|
|
|
|
|
2017-10-13 08:00:20 -04:00
|
|
|
def display_name
|
|
|
|
"#{job_data['job_class']} [#{job_data['job_id']}] from DelayedJob(#{job_data['queue_name']}) with arguments: #{job_data['arguments']}"
|
|
|
|
end
|
|
|
|
|
2014-09-18 15:52:30 -04:00
|
|
|
def perform
|
2014-08-25 10:34:50 -04:00
|
|
|
Base.execute(job_data)
|
2014-05-18 17:47:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|