lib/invoker/command_worker.rb in invoker-1.0.4 vs lib/invoker/command_worker.rb in invoker-1.1.0

- old
+ new

@@ -22,13 +22,39 @@ Invoker::Logger.print(".") end # Print the lines received over the network def receive_line(line) - Invoker::Logger.puts "#{@command_label.color(color)} : #{line}" + tail_watchers = Invoker.tail_watchers[@command_label] + color_line = "#{@command_label.color(color)} : #{line}" + if tail_watchers && !tail_watchers.empty? + json_encoded_tail_response = tail_response(color_line) + if json_encoded_tail_response + tail_watchers.each { |tail_socket| send_data(tail_socket, json_encoded_tail_response) } + end + else + Invoker::Logger.puts color_line + end end def to_h - {:command_label => command_label, :pid => pid.to_s} + { command_label: command_label, pid: pid.to_s } + end + + def send_data(socket, data) + socket.write(data) + rescue + Invoker::Logger.puts "Removing #{@command_label} watcher #{socket} from list" + Invoker.tail_watchers.remove(@command_label, socket) + end + + private + + # Encode current line as json and send the response. + def tail_response(line) + tail_response = Invoker::IPC::Message::TailResponse.new(tail_line: line) + tail_response.encoded_message + rescue + nil end end end