aboutsummaryrefslogtreecommitdiffstats
path: root/activejob/lib/active_job/queue_adapters/resque_adapter.rb
blob: da8212fc9b1bf75239932f808b99a50c0b321d2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require 'resque'
require 'active_support/core_ext/enumerable'
require 'active_support/core_ext/array/access'

begin
  require 'resque-scheduler'
rescue LoadError
  begin
    require 'resque_scheduler'
  rescue LoadError
    false
  end
end

module ActiveJob
  module QueueAdapters
    class ResqueAdapter
      class << self
        def enqueue(job, *args)
          Resque.enqueue_to job.queue_name, JobWrapper, job.name, *args
        end

        def enqueue_at(job, timestamp, *args)
          unless Resque.respond_to?(:enqueue_at_with_queue)
            raise NotImplementedError, "To be able to schedule jobs with Resque you need the " \
              "resque-scheduler gem. Please add it to your Gemfile and run bundle install"
          end
          Resque.enqueue_at_with_queue job.queue_name, timestamp, JobWrapper, job.name, *args
        end
      end

      class JobWrapper
        class << self
          def perform(job_name, *args)
            job_name.constantize.new.execute(*args)
          end
        end
      end
    end
  end
end