Sha256: ef59ae64f18d0ff2ec5e8d8a7013ba6764839c0e1dacb4abc9bcc05820a19f87

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module SidekiqUniqueJobs
  # Module with convenience methods for the Sidekiq::Worker class
  #
  # @author Mikael Henriksson <mikael@zoolutions.se>
  module SidekiqWorkerMethods
    # Avoids duplicating worker_class.respond_to? in multiple places
    # @return [true, false]
    def worker_method_defined?(method_sym)
      worker_class.respond_to?(method_sym)
    end

    # Wraps #get_sidekiq_options to always work with a hash
    # @return [Hash] of the worker class sidekiq options
    def worker_options
      return {} unless sidekiq_worker_class?

      worker_class.get_sidekiq_options.stringify_keys
    end

    # Tests that the
    # @return [true] if worker_class responds to get_sidekiq_options
    # @return [false] if worker_class does not respond to get_sidekiq_options
    def sidekiq_worker_class?
      worker_method_defined?(:get_sidekiq_options)
    end

    # The Sidekiq::Worker implementation
    # @return [Sidekiq::Worker]
    def worker_class
      @_worker_class ||= worker_class_constantize(@worker_class) # rubocop:disable Naming/MemoizedInstanceVariableName
    end

    # The hook to call after a successful unlock
    # @return [Proc]
    def after_unlock_hook
      -> { worker_class.after_unlock if worker_method_defined?(:after_unlock) }
    end

    # Attempt to constantize a string worker_class argument, always
    # failing back to the original argument when the constant can't be found
    #
    # @return [Sidekiq::Worker]
    def worker_class_constantize(klazz)
      return klazz.class if klazz.is_a?(Sidekiq::Worker) # sidekiq v6.x
      return klazz       unless klazz.is_a?(String)

      Object.const_get(klazz)
    rescue NameError => ex
      case ex.message
      when /uninitialized constant/
        klazz
      else
        raise
      end
    end

    def default_worker_options
      Sidekiq.default_worker_options
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
sidekiq-unique-jobs-6.0.23 lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb
sidekiq-unique-jobs-6.0.22 lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb
sidekiq-unique-jobs-6.0.21 lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb
sidekiq-unique-jobs-6.0.20 lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb
sidekiq-unique-jobs-6.0.19 lib/sidekiq_unique_jobs/sidekiq_worker_methods.rb