Class: Nodule::Topology

Inherits:
Object
  • Object
show all
Defined in:
lib/nodule/topology.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Topology

Returns a new instance of Topology.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/nodule/topology.rb', line 32

def initialize(opts={})
  @resources = {}
  @started = {}

  opts.each do |name,value|
    inject_topology(name, value)
    @resources[name] = value
  end

  @all_stopped = true
end

Instance Method Details

#[](key) ⇒ Object



51
52
53
# File 'lib/nodule/topology.rb', line 51

def [](key)
  @resources[key]
end

#[]=(key, value) ⇒ Object



55
56
57
58
# File 'lib/nodule/topology.rb', line 55

def []=(key, value)
  inject_topology(key, value)
  @resources[key] = value
end

#cleanupObject



169
170
171
# File 'lib/nodule/topology.rb', line 169

def cleanup
  @resources.each { |_,object| object.stop }
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/nodule/topology.rb', line 60

def has_key?(key)
  @resources.has_key?(key)
end

#inject_topology(name, value) ⇒ Object



44
45
46
47
48
49
# File 'lib/nodule/topology.rb', line 44

def inject_topology(name, value)
  unless value.respond_to? :join_topology!
    raise TopologyIntegrationRequiredError.new "#{name} => #{value} does not respond to :join_topology!"
  end
  value.join_topology! self, name
end

#key(object) ⇒ Object



68
69
70
# File 'lib/nodule/topology.rb', line 68

def key(object)
  @resources.key(object)
end

#keysObject



64
65
66
# File 'lib/nodule/topology.rb', line 64

def keys
  @resources.keys
end

#reset_allObject

Reset all processes for restart.



189
190
191
192
# File 'lib/nodule/topology.rb', line 189

def reset_all
  raise TopologyProcessStillRunningError.new unless @all_stopped
  @resources.each { |_, object| object.reset }
end

#run_seriallyObject

Run each process in order, waiting for each one to complete & return before running the next.

Resources are all started up at once.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/nodule/topology.rb', line 93

def run_serially
  @all_stopped = false

  @resources.each do |name,object|
    object.run
    if object.respond_to? :wait
      object.wait
    else
      object.stop
    end
  end

  @all_stopped = true
end

#start(*names) ⇒ Object

Starts the node in the topology. Looks up the node’s command given that the topology hash is keyed off of the node’s name.



112
113
114
115
116
117
118
119
120
# File 'lib/nodule/topology.rb', line 112

def start(*names)
  @all_stopped = false
  names.flatten.each do |name|
    # run the command that starts up the node and store the subprocess for later manipulation
    @resources[name].run unless @started[name]

    @started[name] = true
  end
end

#start_allObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/nodule/topology.rb', line 76

def start_all
  @resources.keys.each do |key|
    start key unless @started[key]
  end

  # If we do many cycles, this will wind up getting called repeatedly.
  # The @all_stopped variable will make sure that's a really fast
  # operation.
  at_exit { stop_all }
end

#start_all_but(*resources) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/nodule/topology.rb', line 151

def start_all_but(*resources)
  @resources.keys.each do |key|
    if !@started[key] && !resources.flatten.map(&:to_sym).include?(key)
      start key
    end
  end

  at_exit { stop_all_but resources }
end

#started?(key) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/nodule/topology.rb', line 147

def started?(key)
  @started[key.to_sym] == true
end

#stop(*names) ⇒ Object

Immediately kills a node given its topology name



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/nodule/topology.rb', line 125

def stop(*names)
  names.flatten.each do |name|
    object = @resources[name]
    object.stop
    object.wait 1 unless object.done?
    object.stop!  unless object.done?
    object.wait 1 unless object.done?
    unless object.done?
      raise "Could not stop resource: #{object.class} #{object.inspect}"
    end

    @started[name] = false
  end
end

#stop_allObject

Kills all of the nodes in the topology.



143
144
145
# File 'lib/nodule/topology.rb', line 143

def stop_all
  @resources.each { |name,object| stop name unless object.done? } unless @all_stopped
end

#stop_all_but(*resources) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/nodule/topology.rb', line 161

def stop_all_but(*resources)
  @resources.each do |name,object|
    if !resources.flatten.map(&:to_sym).include?(name.to_sym) && !object.done?
      stop name
    end
  end unless @all_stopped
end

#to_hashObject



72
73
74
# File 'lib/nodule/topology.rb', line 72

def to_hash
  @resources
end

#wait(name, timeout = 60) ⇒ Object



173
174
175
# File 'lib/nodule/topology.rb', line 173

def wait(name, timeout=60)
  @resources[name].wait timeout
end

#wait_allObject

Wait for all resources to exit normally.



180
181
182
183
184
# File 'lib/nodule/topology.rb', line 180

def wait_all
  @resources.each do |name,object|
    object.wait if object.respond_to? :wait
  end
end