Class: AgentManager

Inherits:
Object show all
Includes:
RightScale::Actor, RightScale::OperationResultHelper
Defined in:
lib/right_agent/actors/agent_manager.rb

Overview

Generic actor for all agents to provide basic agent management services

Constant Summary collapse

LEVELS =

Valid log levels

[:debug, :info, :warn, :error, :fatal]

Instance Method Summary collapse

Methods included from RightScale::OperationResultHelper

#cancel_result, #continue_result, #error_result, #non_delivery_result, #result_from, #retry_result, #success_result

Methods included from RightScale::Actor

included

Constructor Details

#initialize(agent) ⇒ AgentManager

Initialize broker

Parameters

agent(RightScale::Agent)

This agent



43
44
45
# File 'lib/right_agent/actors/agent_manager.rb', line 43

def initialize(agent)
  @agent = agent
end

Instance Method Details

#connect(options) ⇒ Object

Connect agent to an additional broker or reconnect it if connection has failed Assumes agent already has credentials on this broker and identity queue exists

Parameters

options(Hash)

Connect options:

:host(String)

Host name of broker

:port(Integer)

Port number of broker

:id(Integer)

Small unique id associated with this broker for use in forming alias

:priority(Integer|nil)

Priority position of this broker in list for use

  by this agent with nil meaning add to end of list
:force(Boolean):: Reconnect even if already connected

Return

res(RightScale::OperationResult)

Success unless exception is raised



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/right_agent/actors/agent_manager.rb', line 161

