Class: Raindrops::Watcher::Tailer

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

Overview

This is the response body returned for “/tail/$ADDRESS.txt”. This must use a multi-threaded Rack server with streaming response support. It is an internal class and not expected to be used directly

Instance Method Summary collapse

Constructor Details

#initialize(rdmon, addr, env) ⇒ Tailer

:nodoc:



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/raindrops/watcher.rb', line 379

def initialize(rdmon, addr, env) # :nodoc:
  @rdmon = rdmon
  @addr = addr
  q = Rack::Utils.parse_query env["QUERY_STRING"]
  @active_min = q["active_min"].to_i
  @queued_min = q["queued_min"].to_i
  len = addr.size
  len = 35 if len > 35
  @fmt = "%20s % #{len}s % 10u % 10u\n"
  case env["HTTP_VERSION"]
  when "HTTP/1.0", nil
    @chunk = false
  else
    @chunk = true
  end
end

Instance Method Details

#each {|"0\r\n\r\n"| ... } ⇒ Object

called by the Rack server

Yields:

  • ("0\r\n\r\n")


407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/raindrops/watcher.rb', line 407

def each # :nodoc:
  begin
    time, all = @rdmon.wait_snapshot
    stats = all[@addr] or next
    stats.queued >= @queued_min or next
    stats.active >= @active_min or next
    body = sprintf(@fmt, time.iso8601, @addr, stats.active, stats.queued)
    body = "#{body.size.to_s(16)}\r\n#{body}\r\n" if @chunk
    yield body
  end while true
  yield "0\r\n\r\n" if @chunk
end

#finishObject



396
397
398
399
400
401
402
403
404
# File 'lib/raindrops/watcher.rb', line 396

def finish
  headers = {
    "Content-Type" => "text/plain",
    "Cache-Control" => "no-transform",
    "Expires" => Time.at(0).httpdate,
  }
  headers["Transfer-Encoding"] = "chunked" if @chunk
  [ 200, headers, self ]
end