Sha256: 77628259a4aa466cce8d9fa8a968dc3ef40714058e7530ee444eb60664412247

Contents?: true

Size: 1.08 KB

Versions: 7

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  module OnConflict
    # Strategy to replace the job on conflict
    #
    # @author Mikael Henriksson <mikael@zoolutions.se>
    class Replace < OnConflict::Strategy
      attr_reader :queue, :unique_digest

      # @param [Hash] item sidekiq job hash
      def initialize(item)
        super
        @queue         = item[QUEUE_KEY]
        @unique_digest = item[UNIQUE_DIGEST_KEY]
      end

      # Replace the old job in the queue
      # @yield to retry the lock after deleting the old one
      def call(&block)
        return unless delete_job_by_digest

        delete_lock
        block&.call
      end

      # Delete the job from either schedule, retry or the queue
      def delete_job_by_digest
        Scripts.call(
          :delete_job_by_digest,
          nil,
          keys: ["#{QUEUE_KEY}:#{queue}", SCHEDULE_SET, RETRY_SET], argv: [unique_digest],
        )
      end

      # Delete the keys belonging to the job
      def delete_lock
        SidekiqUniqueJobs::Digests.delete_by_digest(unique_digest)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.25 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.24 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.23 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.22 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.21 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.20 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-6.0.19 lib/sidekiq_unique_jobs/on_conflict/replace.rb