Sha256: c674d88f5acd95b980ba35aa25b49611dcc5be54fb65be97742a1c4f1a904aaa

Contents?: true

Size: 683 Bytes

Versions: 2

Compression:

Stored size: 683 Bytes

Contents

module Qless
  module JobReservers
    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|
          if job = next_queue.pop
            return job
          end
        end
        nil
      end

      def description
        @description ||= @queues.map(&:name).join(', ') + " (round robin)"
      end

    private

      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

2 entries across 2 versions & 1 rubygems

Version Path
qless-0.9.2 lib/qless/job_reservers/round_robin.rb
qless-0.9.1 lib/qless/job_reservers/round_robin.rb