Class: IO::Tail::Tailable

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

Direct Known Subclasses

File, Process

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#break_if_eofObject

If this attribute is set to a true value, File::Fail’s tail method raises a BreakException if the end of the file is reached.



81
82
83
# File 'lib/io/tail.rb', line 81

def break_if_eof
  @break_if_eof
end

#default_bufsizeObject

Default buffer size, that is used while going backward from a file’s end. This defaults to nil, which means that File::Tail attempts to derive this value from the filesystem block size.



90
91
92
# File 'lib/io/tail.rb', line 90

def default_bufsize
  @default_bufsize
end

#intervalObject

The start value of the sleep interval. This value goes against max_interval if the tailed file is silent for a sufficient time.



49
50
51
# File 'lib/io/tail.rb', line 49

def interval
  @interval
end

#max_intervalObject

The maximum interval File::Tail sleeps, before it tries to take some action like reading the next few lines or reopening the file.



44
45
46
# File 'lib/io/tail.rb', line 44

def max_interval
  @max_interval
end

#reopen_deletedObject

If this attribute is set to a true value, File::Tail persists on reopening a deleted file waiting max_interval seconds between the attempts. This is useful if logfiles are moved away while rotation occurs but are recreated at the same place after a while. It defaults to true.



56
57
58
# File 'lib/io/tail.rb', line 56

def reopen_deleted
  @reopen_deleted
end

#reopen_suspiciousObject

If this attribute is set to a true value, File::Tail attempts to reopen it’s tailed file after suspicious_interval seconds of silence.



61
62
63
# File 'lib/io/tail.rb', line 61

def reopen_suspicious
  @reopen_suspicious
end

#return_if_eofObject

If this attribute is set to a true value, File::Fail’s tail method just returns if the end of the file is reached.



85
86
87
# File 'lib/io/tail.rb', line 85

def return_if_eof
  @return_if_eof
end

#suspicious_intervalObject

This attribute is the interval in seconds before File::Tail gets suspicious that something has happend to its tailed file and an attempt to reopen it is made.

If the attribute reopen_suspicious is set to a non true value, suspicious_interval is meaningless. It defaults to 60 seconds.



77
78
79
# File 'lib/io/tail.rb', line 77

def suspicious_interval
  @suspicious_interval
end

Instance Method Details

#after_reopen(&block) ⇒ Object

The callback is called with self as an argument after a reopen has occured. This allows a tailing script to find out, if a logfile has been rotated.



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

def after_reopen(&block)
  @after_reopen = block
end

#closeObject

Override me



129
130
131
# File 'lib/io/tail.rb', line 129

def close
  raise "Not implemented"
end

#tail(n = nil, &block) ⇒ Object

This method tails this file and yields to the given block for every new line that is read. If no block is given an array of those lines is returned instead. (In this case it’s better to use a reasonable value for n or set the return_if_eof or break_if_eof attribute to a true value to stop the method call from blocking.)

If the argument n is given, only the next n lines are read and the method call returns. Otherwise this method call doesn’t return, but yields to block for every new line read from this file for ever.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/io/tail.rb', line 106

def tail(n = nil, &block) # :yields: line
  @n = n
  result = []
  array_result = false
  unless block
    block = lambda { |line| result << line }
    array_result = true
  end
  preset_attributes unless @lines
  loop do
    begin
      restat
      read_line(&block)
      redo
    rescue ReopenException => e
      handle_ReopenException(e, &block)
    rescue ReturnException
      return array_result ? result : nil
    end
  end
end