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



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

def initialize(agent)
  @agent = agent
  @error_tracker = RightScale::ErrorTracker.instance
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

(RightScale::OperationResult)

Success unless exception is raised



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

def connect(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  result = success_result
  begin
    if (error = @agent.connect(options[:host], options[:port], options[:id], options[:priority], options[:force]))
      result = error_result(error)
    end
  rescue Exception => e
    @error_tracker.log(self, error = "Failed to connect to broker", e)
    result = error_result(error, e)
  end
  result
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

(RightScale::OperationResult)

Success unless exception is raised



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

def connect_failed(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  result = success_result
  begin
    if (error = @agent.connect_failed(options[:brokers]))
      result = error_result(error)
    end
  rescue Exception => e
    @error_tracker.log(self, error = "Failed to notify agent that brokers #{options[:brokers]} are unusable", e)
    result = error_result(error, e)
  end
  result
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

(RightScale::OperationResult)

Success unless exception is raised



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

def disconnect(options)
  options = RightScale::SerializationHelper.symbolize_keys(options)
  result = success_result
  begin
    if (error = @agent.disconnect(options[:host], options[:port], options[:remove]))
      result = error_result(error)
    end
  rescue Exception => e
    @error_tracker.log(self, error = "Failed to disconnect from broker", e)
    result = error_result(error, e)
  end
  result
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


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

def execute(code)
  begin
    success_result(self.instance_eval(code))
  rescue Exception => e
    error_result("Failed executing: #{code.inspect}", e, :trace)
  end
end

#ping(_) ⇒ Object

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

Return

(RightScale::OperationResult)

Always returns success



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

def ping(_)
  success_result(:identity => @agent.options[:identity],
                 :hostname => Socket.gethostname,
                 :version  => RightScale::AgentConfig.protocol_version,
                 :client   => @agent.client.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



82
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
# File 'lib/right_agent/actors/agent_manager.rb', line 82

def profile(options)
  return error_result("The memprof gem is not available for profiling.  Please install memprof 0.3 manually") unless require_succeeds?('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



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

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



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

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

#terminate(options = nil) ⇒ Object

Terminate self

Parameters

options(Hash)

Terminate options

Return

true



227
228
229
230
231
232
# File 'lib/right_agent/actors/agent_manager.rb', line 227

def terminate(options = nil)
  RightScale::CommandRunner.stop
  # Delay terminate a bit to give request message a chance to be ack'd and reply to be sent
  EM.add_timer(1) { @agent.terminate }
  true
end