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
-
.add_node(h) ⇒ Object
add_node add a node to the infrastructure.
-
.add_service(h) ⇒ Object
add_service add a service to the infrastructure.
-
.assign(o, service_name, h = {}) ⇒ Object
assign object to a node.
-
.assigned_node(o, service_name) ⇒ Object
return the assigned node to an object, for a specific service.
-
.connection_string ⇒ Object
return connection string to the database.
-
.log_filename ⇒ Object
return the log filename.
-
.logger ⇒ Object
return the logger.
- .node(name) ⇒ Object
- .nodes ⇒ Object
-
.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.
- .service(name) ⇒ Object
- .services ⇒ Object
-
.set_connection_string(s) ⇒ Object
define the connectionstring to the database.
-
.set_log_filename(s) ⇒ Object
define a filename for the log file.
- .set_logger(l) ⇒ Object
Class Method Details
.add_node(h) ⇒ Object
add_node add a node to the infrastructure
301 302 303 |
# File 'lib/workmesh.rb', line 301 def self.add_node(h) @@nodes << BlackStack::Workmesh::Node.new(h) end |
.add_service(h) ⇒ Object
add_service add a service to the infrastructure
315 316 317 |
# File 'lib/workmesh.rb', line 315 def self.add_service(h) @@services << BlackStack::Workmesh::Service.new(h) end |
.assign(o, service_name, h = {}) ⇒ Object
assign object to a node
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/workmesh.rb', line 367 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.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/workmesh.rb', line 349 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_string ⇒ Object
return connection string to the database. Example: mysql2://user:password@localhost:3306/database
295 296 297 |
# File 'lib/workmesh.rb', line 295 def self.connection_string() @@connection_string end |
.log_filename ⇒ Object
return the log filename.
285 286 287 |
# File 'lib/workmesh.rb', line 285 def self.log_filename() @@log_filename end |
.logger ⇒ Object
return the logger.
276 277 278 |
# File 'lib/workmesh.rb', line 276 def self.logger() @@logger end |
.node(name) ⇒ Object
309 310 311 |
# File 'lib/workmesh.rb', line 309 def self.node(name) @@nodes.select { |n| n.name.to_s == name.to_s }.first end |
.nodes ⇒ Object
305 306 307 |
# File 'lib/workmesh.rb', line 305 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.
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/workmesh.rb', line 330 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
323 324 325 |
# File 'lib/workmesh.rb', line 323 def self.service(name) @@services.select { |s| s.name.to_s == name.to_s }.first end |
.services ⇒ Object
319 320 321 |
# File 'lib/workmesh.rb', line 319 def self.services @@services end |
.set_connection_string(s) ⇒ Object
define the connectionstring to the database.
290 291 292 |
# File 'lib/workmesh.rb', line 290 def self.set_connection_string(s) @@connection_string = s end |
.set_log_filename(s) ⇒ Object
define a filename for the log file.
270 271 272 273 |
# File 'lib/workmesh.rb', line 270 def self.set_log_filename(s) @@log_filename = s @@logger = BlackStack::LocalLogger.new(s) end |
.set_logger(l) ⇒ Object
280 281 282 |
# File 'lib/workmesh.rb', line 280 def self.set_logger(l) @@logger = l end |