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, debug: false, base_dir: nil) ⇒ Runner

Returns a new instance of Runner.



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

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

Instance Method Details

#create_task_futures(task) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/kdeploy/runner.rb', line 100

def create_task_futures(task)
  # Store host names in order to match with futures
  @host_names = @hosts.keys
  @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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kdeploy/runner.rb', line 36

def execute_concurrent_tasks(task)
  futures = create_task_futures(task)

  # If no hosts, return empty results immediately
  return @results if futures.empty?

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

  # Collect results from futures
  futures.each_with_index do |future, index|
    host_name = @host_names[index] # Get host name from the stored list
    begin
      # Wait for future to complete and get its value
      # This ensures the future has finished executing
      future_result = future.value

      # Handle the result
      if future_result.nil?
        # Future returned nil - create a default result
        @results[host_name] = { status: :unknown, error: 'Future returned nil', output: [] }
      elsif future_result.is_a?(Array) && future_result.length == 2
        name, result = future_result
        # Store the result using the name from the future
        @results[name] = result
      else
        # Handle unexpected result format - create a default result
        @results[host_name] = {
          status: :unknown,
          error: "Unexpected result format: #{future_result.class}",
          output: []
        }
      end

      # Check if future raised an exception
      if future.rejected?
        error = begin
          future.reason
        rescue StandardError
          'Unknown error'
        end
        @results[host_name] = { status: :failed, error: error, output: [] } unless @results.key?(host_name)
      end
    rescue StandardError => e
      # If future.value raises an exception, create an error result
      @results[host_name] = { status: :failed, error: "#{e.class}: #{e.message}", output: [] }
    ensure
      # Ensure we always have a result for this host
      @results[host_name] ||= { status: :unknown, error: 'No result collected', output: [] }
    end

    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:



28
29
30
31
32
33
34
# File 'lib/kdeploy/runner.rb', line 28

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

  raise TaskNotFoundError, task_name unless task

  task
end

#run(task_name) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/kdeploy/runner.rb', line 20

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