Class: RightScale::CommandParser

Inherits:
Object
  • Object
show all
Defined in:
lib/right_agent/command/command_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CommandParser

Register callback block

Block

Block that will get called back whenever a command is successfully parsed

Raise

(RightScale::Exceptions::Argument): If block is missing



35
36
37
38
39
# File 'lib/right_agent/command/command_parser.rb', line 35

def initialize &block
  raise RightScale::Exceptions::Argument, '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 Exception => 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 Exception => e
  # log any other exceptions instead of halting EM.
  Log.error("Failed parsing command chunk", e, :trace)
end