Sha256: 022b5311153a27ab0ce4ca2e0b19fdaf9f2233b02cc725be4be654c616da8413

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 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
      #
      # @!attribute [r] queue
      #   @return [String] rthe sidekiq queue this job belongs to
      attr_reader :queue
      #
      # @!attribute [r] unique_digest
      #   @return [String] the unique digest to use for locking
      attr_reader :unique_digest

      #
      # Initialize a new Replace strategy
      #
      # @param [Hash] item sidekiq job hash
      #
      def initialize(item, redis_pool = nil)
        super(item, redis_pool)
        @queue         = item[QUEUE]
        @unique_digest = item[UNIQUE_DIGEST]
      end

      #
      # Replace the old job in the queue
      #
      #
      # @return [void] <description>
      #
      # @yield to retry the lock after deleting the old one
      #
      def call(&block)
        return unless (deleted_job = delete_job_by_digest)

        log_info("Deleting job: #{deleted_job}")
        if (del_count = delete_lock)
          log_info("Deleted `#{del_count}` keys for #{unique_digest}")
        end
        block&.call
      end

      #
      # Delete the job from either schedule, retry or the queue
      #
      #
      # @return [String] the deleted job hash
      # @return [nil] when deleting nothing
      #
      def delete_job_by_digest
        call_script(:delete_job_by_digest,
                    keys: ["#{QUEUE}:#{queue}", SCHEDULE, RETRY],
                    argv: [unique_digest])
      end

      #
      # Delete the keys belonging to the job
      #
      #
      # @return [Integer] the number of keys deleted
      #
      def delete_lock
        call_script(:delete_by_digest, keys: [unique_digest, DIGESTS])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.0.0.beta4 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-7.0.0.beta3 lib/sidekiq_unique_jobs/on_conflict/replace.rb
sidekiq-unique-jobs-7.0.0.beta2 lib/sidekiq_unique_jobs/on_conflict/replace.rb