Class: Kdeploy::Runner

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

Overview

Concurrent task runner for executing tasks across multiple hosts

Instance Method Summary collapse

Constructor Details

#initialize(hosts, tasks, parallel: Configuration.default_parallel, output: ConsoleOutput.new) ⇒ Runner

Returns a new instance of Runner.



8
9
10
11
12
13
14
15
# File 'lib/kdeploy/runner.rb', line 8

def initialize(hosts, tasks, parallel: Configuration.default_parallel, output: ConsoleOutput.new)
  @hosts = hosts
  @tasks = tasks
  @parallel = parallel
  @output = output
  @pool = Concurrent::FixedThreadPool.new(@parallel)
  @results = Concurrent::Hash.new
end

Instance Method Details

#create_task_futures(task) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/kdeploy/runner.rb', line 38

def create_task_futures(task)
  @hosts.map do |name, config|
    Concurrent::Future.execute(executor: @pool) do
      execute_task_for_host(name, config, task)
    end
  end
end

#execute_concurrent_tasks(task) ⇒ Object



32
33
34
35
36
# File 'lib/kdeploy/runner.rb', line 32

def execute_concurrent_tasks(task)
  futures = create_task_futures(task)
  futures.each(&:wait)
  @results
end

#find_task(task_name) ⇒ Object

Raises:



24
25
26
27
28
29
30
# File 'lib/kdeploy/runner.rb', line 24

def find_task(task_name)
  task = @tasks[task_name]

  raise TaskNotFoundError, task_name unless task

  task
end

#run(task_name) ⇒ Object



17
18
19
20
21
22
# File 'lib/kdeploy/runner.rb', line 17

def run(task_name)
  task = find_task(task_name)
  execute_concurrent_tasks(task)
ensure
  @pool.shutdown
end