Class: LogFetcher
- Inherits:
-
Object
- Object
- LogFetcher
- Defined in:
- lib/zillabyte/common/log_fetcher.rb
Class Method Summary collapse
- .fetch_logs(hash, operation = nil, exit_on = nil) ⇒ Object
- .fetch_logs_async(hash, operation = nil, exit_on = nil) ⇒ Object
Instance Method Summary collapse
- #color_for(operation_name) ⇒ Object
-
#initialize ⇒ LogFetcher
constructor
A new instance of LogFetcher.
- #run_sync(hash, operation = nil, exit_on = nil) ⇒ Object
- #start_async(hash, operation, exit_on) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize ⇒ LogFetcher
3 4 5 6 7 8 9 10 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 3 def initialize require("colorize") require("pty") @color_map = {} @all_colors = [:green, :yellow, :magenta, :cyan, :light_black, :light_green, :light_yellow, :light_blue, :light_magenta, :light_cyan] @terminate = false @pid = nil end |
Class Method Details
.fetch_logs(hash, operation = nil, exit_on = nil) ⇒ Object
78 79 80 81 82 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 78 def self.fetch_logs(hash, operation=nil, exit_on=nil) lf = LogFetcher.new lf.run_sync(hash, operation, exit_on) lf end |
.fetch_logs_async(hash, operation = nil, exit_on = nil) ⇒ Object
84 85 86 87 88 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 84 def self.fetch_logs_async(hash, operation=nil, exit_on=nil) lf = LogFetcher.new lf.start_async(hash, operation, exit_on) lf end |
Instance Method Details
#color_for(operation_name) ⇒ Object
12 13 14 15 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 12 def color_for(operation_name) @color_map[operation_name] ||= @all_colors[ @color_map.size % @all_colors.size ] @color_map[operation_name] end |
#run_sync(hash, operation = nil, exit_on = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 19 def run_sync(hash, operation=nil, exit_on=nil) ip = hash["server"] || ENV["ZILLABYTE_API_HOST"] #Quick fix for logs on zilla machines exit_on = [exit_on] unless exit_on.is_a?(Array) @color_map = {} # TODO: (nitpick) we should use ruby instead of shelling to curl -- we can't guarantee curl will exist cmd = "curl -G http://#{hash["flow"]}:#{hash["token"]}@#{ip}:#{hash["port"]}/getLogs?flow=#{hash["flow"]}" begin PTY.spawn( cmd ) do |r, w, pid| @pid = pid; break if @terminate r.each do |line| begin op = line.match(/\[(.*?)\]/)[0].to_s[1...-1] op_name = op.split(".")[0] next if op_name != operation and operation != "_ALL_" line_split = line.split("[") print line_split[0] + "[" + op.colorize(color_for(op)) + line_split[1..-1].join("[")[op.length..-1] unless line.include?("[HIDDEN]") done = false exit_on.each do |exit_str| if line.include? exit_str done = true break end end break if done rescue Exception => e next end end end rescue PTY::ChildExited => e end end |
#start_async(hash, operation, exit_on) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 60 def start_async(hash, operation, exit_on) Signal.trap("SIGINT") do self.stop() exit() end @thread = Thread.new do run_sync(hash, operation, exit_on) end end |
#stop ⇒ Object
72 73 74 75 |
# File 'lib/zillabyte/common/log_fetcher.rb', line 72 def stop @terminate = true Process.kill("SIGKILL", @pid) rescue nil end |