Class: Nanite::Agent

Inherits:
Object show all
Includes:
AMQPHelper, ConsoleHelper, DaemonizeHelper, FileStreaming
Defined in:
lib/nanite/agent.rb

Constant Summary collapse

DEFAULT_OPTIONS =
COMMON_DEFAULT_OPTIONS.merge({:user => 'nanite', :ping_time => 15,
:default_services => []})

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DaemonizeHelper

#daemonize

Methods included from ConsoleHelper

included, #start_console

Methods included from FileStreaming

#broadcast_data, #broadcast_file, #subscribe_to_files

Methods included from AMQPHelper

#start_amqp

Constructor Details

#initialize(opts) ⇒ Agent

Returns a new instance of Agent.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/nanite/agent.rb', line 74

def initialize(opts)
  set_configuration(opts)
  @log = Log.new(@options, @identity)
  @serializer = Serializer.new(@options[:format])
  @status_proc = lambda { parse_uptime(`uptime`) rescue 'no status' }
  daemonize if @options[:daemonize]
  @amq = start_amqp(@options)
  @registry = ActorRegistry.new(@log)
  @dispatcher = Dispatcher.new(@amq, @registry, @serializer, @identity, @log, @options)
  load_actors
  setup_queue
  advertise_services
  setup_heartbeat
  start_console if @options[:console] && !@options[:daemonize]
end

Instance Attribute Details

#amqObject (readonly)

Returns the value of attribute amq.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def amq
  @amq
end

#dispatcherObject (readonly)

Returns the value of attribute dispatcher.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def dispatcher
  @dispatcher
end

#identityObject (readonly)

Returns the value of attribute identity.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def identity
  @identity
end

#logObject (readonly)

Returns the value of attribute log.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def log
  @log
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def options
  @options
end

#registryObject (readonly)

Returns the value of attribute registry.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def registry
  @registry
end

#serializerObject (readonly)

Returns the value of attribute serializer.



8
9
10
# File 'lib/nanite/agent.rb', line 8

def serializer
  @serializer
end

#status_procObject

Returns the value of attribute status_proc.



9
10
11
# File 'lib/nanite/agent.rb', line 9

def status_proc
  @status_proc
end

Class Method Details

.start(options = {}) ⇒ Object

Initializes a new agent and establishes AMQP connection. This must be used inside EM.run block or if EventMachine reactor is already started, for instance, by a Thin server that your Merb/Rails application runs on.

Agent options:

identity : identity of this agent, may be any string

status_proc : a callable object that returns agent load as a string,

defaults to load averages string extracted from `uptime`

format : format to use for packets serialization. One of the three:

:marshall, :json, or :yaml. Defaults to
Ruby's Marshall format. For interoperability with
AMQP clients implemented in other languages, use JSON.

Note that Nanite uses JSON gem,
and ActiveSupport's JSON encoder may cause clashes
if ActiveSupport is loaded after JSON gem.

root : application root for this agent, defaults to Dir.pwd

log_dir : path to directory where agent stores it’s log file

if not given, app_root is used.

file_root : path to directory to files this agent provides

defaults to app_root/files

ping_time : time interval in seconds between two subsequent heartbeat messages

this agent broadcasts. Default value is 15.

console : true tells Nanite to start interactive console

daemonize : true tells Nanite to daemonize

services : list of services provided by this agent, by default

all methods exposed by actors are listed

Connection options:

vhost : AMQP broker vhost that should be used

user : AMQP broker user

pass : AMQP broker password

host : host AMQP broker (or node of interest) runs on,

defaults to 0.0.0.0

port : port AMQP broker (or node of interest) runs on,

this defaults to 5672, port used by some widely
used AMQP brokers (RabbitMQ and ZeroMQ)

On start Nanite reads config.yml, so it is common to specify options in the YAML file. However, when both Ruby code options and YAML file specify option, Ruby code options take precedence.



70
71
72
# File 'lib/nanite/agent.rb', line 70

def self.start(options = {})
  new(options)
end

Instance Method Details

#register(actor, prefix = nil) ⇒ Object



90
91
92
# File 'lib/nanite/agent.rb', line 90

def register(actor, prefix = nil)
  registry.register(actor, prefix)
end