Sha256: 4fad20c5caaf5f7e4ec6020a8f9e5bf21987161559fce4f00eae3e1c35b2ba4f

Contents?: true

Size: 904 Bytes

Versions: 1

Compression:

Stored size: 904 Bytes

Contents

require "delayed_job_active_record"

module Delayed
  module UniqueJob
    def enqueue_once(*args, **kwargs)
      job = args.first
      raise "Job `#{job.class}` must respond to `#unique_key`" unless job.respond_to?(:unique_key)
      props = kwargs.merge({key: job.unique_key})
      Delayed::Job.transaction do
        if job_already_enqueued?(job)
          Delayed::Worker.logger.error "Job with key: #{job.unique_key} it's already on queue"
          false
        else
          Delayed::Job.enqueue(job, props)
        end
      end
    end

    private

    def job_already_enqueued?(job)
      # failed_at not nil means DJ doesnt retry the job again
      # https://github.com/collectiveidea/delayed_job_active_record/blob/c7e7ceb10b7a50cf4de4e485fdef4581ad2c37e1/lib/delayed/backend/active_record.rb#L57
      Delayed::Job.where(failed_at: nil, key: job.unique_key).exists?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
delayed_job_unique-0.1.1 lib/delayed_job_unique/unique_job.rb