Module: BlackStack::Workmesh

Defined in:
lib/workmesh.rb

Defined Under Namespace

Classes: Node, Protocol, Service

Constant Summary collapse

@@roundrobin =

hash with the round-robin positions per service.

{}
@@nodes =

infrastructure configuration

[]
@@services =
[]
@@log_filename =

logger configuration

nil
@@logger =
BlackStack::DummyLogger.new(nil)
@@connection_string =

Connection string to the database. Example: mysql2://user:password@localhost:3306/database

nil

Class Method Summary collapse

Class Method Details

.add_node(h) ⇒ Object

add_node add a node to the infrastructure



281
282
283
# File 'lib/workmesh.rb', line 281

def self.add_node(h)
  @@nodes << BlackStack::Workmesh::Node.new(h)
end

.add_service(h) ⇒ Object

add_service add a service to the infrastructure



295
296
297
# File 'lib/workmesh.rb', line 295

def self.add_service(h)
  @@services << BlackStack::Workmesh::Service.new(h)
end

.assign(o, service_name, h = {}) ⇒ Object

assign object to a node



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/workmesh.rb', line 347

def self.assign(o, service_name, h = {})
  # getting the service
  s = @@services.select { |s| s.name.to_s == service_name.to_s }.first
  # validate: the service exists
  raise "The service #{service_name} does not exists" if s.nil?
  # validate: the object o is an instance of the Sequel Class mapping the table :entity_table
  raise "The object o is not an instance of :entity_table (#{s.entity_table.to_s})" unless o.is_a?(Sequel::Model) && o.class.table_name.to_s == s.entity_table.to_s
  # reassign
  if h[:reassign] == true
    o[s.entity_field_assignation] = nil
    o.save
  end
  # validate: the object o has not been assigned yet
  raise "The object o has been already assigned to a node. Use the :reassign parameter for reassignation." unless o[s.entity_field_assignation].nil?
  # decide the assignation method
  if s.assignation == :entityweight
    raise 'The assignation method :entityweight is not implemented yet.'
  elsif s.assignation == :roundrobin
    return BlackStack::Workmesh.roundrobin(o, service_name)
  elsif s.assignation == :entitynumber
    raise 'The assignation method :entitynumber is not implemented yet.'
  else
    raise "The assignation method #{s.assignation} is unknown."
  end
end

.assigned_node(o, service_name) ⇒ Object

return the assigned node to an object, for a specific service. if the object has not a node assigned, then return nil.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/workmesh.rb', line 329

def self.assigned_node(o, service_name)
  # getting the service
  s = @@services.select { |s| s.name.to_s == service_name.to_s }.first
  # validate: the service exists
  raise "The service #{service_name} does not exists" if s.nil?
  # validate: the object o is an instance of the Sequel Class mapping the table :entity_table
  raise "The object o is not an instance of :entity_table (#{s.entity_table.to_s})" unless o.is_a?(Sequel::Model) && o.class.table_name.to_s == s.entity_table.to_s
  # if the object has not a node assigned, then return nil.
  return nil if o[s.entity_field_assignation].nil?
  # find the node
  ret = @@nodes.select { |n| n.name.to_s == o[s.entity_field_assignation].to_s }.first
  # validate: the node exists
  raise "The node #{o[s.entity_field_assignation]} does not exists in configuration file" if ret.nil?
  # return
  ret
end

.connection_stringObject

return connection string to the database. Example: mysql2://user:password@localhost:3306/database



275
276
277
# File 'lib/workmesh.rb', line 275

def self.connection_string()
  @@connection_string
end

.log_filenameObject

return the log filename.



265
266
267
# File 'lib/workmesh.rb', line 265

def self.log_filename()
  @@log_filename
end

.loggerObject

return the logger.



256
257
258
# File 'lib/workmesh.rb', line 256

def self.logger()
  @@logger
end

.node(name) ⇒ Object



289
290
291
# File 'lib/workmesh.rb', line 289

def self.node(name)
  @@nodes.select { |n| n.name.to_s == name.to_s }.first
end

.nodesObject



285
286
287
# File 'lib/workmesh.rb', line 285

def self.nodes
  @@nodes
end

.roundrobin(o, service_name) ⇒ Object

assign object to a node using a round-robin algorithm this method is used when the service assignation is :roundrobin this method is for internal use only, and it should not be called directly.



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/workmesh.rb', line 310

def self.roundrobin(o, service_name)
  @@roundrobin[service_name] = 0 if @@roundrobin[service_name].nil?
  # getting the service
  s = @@services.select { |s| s.name.to_s == service_name.to_s }.first
  # getting all the nodes assigned to the service
  nodes = @@nodes.select { |n| n.workmesh_service.to_s == service_name.to_s }.sort_by { |n| n.name.to_s }
  # increase i
  @@roundrobin[service_name] += 1
  @@roundrobin[service_name] = 0 if @@roundrobin[service_name] >= nodes.length
  # assign the object to the node
  n = nodes[@@roundrobin[service_name]]
  o[s.entity_field_assignation] = n.name
  o.save
  # return
  n
end

.service(name) ⇒ Object



303
304
305
# File 'lib/workmesh.rb', line 303

def self.service(name)
  @@services.select { |s| s.name.to_s == name.to_s }.first
end

.servicesObject



299
300
301
# File 'lib/workmesh.rb', line 299

def self.services
  @@services
end

.set_connection_string(s) ⇒ Object

define the connectionstring to the database.



270
271
272
# File 'lib/workmesh.rb', line 270

def self.set_connection_string(s)
  @@connection_string = s
end

.set_log_filename(s) ⇒ Object

define a filename for the log file.



250
251
252
253
# File 'lib/workmesh.rb', line 250

def self.set_log_filename(s)
  @@log_filename = s
  @@logger = BlackStack::LocalLogger.new(s)
end

.set_logger(l) ⇒ Object



260
261
262
# File 'lib/workmesh.rb', line 260

def self.set_logger(l)
  @@logger = l
end