2014-05-19 17:01:56 -04:00
|
|
|
require 'queue_classic'
|
|
|
|
|
|
|
|
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
|
|
|
|
class << self
|
2014-09-21 16:20:23 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2014-08-25 10:34:50 -04:00
|
|
|
build_queue(job.queue_name).enqueue("#{JobWrapper.name}.perform", job.serialize)
|
2014-05-19 17:01:56 -04:00
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2014-09-21 16:20:23 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
2014-08-24 08:29:20 -04:00
|
|
|
queue = build_queue(job.queue_name)
|
|
|
|
unless queue.respond_to?(:enqueue_at)
|
2014-10-06 11:58:45 -04:00
|
|
|
raise NotImplementedError, 'To be able to schedule jobs with queue_classic ' \
|
2014-10-22 09:56:46 -04:00
|
|
|
'the QC::Queue needs to respond to `enqueue_at(timestamp, method, *args)`. ' \
|
2014-08-24 08:29:20 -04:00
|
|
|
'You can implement this yourself or you can use the queue_classic-later gem.'
|
|
|
|
end
|
2014-08-25 10:34:50 -04:00
|
|
|
queue.enqueue_at(timestamp, "#{JobWrapper.name}.perform", job.serialize)
|
2014-08-24 08:29:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a <tt>QC::Queue</tt> object to schedule jobs on.
|
|
|
|
#
|
2014-09-27 07:21:29 -04:00
|
|
|
# If you have a custom <tt>QC::Queue</tt> subclass you'll need to subclass
|
2014-08-24 08:29:20 -04:00
|
|
|
# <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 18:27:28 -04:00
|
|
|
end
|
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
|