2014-05-19 18:56:08 -04:00
|
|
|
require 'sneakers'
|
2014-05-20 05:32:29 -04:00
|
|
|
require 'thread'
|
2014-05-19 18:56:08 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-09-21 16:20:23 -04:00
|
|
|
# == Sneakers adapter for Active Job
|
|
|
|
#
|
|
|
|
# A high-performance RabbitMQ background processing framework for Ruby.
|
|
|
|
# Sneakers is being used in production for both I/O and CPU intensive
|
|
|
|
# workloads, and have achieved the goals of high-performance and
|
|
|
|
# 0-maintenance, as designed.
|
|
|
|
#
|
|
|
|
# Read more about Sneakers {here}[https://github.com/jondot/sneakers].
|
|
|
|
#
|
|
|
|
# To use Sneakers set the queue_adapter config to +:sneakers+.
|
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :sneakers
|
2014-05-19 18:56:08 -04:00
|
|
|
class SneakersAdapter
|
2015-03-11 17:57:13 -04:00
|
|
|
def initialize
|
|
|
|
@monitor = Monitor.new
|
|
|
|
end
|
2014-05-22 16:38:01 -04:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
|
|
|
@monitor.synchronize do
|
|
|
|
JobWrapper.from_queue job.queue_name
|
|
|
|
JobWrapper.enqueue ActiveSupport::JSON.encode(job.serialize)
|
2014-05-19 18:56:08 -04:00
|
|
|
end
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2014-05-20 11:44:00 -04:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
|
|
|
raise NotImplementedError
|
2014-05-19 18:56:08 -04:00
|
|
|
end
|
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
class JobWrapper #:nodoc:
|
2014-05-19 18:56:08 -04:00
|
|
|
include Sneakers::Worker
|
2014-08-18 03:19:41 -04:00
|
|
|
from_queue 'default'
|
2014-05-19 18:56:08 -04:00
|
|
|
|
2014-08-06 17:09:28 -04:00
|
|
|
def work(msg)
|
2014-08-25 10:34:50 -04:00
|
|
|
job_data = ActiveSupport::JSON.decode(msg)
|
|
|
|
Base.execute job_data
|
2014-08-06 17:09:28 -04:00
|
|
|
ack!
|
2014-05-19 18:56:08 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|