1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/lib/active_job/queue_adapters/delayed_job_adapter.rb

48 lines
1.5 KiB
Ruby
Raw Normal View History

2017-07-09 13:49:52 -04:00
# frozen_string_literal: true
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
# 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].
#
# 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
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)
job.provider_job_id = delayed_job.id
delayed_job
end
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))
job.provider_job_id = delayed_job.id
delayed_job
2014-05-19 06:34:27 -04:00
end
class JobWrapper #:nodoc:
attr_accessor :job_data
def initialize(job_data)
@job_data = job_data
end
def display_name
"#{job_data['job_class']} [#{job_data['job_id']}] from DelayedJob(#{job_data['queue_name']}) with arguments: #{job_data['arguments']}"
end
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