Class: ScoutAgent::Agent::MasterAgent

Inherits:
ScoutAgent::Agent show all
Defined in:
lib/scout_agent/agent/master_agent.rb

Instance Attribute Summary

Attributes inherited from ScoutAgent::Agent

#log

Instance Method Summary collapse

Methods inherited from ScoutAgent::Agent

#authorize

Methods included from Tracked

#clear_status, #force_status_database_reload, #status, #status_database, #status_log

Constructor Details

#initializeMasterAgent

Returns a new instance of MasterAgent.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/scout_agent/agent/master_agent.rb', line 10

def initialize
  super  # setup our log and status
  
  @running   = true
  @main_loop = nil
  @server    = Server.new(log)
  @db        = Database.load(:mission_log, log)
  @queue     = Database.load(:queue,       log)
  @snapshots = Database.load(:snapshots,   log)
  
  if [@db, @queue, @snapshots].any? { |db| db.nil? }
    log.fatal("Could not load all required databases.")
    exit
  end
end

Instance Method Details

#finishObject



50
51
52
53
54
55
56
57
58
# File 'lib/scout_agent/agent/master_agent.rb', line 50

def finish
  if @running
    log.info("Shutting down.")
  else
    log.warn("Received multiple shutdown signals.")
  end
  @running = false
  notice_changes
end

#notice_changesObject



44
45
46
47
48
# File 'lib/scout_agent/agent/master_agent.rb', line 44

def notice_changes
  @main_loop.run if @main_loop
rescue ThreadError  # Thread was already killed
  # do nothing:  we're shutting down and can't notice new things
end

#runObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/scout_agent/agent/master_agent.rb', line 26

def run
  log.info("Running.")
  @main_loop = Thread.new do
    Thread.current.abort_on_exception = true
    loop do
      %w[ fetch_plan 
          execute_missions 
          checkin
          perform_maintenance 
          wait_for_orders ].each do |stage|
        send(stage)
        check_running_status
      end
    end
  end
  @main_loop.join
end