Sha256: 6fb2f8e0ee2e7ec487d6f7ed3dc6746965888cd04966fb1408d2b82b9f77651b

Contents?: true

Size: 871 Bytes

Versions: 1

Compression:

Stored size: 871 Bytes

Contents

require 'singleton'

module QueueToTheFuture  
  class Coordinator
    include Singleton
    
    def initialize
      @job_queue  = []
      @workforce  = []
      @lock       = Mutex.new
    end
    
    def next_job
      synchronize { @job_queue.shift }
    end
    
    def job_count
      synchronize { @job_queue.size }
    end
    
    def workforce_size
      synchronize { @workforce.size }
    end
    
    def relieve(worker)
      synchronize { @workforce -= [worker] }
    end
    
    def schedule(job)
      synchronize do
        @job_queue.push(job)
        
        if @workforce.size < QueueToTheFuture.maximum_workers && @workforce.size < @job_queue.size
          @workforce.push Worker.new(@workforce.size)
        end
      end
      
      job
    end
    
    private
    def synchronize(&block)
      @lock.synchronize(&block)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
queue_to_the_future-0.1.0 lib/queue_to_the_future/coordinator.rb