class WGU::DelegationAgent
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.
Attributes
Public Class Methods
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.
# 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
# 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
Public Instance Methods
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
# 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
Load and include the handler that will be able to do what this message will need, when it's ready to run.
# 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
cloudwatch could do something if it saw this line and/or this method could do something like send an email or pagerduty alert etc.
# File lib/pps_commons/delegation_agent.rb, line 29 def alert_human(message) logger.info("!!!HUMAN ALERT!!!--->#{message}<---!!!HUMAN ALERT!!!") self end
Collect the actions for this message, so that they can be easily used together, in the correct order
# File lib/pps_commons/delegation_agent.rb, line 78 def callbacks_for_deferral [opperation, callback, errorback] end
Send a message to the appropriate receiver.
# 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
Private Instance Methods
# File lib/pps_commons/delegation_agent.rb, line 99 def _set_up_logger Logger.new('../logs/test.log') end