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-0.9.0 plugins/core/lib/core/task_queue.rb
redcar-0.8.1 plugins/core/lib/core/task_queue.rb
redcar-0.8 plugins/core/lib/core/task_queue.rb
redcar-0.7 plugins/core/lib/core/task_queue.rb
redcar-0.6.1 plugins/core/lib/core/task_queue.rb
redcar-0.6 plugins/core/lib/core/task_queue.rb
redcar-0.6.1dev plugins/core/lib/core/task_queue.rb
redcar-0.5.1 plugins/core/lib/core/task_queue.rb
redcar-0.5 plugins/core/lib/core/task_queue.rb
redcar-0.5.6dev plugins/core/lib/core/task_queue.rb
redcar-0.5.5dev plugins/core/lib/core/task_queue.rb
redcar-0.5.4dev plugins/core/lib/core/task_queue.rb
redcar-0.5.3dev plugins/core/lib/core/task_queue.rb
redcar-0.5.2dev plugins/core/lib/core/task_queue.rb
redcar-0.5.1dev plugins/core/lib/core/task_queue.rb
redcar-0.4.1 plugins/core/lib/core/task_queue.rb
redcar-0.4 plugins/core/lib/core/task_queue.rb
redcar-0.3.10.1dev plugins/core/lib/core/task_queue.rb
redcar-0.3.10.0dev plugins/core/lib/core/task_queue.rb
redcar-0.3.9 plugins/core/lib/core/task_queue.rb