Class: Disbatch::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/disbatch/node.rb

Overview

Represesents a Disbatch node

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Node

Create a node object

Parameters:

  • id (String)


11
12
13
14
# File 'lib/disbatch/node.rb', line 11

def initialize(id)
  @registered = false
  @id   = id
end

Instance Attribute Details

#idObject

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

Parameters:

  • id (String) (defaults to: Disbatch.node_id)


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

Parameters:

  • id (String) (defaults to: Disbatch.node_id)


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_allObject

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

Parameters:



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.

Returns:

  • (Boolean)


102
103
104
# File 'lib/disbatch/node.rb', line 102

def registered?
  @registered
end

#releaseObject

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