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 "queue_classic"
|
2014-05-19 17:01:56 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-10-06 11:58:45 -04:00
|
|
|
# == queue_classic adapter for Active Job
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
|
|
|
# queue_classic provides a simple interface to a PostgreSQL-backed message
|
|
|
|
# queue. queue_classic specializes in concurrent locking and minimizing
|
|
|
|
# database load while providing a simple, intuitive developer experience.
|
|
|
|
# queue_classic assumes that you are already using PostgreSQL in your
|
|
|
|
# production environment and that adding another dependency (e.g. redis,
|
|
|
|
# beanstalkd, 0mq) is undesirable.
|
|
|
|
#
|
2014-10-06 11:58:45 -04:00
|
|
|
# Read more about queue_classic {here}[https://github.com/QueueClassic/queue_classic].
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
2014-10-06 11:58:45 -04:00
|
|
|
# To use queue_classic set the queue_adapter config to +:queue_classic+.
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :queue_classic
|
2014-05-19 17:01:56 -04:00
|
|
|
class QueueClassicAdapter
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2015-08-13 03:56:06 -04:00
|
|
|
qc_job = build_queue(job.queue_name).enqueue("#{JobWrapper.name}.perform", job.serialize)
|
|
|
|
job.provider_job_id = qc_job["id"] if qc_job.is_a?(Hash)
|
|
|
|
qc_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:
|
|
|
|
queue = build_queue(job.queue_name)
|
|
|
|
unless queue.respond_to?(:enqueue_at)
|
2016-08-06 12:40:07 -04:00
|
|
|
raise NotImplementedError, "To be able to schedule jobs with queue_classic " \
|
|
|
|
"the QC::Queue needs to respond to `enqueue_at(timestamp, method, *args)`. " \
|
|
|
|
"You can implement this yourself or you can use the queue_classic-later gem."
|
2014-08-24 08:29:20 -04:00
|
|
|
end
|
2015-08-13 03:56:06 -04:00
|
|
|
qc_job = queue.enqueue_at(timestamp, "#{JobWrapper.name}.perform", job.serialize)
|
|
|
|
job.provider_job_id = qc_job["id"] if qc_job.is_a?(Hash)
|
|
|
|
qc_job
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2014-08-24 08:29:20 -04:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
# Builds a <tt>QC::Queue</tt> object to schedule jobs on.
|
|
|
|
#
|
|
|
|
# If you have a custom <tt>QC::Queue</tt> subclass you'll need to subclass
|
|
|
|
# <tt>ActiveJob::QueueAdapters::QueueClassicAdapter</tt> and override the
|
|
|
|
# <tt>build_queue</tt> method.
|
|
|
|
def build_queue(queue_name)
|
|
|
|
QC::Queue.new(queue_name)
|
2014-05-19 17:01:56 -04:00
|
|
|
end
|
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
class JobWrapper #:nodoc:
|
2014-08-19 06:29:39 -04:00
|
|
|
class << self
|
2014-08-25 10:34:50 -04:00
|
|
|
def perform(job_data)
|
|
|
|
Base.execute job_data
|
2014-08-19 06:29:39 -04:00
|
|
|
end
|
2014-05-19 17:01:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|