Class: Ultragrep::RequestPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ultragrep.rb

Direct Known Subclasses

RequestPerformancePrinter

Instance Method Summary collapse

Constructor Details

#initialize(verbose) ⇒ RequestPrinter

Returns a new instance of RequestPrinter.



15
16
17
18
19
20
21
# File 'lib/ultragrep.rb', line 15

def initialize(verbose)
  @mutex = Mutex.new
  @all_data = []
  @children_timestamps = {}
  @finish = false
  @verbose = verbose
end

Instance Method Details

#add_request(parsed_up_to, text) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/ultragrep.rb', line 54

def add_request(parsed_up_to, text)
  @mutex.synchronize do
    if text = format_request(parsed_up_to, text)
      @all_data << [parsed_up_to, text]
    end
  end
end

#dump_bufferObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ultragrep.rb', line 23

def dump_buffer
  dump_this = []
  new_data = []

  @mutex.synchronize do
    to_this_ts = @children_timestamps.values.min || 0 # FIXME : should not be necessary, but fails with -t -p
    $stderr.puts("I've searched up through #{Time.at(to_this_ts)}") if @verbose && to_this_ts > 0 && to_this_ts != 2**50
    @all_data.each do |req|
      if req[0] <= to_this_ts
        dump_this << req
      else
        new_data << req
      end
    end
    @all_data = new_data
  end

  STDOUT.write(dump_this.sort.map(&:last).join)
  STDOUT.flush
end

#finishObject



74
75
76
77
# File 'lib/ultragrep.rb', line 74

def finish
  @finish = true
  dump_buffer
end

#format_request(parsed_up_to, text) ⇒ Object



62
63
64
# File 'lib/ultragrep.rb', line 62

def format_request(parsed_up_to, text)
  text.join
end

#runObject



44
45
46
47
48
49
50
51
52
# File 'lib/ultragrep.rb', line 44

def run
  Thread.new do
    while @all_data.size > 0 || !@finish
      sleep 2
      dump_buffer
    end
    dump_buffer
  end
end

#set_done(key) ⇒ Object



70
71
72
# File 'lib/ultragrep.rb', line 70

def set_done(key)
  @mutex.synchronize { @children_timestamps[key] = 2**50 }
end

#set_read_up_to(key, val) ⇒ Object



66
67
68
# File 'lib/ultragrep.rb', line 66

def set_read_up_to(key, val)
  @mutex.synchronize { @children_timestamps[key] = val }
end