Sha256: 5b8078b95e8bdddece8f7113f7a0397ed96f995860a531c6295f42a32fb55c09

Contents?: true

Size: 1.06 KB

Versions: 4

Compression:

Stored size: 1.06 KB

Contents

require 'resque'

module ActiveJob
  module Cancel
    module QueueAdapters
      class ResqueAdapter
        def cancel(job_id, queue_name)
          queue_name = queue_name.call if queue_name.is_a?(Proc)
          job = find_job(job_id, queue_name)

          if job
            Resque.redis.lrem(redis_key_for_queue(queue_name), 0, Resque.encode(job))
            return true
          else
            if defined?(Resque.remove_delayed_selection)
              removed_count = Resque.remove_delayed_selection { |target| target[0]["job_id"] == job_id }
              return true if removed_count > 0
            end
          end

          false
        end

        private
          def find_job(job_id, queue_name)
            jobs = Resque.list_range(redis_key_for_queue(queue_name), 0, Resque.size(queue_name))
            jobs = [jobs] if jobs.is_a?(Hash)

            jobs.find do |job|
              job["args"][0]["job_id"] == job_id
            end
          end

          def redis_key_for_queue(queue)
            "queue:#{queue}"
          end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activejob-cancel-0.3.2 lib/active_job/cancel/queue_adapters/resque_adapter.rb
activejob-cancel-0.3.1 lib/active_job/cancel/queue_adapters/resque_adapter.rb
activejob-cancel-0.3.0 lib/active_job/cancel/queue_adapters/resque_adapter.rb
activejob-cancel-0.2.0 lib/active_job/cancel/queue_adapters/resque_adapter.rb