Class: InstanceAgent::Runner::Child

Inherits:
ProcessManager::Daemon::Child
  • Object
show all
Defined in:
lib/instance_agent/runner/child.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runnerObject

Returns the value of attribute runner.



8
9
10
# File 'lib/instance_agent/runner/child.rb', line 8

def runner
  @runner
end

Instance Method Details

#descriptionObject



42
43
44
45
46
47
48
# File 'lib/instance_agent/runner/child.rb', line 42

def description
  if runner
    "#{runner.description} of master #{master_pid.inspect}"
  else
    'booting child'
  end
end

#load_plugins(plugins) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/instance_agent/runner/child.rb', line 10

def load_plugins(plugins)
  ProcessManager::Log.debug("Registering Plugins: #{plugins.inspect}.")
  plugins.each do |plugin|
    plugin_dir = File.expand_path("../plugins/#{plugin}/register_plugin", File.dirname(__FILE__))
    ProcessManager::Log.debug("Loading plugin #{plugin} from #{plugin_dir}")
    begin
      require plugin_dir
    rescue LoadError => e
      ProcessManager::Log.error("Plugin #{plugin} could not be loaded: #{e.message}.")
      raise
    end
  end
  registered_plugins = InstanceAgent::Agent::Base.plugins
  ProcessManager::Log.debug("Registered Plugins: #{registered_plugins.inspect}.")
  Hash[registered_plugins.map.with_index { |value, index| [index, value] }]
end

#prepare_runObject



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

def prepare_run
  @plugins ||= load_plugins(ProcessManager::Config.config[:plugins] || ["codedeploy"])
  validate_index
  with_error_handling do
    @runner = @plugins[index].runner
    ProcessManager.set_program_name(description)
  end
end

#runObject



36
37
38
39
40
# File 'lib/instance_agent/runner/child.rb', line 36

def run
  with_error_handling do
    runner.run
  end
end

#validate_indexObject

Raises:

  • (ArgumentError)


50
51
52
# File 'lib/instance_agent/runner/child.rb', line 50

def validate_index
  raise ArgumentError, "Invalid index #{index.inspect}" unless @plugins.keys.include?(index)
end

#with_error_handlingObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/instance_agent/runner/child.rb', line 54

def with_error_handling
  yield
rescue SocketError => e
  ProcessManager::Log.info "#{description}: failed to run as the connection failed! #{e.class} - #{e.message} - #{e.backtrace.join("\n")}"
  sleep ProcessManager::Config.config[:wait_after_connection_problem]
  exit 1
rescue Exception => e
  if (e.message.to_s.match(/throttle/i) || e.message.to_s.match(/rateexceeded/i) rescue false)
    ProcessManager::Log.error "#{description}: ran into throttling - waiting for #{ProcessManager::Config.config[:wait_after_throttle_error]}s until retrying"
    sleep ProcessManager::Config.config[:wait_after_throttle_error]
  else
    ProcessManager::Log.error "#{description}: error during start or run: #{e.class} - #{e.message} - #{e.backtrace.join("\n")}"
  end
  exit 1
end