Class: Resque::Pool::Dynamic::Logfile

Inherits:
IO::Tail::Logfile
  • Object
show all
Defined in:
lib/resque/pool/dynamic/logfile.rb

Overview

Slightly customized tailable file class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#pidInteger, NilClass

PID of process that writes the log file

Returns:

  • (Integer, NilClass)


32
33
34
# File 'lib/resque/pool/dynamic/logfile.rb', line 32

def pid
  @pid
end

Class Method Details

.open(filename, opts = {}, &block) ⇒ Object



34
35
36
37
# File 'lib/resque/pool/dynamic/logfile.rb', line 34

def self.open(filename, opts = {}, &block)
  raise "Need :pid" unless opts.has_key? :pid
  super
end

Instance Method Details

#alive?Boolean

True if child process is alive

Returns:

  • (Boolean)


40
41
42
# File 'lib/resque/pool/dynamic/logfile.rb', line 40

def alive?
  Process.getpgid(pid) rescue nil
end

#restatObject

Make sure to return on EOF if process is dead



45
46
47
48
# File 'lib/resque/pool/dynamic/logfile.rb', line 45

def restat
  self.return_if_eof = true unless alive?
  super
end

#tail(n = nil) {|ln| ... } ⇒ Object

Show last n lines of the file, or follow the file

Parameters:

  • n (Integer, NilClass) (defaults to: nil)

    Number of lines or nil

Yields:

  • (ln)

    Called for every line of file

Yield Parameters:

  • ln (String)

    Input line



55
56
57
58
59
60
61
62
# File 'lib/resque/pool/dynamic/logfile.rb', line 55

def tail(n = nil, &block)
  super(n) do |ln|
    $stderr.puts ln
    block.call(ln) if block
  end
rescue Interrupt => e
  nil
end

#tail_to_eofObject

Print contents of the file until current end of file



74
75
76
77
78
79
# File 'lib/resque/pool/dynamic/logfile.rb', line 74

def tail_to_eof
  orig_return_if_eof, return_if_eof = return_if_eof, true
  tail
ensure
  return_if_eof = orig_return_if_eof
end

#tail_until(rx) ⇒ Object

Follow the file until a line matches regexp



66
67
68
69
70
# File 'lib/resque/pool/dynamic/logfile.rb', line 66

def tail_until(rx)
  self.tail do |ln|
    raise IO::Tail::ReturnException if ln =~ rx
  end
end