Class: BlackStack::Workmesh::Service

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

Overview

stub worker class

Constant Summary collapse

ASSIGANTIONS =
[:entityweight, :roundrobin, :entitynumber]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Service

setup dispatcher configuration here



224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/workmesh.rb', line 224

def initialize(h)
  errors = BlackStack::Workmesh::Service.descriptor_errors(h)
  raise "The worker descriptor is not valid: #{errors.uniq.join(".\n")}" if errors.length > 0
  self.name = h[:name]
  self.entity_table = h[:entity_table]
  self.entity_field_assignation = h[:entity_field_assignation]
  self.protocols = []
  if h[:protocols]
    h[:protocols].each do |i|
      self.protocols << BlackStack::Workmesh::Protocol.new(i)
    end
  end 
  self.assignation = h[:assignation]     
end

Instance Attribute Details

#assignationObject

name to identify uniquely the worker



194
195
196
# File 'lib/workmesh.rb', line 194

def assignation
  @assignation
end

#entity_field_assignationObject

name to identify uniquely the worker



194
195
196
# File 'lib/workmesh.rb', line 194

def entity_field_assignation
  @entity_field_assignation
end

#entity_tableObject

name to identify uniquely the worker



194
195
196
# File 'lib/workmesh.rb', line 194

def entity_table
  @entity_table
end

#nameObject

name to identify uniquely the worker



194
195
196
# File 'lib/workmesh.rb', line 194

def name
  @name
end

#protocolsObject

name to identify uniquely the worker



194
195
196
# File 'lib/workmesh.rb', line 194

def protocols
  @protocols
end

Class Method Details

.descriptor_errors(h) ⇒ Object

return an array with the errors found in the description of the job



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/workmesh.rb', line 196

def self.descriptor_errors(h)
  errors = []
  # validate: the key :name exists and is an string
  errors << "The key :name is missing" if h[:name].nil?
  errors << "The key :name must be an String" unless h[:name].is_a?(String)
  # validate: the key :entity_table exists and is an symbol
  errors << "The key :entity_table is missing" if h[:entity_table].nil?
  errors << "The key :entity_table must be an Symbol" unless h[:entity_table].is_a?(Symbol)
  # validate: the key :entity_field_assignation exists and is an symbol
  errors << "The key :entity_field_assignation is missing" if h[:entity_field_assignation].nil?
  errors << "The key :entity_field_assignation must be an Symbol" unless h[:entity_field_assignation].is_a?(Symbol)
  # validate: the key :protocols exists is nil or it is an array of valid hash descritors of the Protocol class.
  errors << "The key :protocols must be an Array" unless h[:protocols].nil? || h[:protocols].is_a?(Array)
  if h[:protocols].is_a?(Array)
    h[:protocols].each do |protocol|
      errors << "The key :protocols must be an Array of valid hash descritors of the Protocol class" unless protocol.is_a?(Hash)
      errors << "The key :protocols must be an Array of valid hash descritors of the Protocol class" unless Protocol.descriptor_errors(protocol).length == 0
    end
  end
  # validate: the key :assignation is nil or it is a symbol belonging the array ASSIGANTIONS
  errors << "The key :assignation must be an Symbol" unless h[:assignation].nil? || h[:assignation].is_a?(Symbol)
  unless h[:assignation].nil?
    errors << "The key :assignation must be one of the following values: #{ASSIGANTIONS.join(", ")}" unless ASSIGANTIONS.include?(h[:assignation])
  end
  # return list of errors
  errors.uniq
end

Instance Method Details

#protocol(name) ⇒ Object

get a protocol from its name



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

def protocol(name)
  self.protocols.select { |p| p.name.to_s == name.to_s }.first
end

#to_hashObject

return a hash descriptor of the worker



239
240
241
242
243
244
245
246
247
# File 'lib/workmesh.rb', line 239

def to_hash()
  {
    :name => self.name,
    :entity_table => self.entity_table,
    :entity_field_assignation => self.entity_field_assignation,
    :protocols => self.protocols.map { |p| p.to_hash },
    :assignation => self.assignation
  }
end