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.



651
652
653
654
# File 'lib/kitchen/instance.rb', line 651

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

Instance Method Details

#call(what, &block) ⇒ Object



656
657
658
659
660
661
662
663
664
665
666
667
668
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
# File 'lib/kitchen/instance.rb', line 656

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