Sha256: b356329df5b463f2caaab5b17331562d9084fba7b0e24f9645c823c514b74a19

Contents?: true

Size: 1.42 KB

Versions: 70

Compression:

Stored size: 1.42 KB

Contents

module Redcar
  class TaskQueue
    MAX_COMPLETED_LENGTH = 20
    
    attr_reader :in_process, :mutex
    
    def initialize
      @executor = java.util.concurrent.Executors.newSingleThreadExecutor
      @mutex    = Mutex.new
      @pending   = []
      @completed = []
      @in_process = nil
    end
    
    def submit(task)
      @mutex.synchronize do
        @pending << task
        task._queue        = self
        task.enqueue_time = Time.now
        future = @executor.submit(task)
      end
    end
    
    def pending
      @mutex.synchronize do
        @pending.dup
      end
    end
    
    def completed
      @mutex.synchronize do
        @completed.dup
      end
    end
        
    def stop
      @mutex.synchronize do
        @executor.shutdown_now
      end
    end
    
    def cancel_all
      @mutex.synchronize do
        @pending.each {|task| task.send(:_set_cancelled) }
        @completed += @pending
        @pending   = []
      end
    end

    private
    
    def started_task(task)
      @mutex.synchronize do
        @in_process = task
        @pending.delete(task)
      end
    end
    
    def completed_task(task)
      @mutex.synchronize do
        @pending.delete(task)
        @in_process = nil if @in_process == task
        @completed << task
        if @completed.length > MAX_COMPLETED_LENGTH
          @completed = @completed[(-1*MAX_COMPLETED_LENGTH)..-1]
        end
      end
    end
  end
end

Version data entries

70 entries across 70 versions & 2 rubygems

Version Path
redcar-dev-0.12.16dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.15dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.14dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.13dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.12dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.11dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.10dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.9dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.8dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.7dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.6dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.4dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.3dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.1dev-java plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.0dev-java plugins/core/lib/core/task_queue.rb
redcar-0.11 plugins/core/lib/core/task_queue.rb
redcar-0.11.0dev plugins/core/lib/core/task_queue.rb
redcar-0.10 plugins/core/lib/core/task_queue.rb
redcar-0.9.2 plugins/core/lib/core/task_queue.rb
redcar-0.9.1 plugins/core/lib/core/task_queue.rb