Sha256: ff8fc0c038932826a8f44cb50fd7e692bafe19489dd4e30cdf909b4ff84abd0d

Contents?: true

Size: 946 Bytes

Versions: 4

Compression:

Stored size: 946 Bytes

Contents

require 'delayed_job'
require 'delayed_job_active_record'

module ActiveJob
  module Cancel
    module QueueAdapters
      class DelayedJobAdapter
        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
            job.destroy
            return true
          end
          false
        end

        def cancel_by(opts, _queue_name)
          raise ArgumentError, 'Please specify ":provider_job_id"' unless opts[:provider_job_id]
          job_id = opts[:provider_job_id]

          job = Delayed::Job.find_by(id: job_id)
          if job
            job.destroy
            return true
          end
          false
        end

        private
          def find_job(job_id, queue_name)
            Delayed::Job.where('handler LIKE ?', "%job_id: #{job_id}%").where(queue: queue_name).first
          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/delayed_job_adapter.rb
activejob-cancel-0.3.1 lib/active_job/cancel/queue_adapters/delayed_job_adapter.rb
activejob-cancel-0.3.0 lib/active_job/cancel/queue_adapters/delayed_job_adapter.rb
activejob-cancel-0.2.0 lib/active_job/cancel/queue_adapters/delayed_job_adapter.rb