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.



86
87
88
89
90
91
92
# File 'lib/nanite/agent.rb', line 86

def initialize(opts)
  set_configuration(opts)
  @tags = []
  @tags << opts[:tag]
  @tags.flatten!
  @options.freeze
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

#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

#tagsObject (readonly)

Returns the value of attribute tags.



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

def tags
  @tags
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

pid_dir : path to the directory where the agent stores its pid file (only if daemonized)

defaults to the root or the current working directory.

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

all methods exposed by actors are listed

single_threaded: Run all operations in one thread

threadpool_size: Number of threads to run operations in

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.



80
81
82
83
84
# File 'lib/nanite/agent.rb', line 80

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

Instance Method Details

#register(actor, prefix = nil) ⇒ Object



119
120
121
# File 'lib/nanite/agent.rb', line 119

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

#register_security(security, deny_token = "Denied") ⇒ Object

Can be used in agent’s initialization file to register a security module This security module ‘authorize’ method will be called back whenever the agent receives a request and will be given the corresponding deliverable. It should return ‘true’ for the request to proceed. Requests will return ‘deny_token’ or the string “Denied” by default when ‘authorize’ does not return ‘true’.



129
130
131
132
# File 'lib/nanite/agent.rb', line 129

def register_security(security, deny_token = "Denied")
  @security = security
  @deny_token = deny_token
end

#runObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/nanite/agent.rb', line 94

def run
  Log.init(@identity, @options[:log_path])
  Log.level = @options[:log_level] if @options[:log_level]
  @serializer = Serializer.new(@options[:format])
  @status_proc = lambda { parse_uptime(`uptime 2> /dev/null`) rescue 'no status' }
  pid_file = PidFile.new(@identity, @options)
  pid_file.check
  if @options[:daemonize]
    daemonize(@identity, @options)
    pid_file.write
    at_exit { pid_file.remove }
  end
  @amq = start_amqp(@options)
  @registry = ActorRegistry.new
  @dispatcher = Dispatcher.new(@amq, @registry, @serializer, @identity, @options)
  setup_mapper_proxy
  load_actors
  setup_traps
  setup_queue
  advertise_services
  setup_heartbeat
  at_exit { un_register } unless $TESTING
  start_console if @options[:console] && !@options[:daemonize]
end