aboutsummaryrefslogblamecommitdiffstats
path: root/activejob/lib/active_job/queue_adapters/sneakers_adapter.rb
blob: 051a8c3bd7d9d01418c8511cc79e749680b728b6 (plain) (tree)
1
2
3
4
5
6
7
8
9
                  
                



                         

                            
                   
                               
                                 
                                                
                                                                              
             
           
 
                                             

                                   



                                
                                        
 



                                                           




           
require 'sneakers'
require 'thread'

module ActiveJob
  module QueueAdapters
    class SneakersAdapter
      @monitor = Monitor.new

      class << self
        def enqueue(job, *args)
          @monitor.synchronize do
            JobWrapper.from_queue job.queue_name
            JobWrapper.enqueue ActiveSupport::JSON.encode([ job.name, *args ])
          end
        end

        def enqueue_at(job, timestamp, *args)
          raise NotImplementedError
        end
      end

      class JobWrapper
        include Sneakers::Worker
        from_queue 'active_jobs_default'

        def work(msg)
          job_name, *args = ActiveSupport::JSON.decode(msg)
          job_name.constantize.new.execute *args
          ack!
        end
      end
    end
  end
end