Class: BlackStack::Pampa::Node

Inherits:
Object
  • Object
show all
Includes:
Infrastructure::NodeModule
Defined in:
lib/pampa.rb

Overview

stub node class stub node class is already defined in the blackstack-nodes gem: github.com/leandrosardi/blackstack-nodes we inherit from it to add some extra methods and attributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, i_logger = nil) ⇒ Node

initialize the node



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/pampa.rb', line 289

def initialize(h, i_logger=nil)
    errors = BlackStack::Pampa::Node.descriptor_errors(h)
    raise "The node descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
    super(h, i_logger)
    self.max_workers = h[:max_workers]
    self.workers = []
    self.max_workers.times do |i|
        new_worker = BlackStack::Pampa::Worker.new({:id => "#{self.name}.#{(i+1).to_s}", :node => self.to_hash})
        new_worker.node = self
        self.workers << new_worker
    end
end

Instance Attribute Details

#max_workersObject

array of workers belonging to this node



277
278
279
# File 'lib/pampa.rb', line 277

def max_workers
  @max_workers
end

#workersObject

Returns the value of attribute workers.



278
279
280
# File 'lib/pampa.rb', line 278

def workers
  @workers
end

Class Method Details

.descriptor_errors(h) ⇒ Object

add validations to the node descriptor



280
281
282
283
284
285
286
287
# File 'lib/pampa.rb', line 280

def self.descriptor_errors(h)
  errors = BlackStack::Infrastructure::NodeModule.descriptor_errors(h)
  # validate: the key :max_workers exists and is an integer
  errors << "The key :max_workers is missing" if h[:max_workers].nil?
  errors << "The key :max_workers must be an integer" unless h[:max_workers].is_a?(Integer)
  # return list of errors
  errors.uniq
end

Instance Method Details

#kill_worker(worker_id) ⇒ Object



317
318
319
# File 'lib/pampa.rb', line 317

def kill_worker(worker_id)
    self.exec("kill -9 $(ps -ef | grep \"ruby worker.rb id=#{worker_id}\" | grep -v grep | awk '{print $2}')", false)
end

#kill_workersObject

kill all workers



312
313
314
315
316
# File 'lib/pampa.rb', line 312

def kill_workers()
    self.workers.each do |worker|
        self.kill_worker(worker.id)
    end
end

#to_hashObject

returh a hash descriptor of the node



302
303
304
305
306
307
308
309
310
# File 'lib/pampa.rb', line 302

def to_hash()
    ret = super()
    ret[:max_workers] = self.max_workers
    ret[:workers] = []
    self.workers.each do |worker|
        ret[:workers] << worker.to_hash
    end
    ret
end