Sha256: f0f51c7b1ba3da85eaa5d3201cdc9027ba95edd049e97e4fc7fc236ea384e5c3

Contents?: true

Size: 1.86 KB

Versions: 12

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  # Module containing methods shared between client and server middleware
  #
  # Requires the following methods to be defined in the including class
  #   1. item (required)
  #   2. options (can be nil)
  #   3. job_class (required, can be anything)
  # @author Mikael Henriksson <mikael@mhenrixon.com>
  module OptionsWithFallback
    def self.included(base)
      base.send(:include, SidekiqUniqueJobs::SidekiqWorkerMethods)
    end

    # A convenience method for using the configured locks
    def locks
      SidekiqUniqueJobs.locks
    end

    # Check if unique has been enabled
    # @return [true, false] indicate if the gem has been enabled
    def unique_enabled?
      SidekiqUniqueJobs.enabled? && lock_type
    end

    # Check if unique has been disabled
    def unique_disabled?
      !unique_enabled?
    end

    #
    # A new lock for this Sidekiq Job
    #
    #
    # @return [Lock::BaseLock] an instance of a lock implementation
    #
    def lock_instance
      @lock_instance ||= lock_class.new(item, after_unlock_hook, @redis_pool)
    end

    #
    # Returns the corresponding class for the lock_type
    #
    #
    # @return [Class]
    #
    def lock_class
      @lock_class ||= locks.fetch(lock_type.to_sym) do
        raise UnknownLock, "No implementation for `lock: :#{lock_type}`"
      end
    end

    #
    # The type of lock for this worker
    #
    #
    # @return [Symbol, NilClass]
    #
    def lock_type
      @lock_type ||= options[LOCK] || item[LOCK]
    end

    #
    # The default options with any matching keys overridden from worker options
    #
    #
    # @return [Hash<String, Object>]
    #
    def options
      @options ||= begin
        opts = default_job_options.dup
        opts.merge!(job_options) if sidekiq_job_class?
        (opts || {}).stringify_keys
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-8.0.10 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-7.1.33 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-7.1.32 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.9 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.8 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.7 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.6 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-7.1.31 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.5 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.4 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-7.1.30 lib/sidekiq_unique_jobs/options_with_fallback.rb
sidekiq-unique-jobs-8.0.3 lib/sidekiq_unique_jobs/options_with_fallback.rb