2014-05-18 15:02:02 -04:00
|
|
|
require 'sidekiq'
|
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-09-21 16:20:23 -04:00
|
|
|
# == Sidekiq adapter for Active Job
|
|
|
|
#
|
|
|
|
# Simple, efficient background processing for Ruby. Sidekiq uses threads to
|
|
|
|
# handle many jobs at the same time in the same process. It does not
|
2014-09-27 10:22:15 -04:00
|
|
|
# require Rails but will integrate tightly with it to make background
|
|
|
|
# processing dead simple.
|
2014-09-21 16:20:23 -04:00
|
|
|
#
|
|
|
|
# Read more about Sidekiq {here}[http://sidekiq.org].
|
|
|
|
#
|
|
|
|
# To use Sidekiq set the queue_adapter config to +:sidekiq+.
|
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :sidekiq
|
2014-05-18 15:02:02 -04:00
|
|
|
class SidekiqAdapter
|
|
|
|
class << self
|
2014-09-21 16:20:23 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2014-05-29 12:29:33 -04:00
|
|
|
#Sidekiq::Client does not support symbols as keys
|
2014-05-20 11:37:10 -04:00
|
|
|
Sidekiq::Client.push \
|
|
|
|
'class' => JobWrapper,
|
|
|
|
'queue' => job.queue_name,
|
2014-08-25 10:34:50 -04:00
|
|
|
'args' => [ job.serialize ],
|
2014-05-20 11:37:10 -04:00
|
|
|
'retry' => true
|
2014-05-19 06:34:27 -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-05-20 12:13:28 -04:00
|
|
|
Sidekiq::Client.push \
|
|
|
|
'class' => JobWrapper,
|
|
|
|
'queue' => job.queue_name,
|
2014-08-25 10:34:50 -04:00
|
|
|
'args' => [ job.serialize ],
|
2014-05-20 13:17:10 -04:00
|
|
|
'retry' => true,
|
|
|
|
'at' => timestamp
|
2014-05-19 18:27:28 -04:00
|
|
|
end
|
2014-05-19 06:34:27 -04:00
|
|
|
end
|
|
|
|
|
2014-09-17 15:46:53 -04:00
|
|
|
class JobWrapper #:nodoc:
|
2014-05-19 06:34:27 -04:00
|
|
|
include Sidekiq::Worker
|
|
|
|
|
2014-08-25 10:34:50 -04:00
|
|
|
def perform(job_data)
|
|
|
|
Base.execute job_data
|
2014-05-18 15:02:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|