Class: Smith::AgentBootstrap

Inherits:
Object
  • Object
show all
Includes:
Logger, Utils
Defined in:
lib/smith/bootstrap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#agent_directories, check_and_create_directory, class_from_name, class_name_from_path, path_from_class, split_path

Methods included from Logger

included

Constructor Details

#initialize(name, uuid) ⇒ AgentBootstrap

Returns a new instance of AgentBootstrap.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smith/bootstrap.rb', line 20

def initialize(name, uuid)
  Dir.chdir('/')

  # FIXME
  # This doesn't do what I think it should. If an exception is
  # thrown in setup_control_queue, for example, it just kills
  # the agent without it actually raising the exception.
  Thread.abort_on_exception = true
  @agent_name = name
  @agent_uuid = uuid
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



15
16
17
# File 'lib/smith/bootstrap.rb', line 15

def agent
  @agent
end

Instance Method Details

#load_agentObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/smith/bootstrap.rb', line 42

def load_agent
  path = agent_directories(@agent_name)
  logger.info { "Loading #{@agent_name}" }
  logger.debug { "Loading #{@agent_name} from: #{path}" }
  add_agent_load_path(path)
  load path

  begin
    @agent = class_from_name(@agent_name).new(@agent_uuid)
  rescue NameError => e
    # TODO: include the class name from the path.
    logger.fatal { "Cannot instantiate agent. File #{path} exists but doesn't contain the Class: #{@agent_name}." }
    terminate!
    false
  end
end

#shutdownObject

Clean shutdown of the agent.



87
88
89
90
# File 'lib/smith/bootstrap.rb', line 87

def shutdown
  unlink_pid_file
  Smith.stop if Smith.running?
end

#signal_handlersObject



32
33
34
35
36
37
38
39
40
# File 'lib/smith/bootstrap.rb', line 32

def signal_handlers
  logger.debug { "Installing default signal handlers" }
  %w{TERM INT QUIT}.each do |sig|
    @agent.install_signal_handler(sig) do |sig|
      logger.error { "Agent received: signal #{sig}: #{agent.name} (#{agent.uuid})" }
      terminate!
    end
  end
end

#start!Object



59
60
61
62
# File 'lib/smith/bootstrap.rb', line 59

def start!
  write_pid_file
  @agent.run
end

#terminate!(exception = nil) ⇒ Object

Exceptional shutdown of the agent. Note. Whenever this is called it almost certain that the reactor is not going to be running. So it must be restarted and then shutdown again See the note at the in main.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smith/bootstrap.rb', line 68

def terminate!(exception=nil)

  handle_excecption(exception)

  if Smith.running?
    send_dead_message
    unlink_pid_file
    Smith.stop
  else
    logger.debug { "Reconnecting to AMQP Broker." }
    Smith.start do
      send_dead_message
      unlink_pid_file
      Smith.stop
    end
  end
end