Class: Arachni::RPC::Server::Dispatcher::Service
- Defined in:
- lib/arachni/rpc/server/dispatcher/service.rb
Overview
Base class and namespace for all Dispatcher services.
# RPC accessibility
Only PUBLIC methods YOU have defined will be accessible over RPC.
# Blocking operations
Please try to avoid blocking operations as they will block the main Reactor loop.
However, if you really need to perform such operations, you can update the relevant methods to expect a block and then pass the desired return value to that block instead of returning it the usual way.
This will result in the method's payload to be deferred into a Thread of its own.
In addition, you can use the #defer and #run_asap methods is you need more control over what gets deferred and general scheduling.
# Asynchronous operations
Methods which perform async operations should expect a block and pass their results to that block instead of returning a value.
Instance Attribute Summary collapse
-
#dispatcher ⇒ Object
readonly
Returns the value of attribute dispatcher.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#connect_to_dispatcher(url) ⇒ Client::Dispatcher
Connects to a Dispatcher by `url`.
-
#connect_to_instance(*args) ⇒ Client::Instance
Connects to an Instance by `url`.
-
#defer(operation = nil, callback = nil, &block) ⇒ Object
Defers a blocking operation in order to avoid blocking the main Reactor loop.
-
#each_instance(&block) ⇒ Object
Performs an asynchronous iteration over all running instances.
-
#initialize(options, dispatcher) ⇒ Service
constructor
A new instance of Service.
-
#instances ⇒ Array<Hash>
Alive instances.
-
#iterator_for(list, max_concurrency = 10) ⇒ Reactor::Iterator
Iterator for the provided array.
-
#map_instances(each, after) ⇒ Object
Performs an asynchronous map operation over all running instances.
-
#node ⇒ Server::Dispatcher::Node
Local node.
-
#run_asap(&block) ⇒ Object
Runs a block as soon as possible in the Reactor loop.
Constructor Details
#initialize(options, dispatcher) ⇒ Service
Returns a new instance of Service.
42 43 44 45 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 42 def initialize( , dispatcher ) @options = @dispatcher = dispatcher end |
Instance Attribute Details
#dispatcher ⇒ Object (readonly)
Returns the value of attribute dispatcher.
40 41 42 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 40 def dispatcher @dispatcher end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
39 40 41 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 39 def @options end |
Instance Method Details
#connect_to_dispatcher(url) ⇒ Client::Dispatcher
Connects to a Dispatcher by `url`
121 122 123 124 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 121 def connect_to_dispatcher( url ) @dispatcher_connections ||= {} @dispatcher_connections[url] ||= Client::Dispatcher.new( , url ) end |
#connect_to_instance(*args) ⇒ Client::Instance
Connects to an Instance by `url`.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 136 def connect_to_instance( *args ) url = token = nil if args.size == 2 url, token = *args elsif args.first.is_a? Hash = args.first url = ['url'] || [:url] token = ['token'] || [:token] end @instance_connections ||= {} @instance_connections[url] ||= Client::Instance.new( , url, token ) end |
#defer(operation = nil, callback = nil, &block) ⇒ Object
Defers a blocking operation in order to avoid blocking the main Reactor loop.
The operation will be run in its own Thread - DO NOT block forever.
Accepts either 2 parameters (an `operation` and a `callback` or an operation as a block.
91 92 93 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 91 def defer( operation = nil, callback = nil, &block ) Thread.new( *[operation, callback].compact, &block ) end |
#each_instance(&block) ⇒ Object
Performs an asynchronous iteration over all running instances.
70 71 72 73 74 75 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 70 def each_instance( &block ) wrap = proc do |instance, iterator| block.call( connect_to_instance( instance ), iterator ) end iterator_for( instances ).each( &wrap ) end |
#instances ⇒ Array<Hash>
Returns Alive instances.
112 113 114 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 112 def instances dispatcher.running_jobs end |
#iterator_for(list, max_concurrency = 10) ⇒ Reactor::Iterator
Returns Iterator for the provided array.
106 107 108 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 106 def iterator_for( list, max_concurrency = 10 ) Reactor.global.create_iterator( list, max_concurrency ) end |
#map_instances(each, after) ⇒ Object
Performs an asynchronous map operation over all running instances.
59 60 61 62 63 64 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 59 def map_instances( each, after ) wrap_each = proc do |instance, iterator| each.call( connect_to_instance( instance ), iterator ) end iterator_for( instances ).map( wrap_each, after ) end |
#node ⇒ Server::Dispatcher::Node
Returns Local node.
49 50 51 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 49 def node dispatcher.instance_eval { @node } end |
#run_asap(&block) ⇒ Object
Runs a block as soon as possible in the Reactor loop.
98 99 100 |
# File 'lib/arachni/rpc/server/dispatcher/service.rb', line 98 def run_asap( &block ) Reactor.global.next_tick( &block ) end |