Sha256: 75e1f9474162bde48e8179a87f9abfdfd64a0fd961b00743481bc42b23a8f0da

Contents?: true

Size: 949 Bytes

Versions: 5

Compression:

Stored size: 949 Bytes

Contents

# Encoding: utf-8

module Reqless
  module JobReservers
    # Round-robins through all the provided queues
    class RoundRobin
      attr_reader :queues

      def initialize(queues)
        @queues = queues
        @num_queues = queues.size
        @last_popped_queue_index = @num_queues - 1
      end

      def reserve
        @num_queues.times do |i|
          job = next_queue.pop
          return job if job
        end
        nil
      end

      def prep_for_work!
        # nothing here on purpose
      end

      def description
        @description ||=
          @queues.map(&:name).join(', ') + " (#{self.class::TYPE_DESCRIPTION})"
      end

      def reset_description!
        @description = nil
      end

    private

      TYPE_DESCRIPTION = 'round robin'

      def next_queue
        @last_popped_queue_index = (@last_popped_queue_index + 1) % @num_queues
        @queues[@last_popped_queue_index]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
reqless-0.0.5 lib/reqless/job_reservers/round_robin.rb
reqless-0.0.4 lib/reqless/job_reservers/round_robin.rb
reqless-0.0.3 lib/reqless/job_reservers/round_robin.rb
reqless-0.0.2 lib/reqless/job_reservers/round_robin.rb
reqless-0.0.1 lib/reqless/job_reservers/round_robin.rb