Class: Tomo::Runtime::TaskRunner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tomo/runtime/task_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugins_registry:, settings:) ⇒ TaskRunner

Returns a new instance of TaskRunner.



11
12
13
14
15
16
17
18
19
# File 'lib/tomo/runtime/task_runner.rb', line 11

def initialize(plugins_registry:, settings:)
  interpolated_settings = SettingsInterpolation.interpolate(
    plugins_registry.settings.merge(settings)
  )
  @helper_modules = plugins_registry.helper_modules.freeze
  @context = Context.new(interpolated_settings)
  @tasks_by_name = plugins_registry.bind_tasks(context).freeze
  freeze
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/tomo/runtime/task_runner.rb', line 9

def context
  @context
end

Instance Method Details

#connect(host) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tomo/runtime/task_runner.rb', line 35

def connect(host)
  Current.with(host:) do
    conn = SSH.connect(host:, options: ssh_options)
    remote = Remote.new(conn, context, helper_modules)
    return remote unless block_given?

    begin
      return yield(remote)
    ensure
      remote&.close if block_given?
    end
  end
end

#run(task:, remote:) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/tomo/runtime/task_runner.rb', line 27

def run(task:, remote:)
  validate_task!(task)
  Current.with(task:, remote:) do
    Tomo.logger.task_start(task)
    tasks_by_name[task].call
  end
end

#validate_task!(name) ⇒ Object



21
22
23
24
25
# File 'lib/tomo/runtime/task_runner.rb', line 21

def validate_task!(name)
  return if tasks_by_name.key?(name)

  UnknownTaskError.raise_with(name, unknown_task: name, known_tasks: tasks_by_name.keys)
end