Class: Lemans::Runner::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/lemans/runner/executor.rb

Overview

A default (Threaded) concurrent executor for tasks

Defined Under Namespace

Classes: Results

Instance Method Summary collapse

Constructor Details

#initialize(concurrency) ⇒ Executor

Returns a new instance of Executor.



15
16
17
18
19
20
# File 'lib/lemans/runner/executor.rb', line 15

def initialize(concurrency)
  @concurrency = concurrency
  @queue = Queue.new
  @results = Results.new(Concurrent::Array.new)
  @pool = nil
end

Instance Method Details

#<<(task) ⇒ Object



29
30
31
# File 'lib/lemans/runner/executor.rb', line 29

def <<(task)
  queue << task
end

#shutdownObject

Raises:

  • (@abort)


40
41
42
43
44
# File 'lib/lemans/runner/executor.rb', line 40

def shutdown
  queue.close
  pool.each(&:join)
  raise @abort if @abort
end

#startObject



22
23
24
25
26
27
# File 'lib/lemans/runner/executor.rb', line 22

def start
  @pool = Array.new(concurrency) do
    Thread.new { drain }
  end
  results
end

#terminateObject



33
34
35
36
37
38
# File 'lib/lemans/runner/executor.rb', line 33

def terminate
  queue.clear
  queue.close
  pool.each { it.raise(Shutdown) if it.alive? }
  pool.each(&:join)
end