sneakers_adapter.rb 1.2 KB
Newer Older
J
John DeSilva 已提交
1
require 'sneakers'
2
require 'thread'
J
John DeSilva 已提交
3 4 5

module ActiveJob
  module QueueAdapters
6 7 8 9 10 11 12 13 14 15 16 17
    # == 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
J
John DeSilva 已提交
18
    class SneakersAdapter
C
Cristian Bica 已提交
19 20
      @monitor = Monitor.new

J
John DeSilva 已提交
21
      class << self
22
        def enqueue(job) #:nodoc:
C
Cristian Bica 已提交
23
          @monitor.synchronize do
24
            JobWrapper.from_queue job.queue_name
C
Cristian Bica 已提交
25
            JobWrapper.enqueue ActiveSupport::JSON.encode(job.serialize)
26
          end
J
John DeSilva 已提交
27
        end
28

29
        def enqueue_at(job, timestamp) #:nodoc:
30 31
          raise NotImplementedError
        end
J
John DeSilva 已提交
32 33
      end

A
Akshay Vishnoi 已提交
34
      class JobWrapper #:nodoc:
J
John DeSilva 已提交
35
        include Sneakers::Worker
C
Cristian Bica 已提交
36
        from_queue 'default'
J
John DeSilva 已提交
37

38
        def work(msg)
C
Cristian Bica 已提交
39 40
          job_data = ActiveSupport::JSON.decode(msg)
          Base.execute job_data
41
          ack!
J
John DeSilva 已提交
42 43 44 45 46
        end
      end
    end
  end
end