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.13 plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.5dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.4dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.3dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.2dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.1dev plugins/core/lib/core/task_queue.rb
redcar-0.12.1 plugins/core/lib/core/task_queue.rb
redcar-dev-0.13.0dev plugins/core/lib/core/task_queue.rb
redcar-0.12 plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.27dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.26dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.25dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.24dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.23dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.22dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.21dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.20dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.19dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.18dev plugins/core/lib/core/task_queue.rb
redcar-dev-0.12.17dev plugins/core/lib/core/task_queue.rb