Class: CloudCrowd::NodeRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CloudCrowd::NodeRecord
- Defined in:
- lib/cloud_crowd/models/node_record.rb
Overview
A NodeRecord is the central server’s record of a Node running remotely. We can use it to assign WorkUnits to the Node, and keep track of its status. When a Node exits, it destroys this record.
Class Method Summary collapse
-
.check_in(params, request) ⇒ Object
Register a Node with the central server.
Instance Method Summary collapse
-
#actions ⇒ Object
What Actions is this Node able to run?.
-
#busy? ⇒ Boolean
Is this Node too busy for more work? Determined by number of workers, or the Node’s load average, as configured in config.yml.
-
#display_status ⇒ Object
The printable status of the Node.
-
#node ⇒ Object
Keep a RestClient::Resource handy for contacting the Node, including HTTP authentication, if configured.
-
#release_work_units ⇒ Object
Release all of this Node’s WorkUnits for other nodes to take.
-
#send_work_unit(unit) ⇒ Object
Dispatch a WorkUnit to this node.
-
#to_json(opts = {}) ⇒ Object
The JSON representation of a NodeRecord includes its worker_pids.
-
#url ⇒ Object
The URL at which this Node may be reached.
-
#worker_pids ⇒ Object
A list of the process ids of the workers currently being run by the Node.
Class Method Details
.check_in(params, request) ⇒ Object
Register a Node with the central server. Currently this only happens at Node startup.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/cloud_crowd/models/node_record.rb', line 22 def self.check_in(params, request) attrs = { :ip_address => request.ip, :port => params[:port], :busy => params[:busy], :max_workers => params[:max_workers], :enabled_actions => params[:enabled_actions] } self.find_or_create_by_host(params[:host]).update_attributes!(attrs) end |
Instance Method Details
#actions ⇒ Object
What Actions is this Node able to run?
50 51 52 |
# File 'lib/cloud_crowd/models/node_record.rb', line 50 def actions enabled_actions.split(',') end |
#busy? ⇒ Boolean
Is this Node too busy for more work? Determined by number of workers, or the Node’s load average, as configured in config.yml.
56 57 58 |
# File 'lib/cloud_crowd/models/node_record.rb', line 56 def busy? busy || (max_workers && work_units.count >= max_workers) end |
#display_status ⇒ Object
The printable status of the Node.
73 74 75 |
# File 'lib/cloud_crowd/models/node_record.rb', line 73 def display_status busy? ? 'busy' : 'available' end |
#node ⇒ Object
Keep a RestClient::Resource handy for contacting the Node, including HTTP authentication, if configured.
68 69 70 |
# File 'lib/cloud_crowd/models/node_record.rb', line 68 def node @node ||= RestClient::Resource.new(url, CloudCrowd.) end |
#release_work_units ⇒ Object
Release all of this Node’s WorkUnits for other nodes to take.
83 84 85 |
# File 'lib/cloud_crowd/models/node_record.rb', line 83 def release_work_units WorkUnit.update_all('node_record_id = null, worker_pid = null', "node_record_id = #{id}") end |
#send_work_unit(unit) ⇒ Object
Dispatch a WorkUnit to this node. Places the node at back at the end of the rotation. If we fail to send the WorkUnit, we consider the node to be down, and remove this record, freeing up all of its checked-out work units. If the Node responds that it’s overloaded, we mark it as busy. Returns true if the WorkUnit was dispatched successfully.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cloud_crowd/models/node_record.rb', line 38 def send_work_unit(unit) result = node['/work'].post(:work_unit => unit.to_json) unit.assign_to(self, JSON.parse(result)['pid']) touch && true rescue Errno::ECONNREFUSED # Couldn't post to node, assume it's gone away. destroy && false rescue RestClient::RequestFailed => e raise e unless e.http_code == 503 && e.http_body == Node::OVERLOADED_MESSAGE update_attribute(:busy, true) && false end |
#to_json(opts = {}) ⇒ Object
The JSON representation of a NodeRecord includes its worker_pids.
88 89 90 91 92 93 |
# File 'lib/cloud_crowd/models/node_record.rb', line 88 def to_json(opts={}) { 'host' => host, 'workers' => worker_pids, 'status' => display_status }.to_json end |
#url ⇒ Object
The URL at which this Node may be reached. TODO: Make sure that the host actually has externally accessible DNS.
62 63 64 |
# File 'lib/cloud_crowd/models/node_record.rb', line 62 def url @url ||= "http://#{host}:#{port}" end |
#worker_pids ⇒ Object
A list of the process ids of the workers currently being run by the Node.
78 79 80 |
# File 'lib/cloud_crowd/models/node_record.rb', line 78 def worker_pids work_units.all(:select => 'worker_pid').map(&:worker_pid) end |