Class: Baykit::BayServer::Agent::GrandAgentMonitor

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/baykit/bayserver/agent/grand_agent_monitor.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agt_id, anchorable, com_channel) ⇒ GrandAgentMonitor

Returns a new instance of GrandAgentMonitor.



29
30
31
32
33
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 29

def initialize(agt_id, anchorable, com_channel)
  @agent_id = agt_id
  @anchorable = anchorable
  @communication_channel = com_channel
end

Class Attribute Details

.anchored_port_mapObject (readonly)

Returns the value of attribute anchored_port_map.



14
15
16
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 14

def anchored_port_map
  @anchored_port_map
end

.cur_idObject (readonly)

Returns the value of attribute cur_id.



13
14
15
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 13

def cur_id
  @cur_id
end

.finaleObject (readonly)

Returns the value of attribute finale.



16
17
18
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 16

def finale
  @finale
end

.monitorsObject (readonly)

Returns the value of attribute monitors.



15
16
17
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 15

def monitors
  @monitors
end

.num_agentsObject (readonly)

Returns the value of attribute num_agents.



12
13
14
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 12

def num_agents
  @num_agents
end

Instance Attribute Details

#agent_idObject (readonly)

Returns the value of attribute agent_id.



25
26
27
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 25

def agent_id
  @agent_id
end

#anchorableObject (readonly)

Returns the value of attribute anchorable.



26
27
28
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 26

def anchorable
  @anchorable
end

#communication_channelObject (readonly)

Returns the value of attribute communication_channel.



27
28
29
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 27

def communication_channel
  @communication_channel
end

Class Method Details

.abort_allObject



200
201
202
203
204
205
206
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 200

def self.abort_all()
  @finale = true
  @monitors.dup().values.each do |mon|
    mon.abort()
  end
  exit(1)
end

.add(anchoroable) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 95

def self.add(anchoroable)
  @cur_id = @cur_id + 1
  agt_id = @cur_id
  if agt_id > 100
    BayLog.error("Too many agents started")
    exit(1)
  end

  if BayServer.harbor.multi_core
    new_argv = BayServer.commandline_args.dup
    new_argv.insert(0, "ruby")
    new_argv << "-agentid=" + agt_id.to_s

    ports = ""

    no_close_io = {}  # Port list not to close on spawned
    @anchored_port_map.each_key do |ch|
      no_close_io[ch] = ch
      if ports != ""
        ports +=","
      end
      ports += ch.fileno.to_s
    end
    new_argv << "-ports=" + ports

    server = TCPServer.open("localhost", 0)
    #BayLog.info("port=%d", server.local_address.ip_port)
    new_argv << "-monitor_port=" + server.local_address.ip_port.to_s

    if SysUtil.run_on_windows()
      child = spawn(ENV, new_argv.join(" "))
    else
      child = spawn(ENV, new_argv.join(" "), no_close_io)
    end

    BayLog.debug("Process spawned cmd=%s pid=%d", new_argv, child)

    client_socket = server.accept()
    server.close()

  else

    if SysUtil::run_on_windows()
      pair = Socket.socketpair(Socket::AF_INET, Socket::SOCK_STREAM, 0)
    else
      pair = Socket.socketpair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0)
    end

    client_socket = pair[0]
    GrandAgent.add(agt_id, anchoroable)

    # Agents run on single core (thread mode)
    Thread.new() do
      agt = GrandAgent.get(agt_id)
      agt.run_command_receiver(pair[1])
      agt.run()
    end

  end

  @monitors[agt_id] =
    GrandAgentMonitor.new(
      agt_id,
      anchoroable,
      client_socket)
end

.agent_aborted(agt_id, anchorable) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 162

def self.agent_aborted(agt_id, anchorable)
  BayLog.info(BayMessage.get(:MSG_GRAND_AGENT_SHUTDOWN, agt_id))

  @monitors.delete(agt_id)

  if not @finale
    if @monitors.length < @num_agents
      begin
        if !BayServer.harbor.multi_core
          GrandAgent.add(-1, anchorable)
        end
        add(anchorable)
      rescue => e
        BayLog.error_e(e)
      end
    end
  end
end

.init(num_agents, anchored_port_map) ⇒ Object

Class methods



87
88
89
90
91
92
93
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 87

def self.init(num_agents, anchored_port_map)
  @num_agents = num_agents
  @anchored_port_map = anchored_port_map
  @num_agents.times do
    add(true)
  end
end


208
209
210
211
212
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 208

def self.print_usage_all()
  @monitors.values.each do |mon|
    mon.print_usage()
  end
end

.reload_cert_allObject



181
182
183
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 181

def self.reload_cert_all()
  @monitors.values.each { |mon| mon.reload_cert() }
end

.restart_allObject



185
186
187
188
189
190
191
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 185

def self.restart_all()
  old_monitors = @monitors.dup()

  #@agent_count.times {add()}

  old_monitors.values.each { |mon| mon.shutdown() }
end

.shutdown_allObject



193
194
195
196
197
198
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 193

def self.shutdown_all()
  @finale = true
  @monitors.dup().values.each do |mon|
    mon.shutdown()
  end
end

Instance Method Details

#abortObject



59
60
61
62
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 59

def abort()
  BayLog.debug("%s Send abort command", self)
  send(GrandAgent::CMD_ABORT)
end

#closeObject



79
80
81
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 79

def close()
  @communication_channel.close()
end

#on_readableObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 39

def on_readable()
  begin
    res = IOUtil.read_int32(@communication_channel)
    if res == nil || res == GrandAgent::CMD_CLOSE
      BayLog.debug("%s read Close", self)
      close()
      GrandAgentMonitor.agent_aborted(@agent_id, @anchorable)
    else
      BayLog.debug("%s read OK: %d", self, res)
    end
  rescue IO::WaitReadable
    #BayLog.debug("%s no data", self)
  end
end


69
70
71
72
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 69

def print_usage()
  BayLog.debug("%s Send mem_usage command", self)
  send(GrandAgent::CMD_MEM_USAGE)
end

#reload_certObject



64
65
66
67
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 64

def reload_cert()
  BayLog.debug("%s Send reload command", self)
  send(GrandAgent::CMD_RELOAD_CERT)
end

#send(cmd) ⇒ Object



74
75
76
77
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 74

def send(cmd)
  BayLog.debug("%s send command %s ch=%s", self, cmd, @communication_channel)
  IOUtil.write_int32(@communication_channel, cmd)
end

#shutdownObject



54
55
56
57
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 54

def shutdown()
  BayLog.debug("%s send shutdown command", self)
  send(GrandAgent::CMD_SHUTDOWN)
end

#to_sObject



35
36
37
# File 'lib/baykit/bayserver/agent/grand_agent_monitor.rb', line 35

def to_s()
  return "Monitor##{@agent_id}"
end