Sha256: 4c76dc36d40d501aa14c6478276eea194b5cbaeb369163232ca70756fa67c741

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require 'active_support'
require 'active_job'
require 'active_job/cancel/queue_adapters'
require 'active_job/cancel/queue_adapters/test_adapter'
require 'active_job/cancel/version'

module ActiveJob
  module Cancel
    extend ActiveSupport::Concern

    SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque Test).freeze

    def cancel
      if self.class.can_cancel?
        self.class.cancel_adapter_class.new.cancel(job_id, queue_name)
      else
        raise NotImplementedError, 'This queueing backend does not support cancel.'
      end
    end

    module ClassMethods
      def cancel(job_id)
        if can_cancel?
          cancel_adapter_class.new.cancel(job_id, self.queue_name)
        else
          raise NotImplementedError, 'This queueing backend does not support cancel.'
        end
      end

      def cancel_by(opts)
        if can_cancel?
          cancel_adapter_class.new.cancel_by(opts, self.queue_name)
        else
          raise NotImplementedError, 'This queueing backend does not support cancel_by.'
        end
      end

      def can_cancel?
        SUPPORTED_ADAPTERS.include?(adapter_name)
      end

      def cancel_adapter_class
        Object.const_get("ActiveJob::Cancel::QueueAdapters::#{adapter_name}Adapter")
      end

      private
        def adapter_name
          if ActiveJob.version > Gem::Version.new('4.3')
            self.queue_adapter.class.name.demodulize.chomp('Adapter')
          else
            self.queue_adapter.name.demodulize.chomp('Adapter')
          end
        end
    end
  end
end

ActiveSupport.on_load(:active_job) do
  include(ActiveJob::Cancel)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activejob-cancel-0.3.0 lib/active_job/cancel.rb