Sha256: 495d8e9ceeb09571b5065516f14290845bfbc9fa5140e2fda414152a8bdb5d4c

Contents?: true

Size: 1.38 KB

Versions: 6

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
class Sidekiq::Portal::JobRegistry
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def initialize
    @jobs = {}
    @lock = Sidekiq::Portal::Lock.new
  end

  # @param job [Sidekiq::Portal::Job]
  # @return [void]
  #
  # @api private
  # @since 0.1.0
  def register(job)
    thread_safe { jobs[job.klass] = job }
  end

  # @param job_klass [Class]
  # @return [Sidekiq::Portal::Job]
  #
  # @api private
  # @since 0.1.0
  def resolve(job_klass)
    thread_safe do
      jobs.fetch(job_klass) do
        raise(
          Sidekiq::Portal::NonScheduledJobClassError,
          "Trying to work with non-scheduled job class \"#{job_klass}\""
        )
      end
    end
  end

  # @param job_klass [Class]
  # @return [Boolean]
  #
  # @api private
  # @since 0.1.0
  def include?(job_klass)
    thread_safe { jobs.key?(job_klass) }
  end

  # @param block [Block]
  # @yield [job]
  # @yieldparam job [Sidekiq::Portal::Job]
  # @return [Enumerable]
  #
  # @api private
  # @since 0.1.0
  def each(&block)
    thread_safe do
      block_given? ? jobs.each_value(&block) : jobs.each_value
    end
  end

  private

  # @return [Hash]
  #
  # @api private
  # @since 0.1.0
  attr_reader :jobs

  # @param block [Block]
  # @return [Any]
  #
  # @api private
  # @since 0.1.0
  def thread_safe(&block)
    @lock.thread_safe(&block)
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sidekiq_portal-0.3.2 lib/portal/job_registry.rb
sidekiq_portal-0.3.1 lib/portal/job_registry.rb
sidekiq_portal-0.3.0 lib/portal/job_registry.rb
sidekiq_portal-0.2.0 lib/portal/job_registry.rb
sidekiq_portal-0.1.1 lib/portal/job_registry.rb
sidekiq_portal-0.1.0 lib/portal/job_registry.rb