Sha256: 4fee6b455f079355faa3a2dde652445cedb3b0e8347a4aa6d8dbb375663e7e79

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  class Lock
    # Locks jobs while the job is executing in the server process
    # - Locks before yielding to the worker's perform method
    # - Unlocks after yielding to the worker's perform method
    #
    # See {#lock} for more information about the client.
    # See {#execute} for more information about the server
    #
    # @author Mikael Henriksson <mikael@zoolutions.se>
    class WhileExecuting < BaseLock
      RUN_SUFFIX ||= ":RUN"

      # @param [Hash] item the Sidekiq job hash
      # @param [Proc] callback callback to call after unlock
      # @param [Sidekiq::RedisConnection, ConnectionPool] redis_pool the redis connection
      def initialize(item, callback, redis_pool = nil)
        super(item, callback, redis_pool)
        append_unique_key_suffix
      end

      # Simulate that a client lock was achieved.
      #   These locks should only ever be created in the server process.
      # @return [true] always returns true
      def lock
        true
      end

      # Executes in the Sidekiq server process.
      #   These jobs are locked in the server process not from the client
      # @yield to the worker class perform method
      def execute
        return strategy.call unless locksmith.lock(item[LOCK_TIMEOUT_KEY])

        with_cleanup { yield }
      end

      private

      # This is safe as the base_lock always creates a new digest
      #   The append there for needs to be done every time
      def append_unique_key_suffix
        item[UNIQUE_DIGEST_KEY] = item[UNIQUE_DIGEST_KEY] + RUN_SUFFIX
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.12 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-6.0.11 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-6.0.10 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-6.0.9 lib/sidekiq_unique_jobs/lock/while_executing.rb