Class: Disbatch::Node
- Inherits:
-
Object
- Object
- Disbatch::Node
- Defined in:
- lib/disbatch/node.rb
Overview
Represesents a Disbatch node
Instance Attribute Summary collapse
-
#id ⇒ Object
Returns the value of attribute id.
Class Method Summary collapse
-
.create(id = Disbatch.node_id) ⇒ Object
Create a new node.
-
.get(id = Disbatch.node_id) ⇒ Object
Get an existing node.
-
.get_all ⇒ Object
Get all existing nodes.
Instance Method Summary collapse
-
#==(node) ⇒ Object
Check equality with another node object.
-
#initialize(id) ⇒ Node
constructor
Create a node object.
-
#register(force = false) ⇒ Object
Register node.
-
#registered? ⇒ Boolean
(also: #open?)
Check if process is registered as this node.
-
#release ⇒ Object
Release ownership of node.
Constructor Details
#initialize(id) ⇒ Node
Create a node object
11 12 13 14 |
# File 'lib/disbatch/node.rb', line 11 def initialize(id) @registered = false @id = id end |
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/disbatch/node.rb', line 4 def id @id end |
Class Method Details
.create(id = Disbatch.node_id) ⇒ Object
Create a new node
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/disbatch/node.rb', line 36 def self.create(id = Disbatch.node_id) Mongo.try do Disbatch.db[:nodes].insert({ :_id => id, :ctime => Time.now }) end new(id) end |
.get(id = Disbatch.node_id) ⇒ Object
Get an existing node
19 20 21 22 23 24 |
# File 'lib/disbatch/node.rb', line 19 def self.get(id = Disbatch.node_id) Mongo.try do raise Disbatch::NoNodeError unless Disbatch.db[:nodes].find_one({:_id => id}) new(id) end end |
.get_all ⇒ Object
Get all existing nodes
27 28 29 30 31 |
# File 'lib/disbatch/node.rb', line 27 def self.get_all Mongo.try do Disbatch.db[:nodes].find.map { |doc| new(doc['_id']) } end end |
Instance Method Details
#==(node) ⇒ Object
Check equality with another node object
109 110 111 |
# File 'lib/disbatch/node.rb', line 109 def ==(node) @id == node.id end |
#register(force = false) ⇒ Object
Register node
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/disbatch/node.rb', line 48 def register(force = false) raise Disbatch::AlreadyRegisteredNodeError if @registered begin doc = Mongo.try do Disbatch.db[:nodes].find_and_modify({ :query => force ? { :_id => @id } : { :_id => @id, :pid => nil }, :update => { :$set => { :pid => Process.pid, :version => Disbatch::VERSION, :spec_version => Disbatch::SPEC_VERSION } } }) end rescue doc = Mongo.try do Disbatch.db[:nodes].find({ :_id => @id }) end if doc.count == 0 raise Disbatch::NoNodeError else raise Disbatch::RegisteredNodeError end end @registered = true self end |
#registered? ⇒ Boolean Also known as: open?
Check if process is registered as this node.
102 103 104 |
# File 'lib/disbatch/node.rb', line 102 def registered? @registered end |
#release ⇒ Object
Release ownership of node
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/disbatch/node.rb', line 79 def release raise Disbatch::NotRegisteredNodeError unless @registered doc = Mongo.try do Disbatch.db[:nodes].find_and_modify({ :query => { :_id => @id, :pid => Process.pid }, :update => { :$set => { :pid => nil, :mtime => Time.now, :atime => Time.now } } }) end if doc.nil? false else @registered = false true end end |