Class: Kitchen::Instance::ActionRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/kitchen/instance.rb

Overview

Coordinates one instance action, including state persistence, plugin serialization, and failure wrapping.

Instance Method Summary collapse

Constructor Details

#initialize(instance, state_file) ⇒ ActionRunner

Returns a new instance of ActionRunner.



654
655
656
657
# File 'lib/kitchen/instance.rb', line 654

def initialize(instance, state_file)
  @instance = instance
  @state_file = state_file
end

Instance Method Details

#call(what) { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Runs one instance action, wrapping it in structured logging metadata and benchmarking, and serializing it when the action is registered as non-concurrent.

Parameters:

  • what (Symbol)

    the action to run

Yields:

  • the action body, given the current state hash

Raises:

  • (ActionFailed)

    if the state file could not be read or the action did not complete



669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# File 'lib/kitchen/instance.rb', line 669

def call(what, &block)
  state = nil
  begin
    state = state_file.read
  rescue ::StandardError => e
    log_failure(what, e)
    raise ActionFailed,
      "Failed to complete ##{what} action: [#{e.message}]", e.backtrace
  end

  action_id = new_id
  session_id = state[:instance_session_id] || new_id
  instance.logger.(
    action: what.to_s,
    action_id:,
    instance_session_id: session_id,
    kitchen_run_id: Kitchen.run_id
  ) do
    elapsed = Benchmark.measure do
      synchronize_or_call(what, state, &block)
    end
    record_attempt(state, what, action_id, session_id)
    state[:last_action] = what.to_s
    state[:last_error] = nil
    elapsed
  rescue ActionFailed => e
    log_failure(what, e)
    record_attempt(state, what, action_id, session_id)
    state[:last_error] = e.class.name
    raise(InstanceFailure, failure_message(what) +
      "  Please see .kitchen/logs/#{instance.name}.log for more details",
      e.backtrace)
  rescue ::StandardError => e
    log_failure(what, e)
    record_attempt(state, what, action_id, session_id)
    state[:last_error] = e.class.name
    raise ActionFailed,
      "Failed to complete ##{what} action: [#{e.message}]", e.backtrace
  ensure
    state_file.write(state)
  end
end