Sha256: c336d58454813f8bde91ca8b1e6542794f3d89f327e0afffb3d8ebd314f502eb

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 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@mhenrixon.com>
    class WhileExecuting < BaseLock
      RUN_SUFFIX = ":RUN"

      include SidekiqUniqueJobs::OptionsWithFallback
      include SidekiqUniqueJobs::Logging::Middleware

      # @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
        job_id = item[JID]
        yield if block_given?

        job_id
      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(&block)
        with_logging_context do
          executed = locksmith.execute do
            yield
            callback_safely if locksmith.unlock
          ensure
            locksmith.unlock
          end

          call_strategy(origin: :server, &block) unless executed
        end
      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
        return if (lock_digest = item[LOCK_DIGEST]).end_with?(RUN_SUFFIX)

        item[LOCK_DIGEST] = lock_digest + RUN_SUFFIX
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-7.1.16 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.15 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.14 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.13 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.12 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.11 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.10 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.8 lib/sidekiq_unique_jobs/lock/while_executing.rb
sidekiq-unique-jobs-7.1.7 lib/sidekiq_unique_jobs/lock/while_executing.rb