Class: WGU::DelegationAgent

Inherits:
Object
  • Object
show all
Includes:
PPSCommons
Defined in:
lib/pps_commons/delegation_agent.rb

Overview

The DelegationAgent class allows you to pass in a message and move on with life. It does this by understanding the message and deferring the correct methods for later. This class is called from the PPS agents in the work_loop.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PPSCommons

deep_find, included, unleash_the_fury_level

Constructor Details

#initialize(requester, message, connections) ⇒ DelegationAgent

Returns a new instance of DelegationAgent.



17
18
19
20
21
22
23
24
25
# File 'lib/pps_commons/delegation_agent.rb', line 17

def initialize(requester, message, connections)
  @results = []
  @requester = requester
  @message = (
    message.kind_of?(WGU::Sentence) ? message.log_entry : message.to_s
  ).strip
  @connections = connections
  @sentence = WGU::Sentence.new(requester, message)
end

Instance Attribute Details

#callbackObject

Returns the value of attribute callback.



13
14
15
# File 'lib/pps_commons/delegation_agent.rb', line 13

def callback
  @callback
end

#errorbackObject

Returns the value of attribute errorback.



14
15
16
# File 'lib/pps_commons/delegation_agent.rb', line 14

def errorback
  @errorback
end

#opperationObject

Returns the value of attribute opperation.



12
13
14
# File 'lib/pps_commons/delegation_agent.rb', line 12

def opperation
  @opperation
end

#requesterObject

Returns the value of attribute requester.



10
11
12
# File 'lib/pps_commons/delegation_agent.rb', line 10

def requester
  @requester
end

#resultsObject

Returns the value of attribute results.



15
16
17
# File 'lib/pps_commons/delegation_agent.rb', line 15

def results
  @results
end

#sentenceObject (readonly)

Returns the value of attribute sentence.



11
12
13
# File 'lib/pps_commons/delegation_agent.rb', line 11

def sentence
  @sentence
end

Class Method Details

.delegate_for(*args) ⇒ Object

Create a new DelegationAgent for this message. Inject handler abilities into this newly created DelegationAgent. Lastly, set the opperation, callback and errback methods on the Delegation agent so that they can be called later on, when they are needed.



69
70
71
72
73
74
# File 'lib/pps_commons/delegation_agent.rb', line 69

def self.delegate_for(*args)
  new_delegate = Class.new(WGU::DelegationAgent).new(*args)
  new_delegate._include_handler
  new_delegate._set_callbacks
  new_delegate
end

Instance Method Details

#_handler_info(pr = nil) ⇒ Object

use the @request, category and config file locations to find the path to the correct handler file and format the correct name for the handler.

returns:

  • Array : Name and path to handler file



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pps_commons/delegation_agent.rb', line 48

def _handler_info(pr = nil)
  handler_words = [@requester.agent_name, sentence.category, 'handler']
  handler = "WGU::#{handler_words.map(&:capitalize).join}"
  project_dir = if (pr = ENV['PPS_ROOT']).nil?
    assumed_root = File.expand_path(::File.join(__FILE__, '..', '..'))
    assumed_root.split('/')[0..-1]
  else
    pr
  end
  file_name = handler_words.join('_') + '.rb'
  handler_file = File.join(
    *project_dir, @requester.agent_name.to_s, file_name
  )

  [handler, handler_file]
end

#_include_handlerObject

Load and include the handler that will be able to do what this message will need, when it’s ready to run.



36
37
38
39
40
41
# File 'lib/pps_commons/delegation_agent.rb', line 36

def _include_handler
  handler, handler_file = _handler_info

  load(handler_file)
  self.class.send :include, Object.const_get(handler)
end

#alert_human(message) ⇒ Object

cloudwatch could do something if it saw this line and/or this method could do something like send an email or pagerduty alert etc.



29
30
31
32
# File 'lib/pps_commons/delegation_agent.rb', line 29

def alert_human(message)
  logger.info("!!!HUMAN ALERT!!!--->#{message}<---!!!HUMAN ALERT!!!")
  self
end

#callbacks_for_deferralObject

Collect the actions for this message, so that they can be easily used together, in the correct order



78
79
80
# File 'lib/pps_commons/delegation_agent.rb', line 78

def callbacks_for_deferral
  [opperation, callback, errorback]
end

#send_response_to(recipient, results = @results) ⇒ Object

Send a message to the appropriate receiver.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pps_commons/delegation_agent.rb', line 83

def send_response_to(recipient, results = @results)
  msg = results.kind_of?(WGU::Sentence) ? results.log_entry : "#{results.to_s.strip}\n"
  logger.info(
    "#{requester.agent_name.capitalize} sending message: \"#{msg}\" to -> #{recipient}"
  )

  if recipient.eql?(:broker) && !requester.agent_name.eql?(:broker)
    requester.send_data(msg)
  else
    @connections[recipient].receive_data(msg)
  end

  self
end