class Tlopo::Executor

Simple Executor service aka threadpool executor

Constants

VERSION

Public Class Methods

new(size = 10) click to toggle source
# File lib/tlopo/executor.rb, line 6
def initialize(size = 10)
  @pool = SizedQueue.new(size)
  size.times { @pool << 1 }
  @mutex = Mutex.new
  @running_threads = []
  @error = []
  @jobs = Queue.new
end

Public Instance Methods

errors() click to toggle source
# File lib/tlopo/executor.rb, line 32
def errors
  @error
end
run() click to toggle source
# File lib/tlopo/executor.rb, line 19
def run
  loop do
    break if @jobs.empty?
    run_job @jobs.pop
  end
  @running_threads.each(&:join)
  self
end
schedule(task) click to toggle source
# File lib/tlopo/executor.rb, line 15
def schedule(task)
  @jobs << task
end
success?() click to toggle source
# File lib/tlopo/executor.rb, line 28
def success?
  @error.empty?
end

Private Instance Methods

run_job(task) click to toggle source
# File lib/tlopo/executor.rb, line 38
def run_job(task)
  @pool.pop
  @mutex.synchronize do
    @running_threads << Thread.start do
      begin
        task.call
      rescue Exception => e
        @error << e
      ensure
        @pool << 1
      end
    end
  end
end