Class: RightScale::CommandParser
- Defined in:
- lib/right_agent/command/command_parser.rb
Instance Method Summary collapse
-
#initialize(&block) ⇒ CommandParser
constructor
Register callback block.
-
#parse_chunk(chunk) ⇒ Object
Parse given input May cause multiple callbacks if multiple commands are successfully parsed Callback happens in next EM tick.
Constructor Details
#initialize(&block) ⇒ CommandParser
Register callback block
Block
Block that will get called back whenever a command is successfully parsed
Raise
(ArgumentError): If block is missing
35 36 37 38 39 |
# File 'lib/right_agent/command/command_parser.rb', line 35 def initialize &block raise ArgumentError, 'Missing handler block' unless block @callback = block @buildup = '' end |
Instance Method Details
#parse_chunk(chunk) ⇒ Object
Parse given input May cause multiple callbacks if multiple commands are successfully parsed Callback happens in next EM tick
Parameters
- chunk(String)
-
Chunck of serialized command(s) to be parsed
Return
- true
-
If callback was called at least once
- false
-
Otherwise
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/right_agent/command/command_parser.rb', line 51 def parse_chunk(chunk) @buildup << chunk chunks = @buildup.split(CommandSerializer::SEPARATOR, -1) if (do_call = chunks.size > 1) commands = [] (0..chunks.size - 2).each do |i| begin commands << CommandSerializer.load(chunks[i]) rescue StandardError => e # log any exceptions caused by serializing individual chunks instead # of halting EM. each command is discrete so we need to keep trying # so long as there are more commands to process (although subsequent # commands may lack context if previous commands failed). Log.error("Failed parsing command chunk", e, :trace) end end commands.each do |cmd| EM.next_tick do begin @callback.call(cmd) rescue Exception => e # log any exceptions raised by callback instead of halting EM. Log.error("Failed executing parsed command", e, :trace) end end end @buildup = chunks.last end do_call rescue StandardError => e # log any other exceptions instead of halting EM. Log.error("Failed parsing command chunk", e, :trace) end |