Sha256: 2d2f74cc548f6b0f60491f49924a54a6d7bed9e9fbf6d1b1dbe34b7a876eea8e

Contents?: true

Size: 858 Bytes

Versions: 1

Compression:

Stored size: 858 Bytes

Contents

require "delayed_job_active_record"

module Delayed
  module UniqueJob
    def enqueue_once(*args)
      job = args.first
      raise "Job `#{job.class}` must respond to `#unique_key`" unless job.respond_to?(: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, key: job.unique_key)
        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.0 lib/delayed_job_unique/unique_job.rb