Class: Hydra::Worker
- Inherits:
-
Object
- Object
- Hydra::Worker
- Includes:
- Messages::Worker
- Defined in:
- lib/hydra/worker.rb
Overview
Hydra class responsible to dispatching runners and communicating with the master.
The Worker is never run directly by a user. Workers are created by a Master to delegate to Runners.
The general convention is to have one Worker per machine on a distributed network.
Instance Method Summary collapse
-
#delegate_file(message) ⇒ Object
When the master sends a file down to the worker, it hits this method.
-
#initialize(opts = {}) ⇒ Worker
constructor
Create a new worker.
-
#relay_results(message, runner) ⇒ Object
When a runner finishes, it sends the results up to the worker.
-
#request_file(message, runner) ⇒ Object
When a runner wants a file, it hits this method with a message.
-
#shutdown ⇒ Object
When a master issues a shutdown order, it hits this method, which causes the worker to send shutdown messages to its runners.
Constructor Details
#initialize(opts = {}) ⇒ Worker
Create a new worker.
-
io: The IO object to use to communicate with the master
-
num_runners: The number of runners to launch
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/hydra/worker.rb', line 14 def initialize(opts = {}) @verbose = opts.fetch(:verbose) { false } @io = opts.fetch(:io) { raise "No IO Object" } @runners = [] @listeners = [] boot_runners(opts.fetch(:runners) { 1 }) @runners.each{|r| Process.wait r[:pid] } end |
Instance Method Details
#delegate_file(message) ⇒ Object
When the master sends a file down to the worker, it hits this method. Then the worker delegates the file down to a runner.
38 39 40 41 42 |
# File 'lib/hydra/worker.rb', line 38 def delegate_file() runner = idle_runner runner[:idle] = false runner[:io].write(RunFile.new(eval(.serialize))) end |
#relay_results(message, runner) ⇒ Object
When a runner finishes, it sends the results up to the worker. Then the worker sends the results up to the master.
46 47 48 49 |
# File 'lib/hydra/worker.rb', line 46 def relay_results(, runner) runner[:idle] = true @io.write(Results.new(eval(.serialize))) end |
#request_file(message, runner) ⇒ Object
When a runner wants a file, it hits this method with a message. Then the worker bubbles the file request up to the master.
31 32 33 34 |
# File 'lib/hydra/worker.rb', line 31 def request_file(, runner) @io.write(RequestFile.new) runner[:idle] = true end |
#shutdown ⇒ Object
When a master issues a shutdown order, it hits this method, which causes the worker to send shutdown messages to its runners.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/hydra/worker.rb', line 53 def shutdown @running = false $stdout.write "WORKER| Notifying #{@runners.size} Runners of Shutdown\n" if @verbose @runners.each do |r| $stdout.write "WORKER| Sending Shutdown to Runner\n" if @verbose $stdout.write " | #{r.inspect}\n" if @verbose r[:io].write(Shutdown.new) end Thread.exit end |