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



53
54
55
56
57
58
59
# File 'lib/kdeploy/runner.rb', line 53

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kdeploy/runner.rb', line 32

def execute_concurrent_tasks(task)
  futures = create_task_futures(task)

  # Show progress while waiting for tasks to complete
  total = futures.length
  completed = 0

  futures.each do |future|
    future.wait
    completed += 1
    # Show progress for multiple hosts
    next unless total > 1

    pastel = @output.respond_to?(:pastel) ? @output.pastel : Pastel.new
    @output.write_line(pastel.dim("    [Progress: #{completed}/#{total} hosts completed]"))
    @output.flush if @output.respond_to?(:flush)
  end

  @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