def connect(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  res = success_result
  begin
    if error = @agent.connect(options[:host], options[:port], options[:id], options[:priority], options[:force])
      res = error_result(error)
    end
  rescue Exception => e
    res = error_result("Failed to connect to broker", e)
  end
  res
end

#connect_failed(options) ⇒ Object

Declare one or more broker connections unusable because connection setup has failed

Parameters

options(Hash)

Failure options:

:brokers(Array)

Identity of brokers

Return

res(RightScale::OperationResult)

Success unless exception is raised



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/right_agent/actors/agent_manager.rb', line 205

def connect_failed(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  res = success_result
  begin
    if error = @agent.connect_failed(options[:brokers])
      res = error_result(error)
    end
  rescue Exception => e
    res = error_result("Failed to notify agent that brokers #{options[:brokers]} are unusable", e)
  end
  res
end

#disconnect(options) ⇒ Object

Disconnect agent from a broker

Parameters

options(Hash)

Connect options:

:host(String)

Host name of broker

:port(Integer)

Port number of broker

:remove(Boolean)

Remove broker from configuration in addition to disconnecting it

Return

res(RightScale::OperationResult)

Success unless exception is raised



184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/right_agent/actors/agent_manager.rb', line 184

def disconnect(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  res = success_result
  begin
    if error = @agent.disconnect(options[:host], options[:port], options[:remove])
      res = error_result(error)
    end
  rescue Exception => e
    res = error_result("Failed to disconnect from broker", e)
  end
  res
end

#execute(code) ⇒ Object

Eval given code in context of agent

Parameter

code(String)

Code to be eval’d

Return

(RightScale::OperationResult)

Success with result if code didn’t raise an exception,

otherwise failure with exception message


139
140
141
142
143
144
145
# File 'lib/right_agent/actors/agent_manager.rb', line 139

def execute(code)
  begin
    success_result(self.instance_eval(code))
  rescue Exception => e
    error_result(e.message + " at\n  " + e.backtrace.join("\n  "))
  end
end

#ping(_) ⇒ Object

Always return success along with identity, protocol version, and broker information Used for troubleshooting

Return

(RightScale::OperationResult)

Always returns success



52
53
54
55
56
57
58
# File 'lib/right_agent/actors/agent_manager.rb', line 52

def ping(_)
  success_result(:identity => @agent.options[:identity],
                 :hostname => Socket.gethostname,
                 :version  => RightScale::AgentConfig.protocol_version,
                 :brokers  => @agent.broker.status,
                 :time     => Time.now.to_i)
end

#profile(options) ⇒ Object

Profile memory use

Parameters

options(Hash)

Request options

:start(Boolean)

Whether to start profiling

:stats(Boolean)

Whether to display profile statistics to stdout

:reset(Boolean)

Whether to reset profile statistics when after displaying them

:stop(Boolean)

Whether to stop profiling

Return

(OperationResult)

Empty success result or error result with message



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/right_agent/actors/agent_manager.rb', line 83

def profile(options)
  require 'memprof'

  options = RightScale::SerializationHelper.symbolize_keys(options)
  if options[:start]
    RightScale::Log.info("[profile] Start")
    $stderr.puts "[profile] Start at #{Time.now}"
    Memprof.start
    @profiling = true
  end

  if options[:stats]
    return error_result("Profiling has not yet been started") unless @profiling
    RightScale::Log.info("[profile] GC start")
    $stderr.puts "[profile] GC at #{Time.now}"
    GC.start
    RightScale::Log.info("[profile] Display stats to stderr")
    $stderr.puts "[profile] Stats at #{Time.now}#{options[:reset] ? ' with reset' : ''}"
    options[:reset] ? Memprof.stats! : Memprof.stats
  end

  if options[:stop]
    return error_result("Profiling has not yet been started") unless @profiling
    RightScale::Log.info("[profile] Stop")
    $stderr.puts "[profile] Stop at #{Time.now}"
    Memprof.stop
    @profiling = false
  end
  success_result
end

#set_log_level(level) ⇒ Object

Change log level of agent

Parameter

level(Symbol|String)

One of :debug, :info, :warn, :error, :fatal

Return

(RightScale::OperationResult)

Success if level was changed, error otherwise



121
122
123
124
125
126
127
128
129
# File 'lib/right_agent/actors/agent_manager.rb', line 121

def set_log_level(level)
  level = level.to_sym if level.is_a?(String)
  if LEVELS.include?(level)
    RightScale::Log.level = level
    success_result
  else
    error_result("Invalid log level '#{level.to_s}'")
  end
end

#stats(options) ⇒ Object

Retrieve statistics about agent operation

Parameters:

options(Hash)

Request options:

:reset(Boolean)

Whether to reset the statistics after getting the current ones

Return

(RightScale::OperationResult)

Always returns success



68
69
70
# File 'lib/right_agent/actors/agent_manager.rb', line 68

def stats(options)
  @agent.stats(RightScale::SerializationHelper.symbolize_keys(options))
end

#terminate(options = nil) ⇒ Object

Terminate self

Parameters

options(Hash)

Terminate options

Return

true



256
257
258
259
260
261
# File 'lib/right_agent/actors/agent_manager.rb', line 256

def terminate(options = nil)
  RightScale::CommandRunner.stop
  # Delay terminate a bit to give reply a chance to be sent
  EM.next_tick { @agent.terminate }
  true
end

#tune_heartbeat(options) ⇒ Object

Tune connection heartbeat frequency for all brokers Any response to this request is not likely to get through if :immediate is specified because the broker connections will be in flux Use of :immediate should be avoided when this is a fanned out request to avoid overloading the brokers

Parameters

options(Hash)

Tune options:

:heartbeat(Integer)

New AMQP connection heartbeat setting, nil or 0 means disable

:immediate(Boolean)

Whether to tune heartbeat immediately rather than defer until next

status check

Return

res(RightScale::OperationResult)

Success unless exception is raised



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/right_agent/actors/agent_manager.rb', line 232

def tune_heartbeat(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  res = success_result
  begin
    if options[:immediate]
      if error = @agent.tune_heartbeat(options[:heartbeat])
        res = error_result(error)
      end
    else
      @agent.defer_task { @agent.tune_heartbeat(options[:heartbeat]) }
    end
  rescue Exception => e
    res = error_result("Failed to tune heartbeat", e)
  end
  res
end