Class: FileWatch::WatchedFile
- Inherits:
-
Object
- Object
- FileWatch::WatchedFile
- Includes:
- UnixInode
- Defined in:
- lib/filewatch/watched_file.rb
Instance Attribute Summary collapse
-
#accessed_at ⇒ Object
readonly
Returns the value of attribute accessed_at.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#bytes_read ⇒ Object
readonly
Returns the value of attribute bytes_read.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#filestat ⇒ Object
readonly
Returns the value of attribute filestat.
-
#last_open_warning_at ⇒ Object
Returns the value of attribute last_open_warning_at.
-
#last_stat_size ⇒ Object
readonly
Returns the value of attribute last_stat_size.
-
#listener ⇒ Object
readonly
Returns the value of attribute listener.
-
#modified_at ⇒ Object
readonly
Returns the value of attribute modified_at.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#pathname ⇒ Object
readonly
Returns the value of attribute pathname.
-
#recent_states ⇒ Object
readonly
Returns the value of attribute recent_states.
-
#sdb_key_v1 ⇒ Object
readonly
Returns the value of attribute sdb_key_v1.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #activate ⇒ Object
- #active? ⇒ Boolean
- #all_read? ⇒ Boolean
- #buffer_extract(data) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #compressed? ⇒ Boolean
- #expiry_close_enabled? ⇒ Boolean
- #expiry_ignore_enabled? ⇒ Boolean
- #file_add_opened(rubyfile) ⇒ Object
- #file_can_close? ⇒ Boolean
- #file_closable? ⇒ Boolean
- #file_close ⇒ Object
- #file_ignorable? ⇒ Boolean
- #file_open? ⇒ Boolean
- #file_read(amount) ⇒ Object
- #file_seek(amount, whence = IO::SEEK_SET) ⇒ Object
- #grown? ⇒ Boolean
- #has_listener? ⇒ Boolean
- #ignore ⇒ Object
- #ignored? ⇒ Boolean
- #increment_bytes_read(delta) ⇒ Object
- #initial? ⇒ Boolean
- #initial_completed ⇒ Object
-
#initialize(pathname, stat, settings) ⇒ WatchedFile
constructor
this class represents a file that has been discovered.
- #open ⇒ Object
- #recent_state_history ⇒ Object
- #reset_buffer ⇒ Object
- #restat ⇒ Object
- #set_accessed_at ⇒ Object
- #set_listener(observer) ⇒ Object
- #set_state(value) ⇒ Object
- #shrunk? ⇒ Boolean
- #sincedb_key ⇒ Object
- #size_changed? ⇒ Boolean
- #to_s ⇒ Object
- #unset_listener ⇒ Object
- #unwatch ⇒ Object
- #unwatched? ⇒ Boolean
- #update_bytes_read(total_bytes_read) ⇒ Object
- #update_path(_path) ⇒ Object
- #update_stat(st) ⇒ Object
- #watch ⇒ Object
- #watched? ⇒ Boolean
Methods included from UnixInode
Constructor Details
#initialize(pathname, stat, settings) ⇒ WatchedFile
this class represents a file that has been discovered
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/filewatch/watched_file.rb', line 13 def initialize(pathname, stat, settings) @settings = settings @pathname = Pathname.new(pathname) # given arg pathname might be a string or a Pathname object @path = @pathname.to_path @bytes_read = 0 @last_stat_size = 0 # the prepare_inode method is sourced from the mixed module above @sdb_key_v1 = InodeStruct.new(*prepare_inode(path, stat)) # initial as true means we have not associated this watched_file with a previous sincedb value yet. # and we should read from the beginning if necessary @initial = true @recent_states = [] # keep last 8 states, managed in set_state @state = :watched set_stat(stat) # can change @last_stat_size @listener = nil @last_open_warning_at = nil set_accessed_at end |
Instance Attribute Details
#accessed_at ⇒ Object (readonly)
Returns the value of attribute accessed_at.
8 9 10 |
# File 'lib/filewatch/watched_file.rb', line 8 def accessed_at @accessed_at end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
7 8 9 |
# File 'lib/filewatch/watched_file.rb', line 7 def buffer @buffer end |
#bytes_read ⇒ Object (readonly)
Returns the value of attribute bytes_read.
7 8 9 |
# File 'lib/filewatch/watched_file.rb', line 7 def bytes_read @bytes_read end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
7 8 9 |
# File 'lib/filewatch/watched_file.rb', line 7 def file @file end |
#filestat ⇒ Object (readonly)
Returns the value of attribute filestat.
8 9 10 |
# File 'lib/filewatch/watched_file.rb', line 8 def filestat @filestat end |
#last_open_warning_at ⇒ Object
Returns the value of attribute last_open_warning_at.
10 11 12 |
# File 'lib/filewatch/watched_file.rb', line 10 def last_open_warning_at @last_open_warning_at end |
#last_stat_size ⇒ Object (readonly)
Returns the value of attribute last_stat_size.
9 10 11 |
# File 'lib/filewatch/watched_file.rb', line 9 def last_stat_size @last_stat_size end |
#listener ⇒ Object (readonly)
Returns the value of attribute listener.
9 10 11 |
# File 'lib/filewatch/watched_file.rb', line 9 def listener @listener end |
#modified_at ⇒ Object (readonly)
Returns the value of attribute modified_at.
8 9 10 |
# File 'lib/filewatch/watched_file.rb', line 8 def modified_at @modified_at end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
8 9 10 |
# File 'lib/filewatch/watched_file.rb', line 8 def path @path end |
#pathname ⇒ Object (readonly)
Returns the value of attribute pathname.
8 9 10 |
# File 'lib/filewatch/watched_file.rb', line 8 def pathname @pathname end |
#recent_states ⇒ Object (readonly)
Returns the value of attribute recent_states.
7 8 9 |
# File 'lib/filewatch/watched_file.rb', line 7 def recent_states @recent_states end |
#sdb_key_v1 ⇒ Object (readonly)
Returns the value of attribute sdb_key_v1.
9 10 11 |
# File 'lib/filewatch/watched_file.rb', line 9 def sdb_key_v1 @sdb_key_v1 end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/filewatch/watched_file.rb', line 7 def state @state end |
Instance Method Details
#activate ⇒ Object
139 140 141 |
# File 'lib/filewatch/watched_file.rb', line 139 def activate set_state :active end |
#active? ⇒ Boolean
160 161 162 |
# File 'lib/filewatch/watched_file.rb', line 160 def active? @state == :active end |
#all_read? ⇒ Boolean
68 69 70 |
# File 'lib/filewatch/watched_file.rb', line 68 def all_read? @last_stat_size == bytes_read end |
#buffer_extract(data) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/filewatch/watched_file.rb', line 104 def buffer_extract(data) warning, additional = "", {} lines = @buffer.extract(data) if lines.empty? warning.concat("buffer_extract: a delimiter can't be found in current chunk") warning.concat(", maybe there are no more delimiters or the delimiter is incorrect") warning.concat(" or the text before the delimiter, a 'line', is very large") warning.concat(", if this message is logged often try increasing the `file_chunk_size` setting.") additional["delimiter"] = @settings.delimiter additional["read_position"] = @bytes_read additional["bytes_read_count"] = data.bytesize additional["last_known_file_size"] = @last_stat_size additional["file_path"] = @path end BufferExtractResult.new(lines, warning, additional) end |
#close ⇒ Object
148 149 150 |
# File 'lib/filewatch/watched_file.rb', line 148 def close set_state :closed end |
#closed? ⇒ Boolean
168 169 170 |
# File 'lib/filewatch/watched_file.rb', line 168 def closed? @state == :closed end |
#compressed? ⇒ Boolean
60 61 62 |
# File 'lib/filewatch/watched_file.rb', line 60 def compressed? @path.end_with?('.gz','.gzip') end |
#expiry_close_enabled? ⇒ Boolean
180 181 182 |
# File 'lib/filewatch/watched_file.rb', line 180 def expiry_close_enabled? !@settings.close_older.nil? end |
#expiry_ignore_enabled? ⇒ Boolean
184 185 186 |
# File 'lib/filewatch/watched_file.rb', line 184 def expiry_ignore_enabled? !@settings.ignore_older.nil? end |
#file_add_opened(rubyfile) ⇒ Object
76 77 78 79 |
# File 'lib/filewatch/watched_file.rb', line 76 def file_add_opened(rubyfile) @file = rubyfile @buffer = BufferedTokenizer.new(@settings.delimiter) if @buffer.nil? end |
#file_can_close? ⇒ Boolean
222 223 224 225 |
# File 'lib/filewatch/watched_file.rb', line 222 def file_can_close? return false unless expiry_close_enabled? (Time.now.to_f - @accessed_at) > @settings.close_older end |
#file_closable? ⇒ Boolean
210 211 212 |
# File 'lib/filewatch/watched_file.rb', line 210 def file_closable? file_can_close? && all_read? end |
#file_close ⇒ Object
81 82 83 84 85 |
# File 'lib/filewatch/watched_file.rb', line 81 def file_close return if @file.nil? || @file.closed? @file.close @file = nil end |
#file_ignorable? ⇒ Boolean
214 215 216 217 218 219 220 |
# File 'lib/filewatch/watched_file.rb', line 214 def file_ignorable? return false unless expiry_ignore_enabled? # (Time.now - stat.mtime) <- in jruby, this does int and float # conversions before the subtraction and returns a float. # so use all floats upfront (Time.now.to_f - @modified_at) > @settings.ignore_older end |
#file_open? ⇒ Boolean
96 97 98 |
# File 'lib/filewatch/watched_file.rb', line 96 def file_open? !@file.nil? && !@file.closed? end |
#file_read(amount) ⇒ Object
91 92 93 94 |
# File 'lib/filewatch/watched_file.rb', line 91 def file_read(amount) set_accessed_at @file.sysread(amount) end |
#file_seek(amount, whence = IO::SEEK_SET) ⇒ Object
87 88 89 |
# File 'lib/filewatch/watched_file.rb', line 87 def file_seek(amount, whence = IO::SEEK_SET) @file.sysseek(amount, whence) end |
#grown? ⇒ Boolean
192 193 194 |
# File 'lib/filewatch/watched_file.rb', line 192 def grown? @last_stat_size > @bytes_read end |
#has_listener? ⇒ Boolean
40 41 42 |
# File 'lib/filewatch/watched_file.rb', line 40 def has_listener? !@listener.nil? end |
#ignore ⇒ Object
143 144 145 146 |
# File 'lib/filewatch/watched_file.rb', line 143 def ignore set_state :ignored @bytes_read = @filestat.size end |
#ignored? ⇒ Boolean
164 165 166 |
# File 'lib/filewatch/watched_file.rb', line 164 def ignored? @state == :ignored end |
#increment_bytes_read(delta) ⇒ Object
121 122 123 124 |
# File 'lib/filewatch/watched_file.rb', line 121 def increment_bytes_read(delta) return if delta.nil? @bytes_read += delta end |
#initial? ⇒ Boolean
56 57 58 |
# File 'lib/filewatch/watched_file.rb', line 56 def initial? @initial end |
#initial_completed ⇒ Object
48 49 50 |
# File 'lib/filewatch/watched_file.rb', line 48 def initial_completed @initial = false end |
#open ⇒ Object
72 73 74 |
# File 'lib/filewatch/watched_file.rb', line 72 def open file_add_opened(FileOpener.open(@path)) end |
#recent_state_history ⇒ Object
206 207 208 |
# File 'lib/filewatch/watched_file.rb', line 206 def recent_state_history @recent_states + Array(@state) end |
#reset_buffer ⇒ Object
100 101 102 |
# File 'lib/filewatch/watched_file.rb', line 100 def reset_buffer @buffer.flush end |
#restat ⇒ Object
196 197 198 |
# File 'lib/filewatch/watched_file.rb', line 196 def restat set_stat(pathname.stat) end |
#set_accessed_at ⇒ Object
52 53 54 |
# File 'lib/filewatch/watched_file.rb', line 52 def set_accessed_at @accessed_at = Time.now.to_f end |
#set_listener(observer) ⇒ Object
32 33 34 |
# File 'lib/filewatch/watched_file.rb', line 32 def set_listener(observer) @listener = observer.listener_for(@path) end |
#set_state(value) ⇒ Object
200 201 202 203 204 |
# File 'lib/filewatch/watched_file.rb', line 200 def set_state(value) @recent_states.shift if @recent_states.size == 8 @recent_states << @state @state = value end |
#shrunk? ⇒ Boolean
188 189 190 |
# File 'lib/filewatch/watched_file.rb', line 188 def shrunk? @last_stat_size < @bytes_read end |
#sincedb_key ⇒ Object
44 45 46 |
# File 'lib/filewatch/watched_file.rb', line 44 def sincedb_key @sdb_key_v1 end |
#size_changed? ⇒ Boolean
64 65 66 |
# File 'lib/filewatch/watched_file.rb', line 64 def size_changed? @last_stat_size != bytes_read end |
#to_s ⇒ Object
227 228 229 |
# File 'lib/filewatch/watched_file.rb', line 227 def to_s inspect end |
#unset_listener ⇒ Object
36 37 38 |
# File 'lib/filewatch/watched_file.rb', line 36 def unset_listener @listener = nil end |
#unwatch ⇒ Object
156 157 158 |
# File 'lib/filewatch/watched_file.rb', line 156 def unwatch set_state :unwatched end |
#unwatched? ⇒ Boolean
176 177 178 |
# File 'lib/filewatch/watched_file.rb', line 176 def unwatched? @state == :unwatched end |
#update_bytes_read(total_bytes_read) ⇒ Object
126 127 128 129 |
# File 'lib/filewatch/watched_file.rb', line 126 def update_bytes_read(total_bytes_read) return if total_bytes_read.nil? @bytes_read = total_bytes_read end |
#update_path(_path) ⇒ Object
131 132 133 |
# File 'lib/filewatch/watched_file.rb', line 131 def update_path(_path) @path = _path end |
#update_stat(st) ⇒ Object
135 136 137 |
# File 'lib/filewatch/watched_file.rb', line 135 def update_stat(st) set_stat(st) end |
#watch ⇒ Object
152 153 154 |
# File 'lib/filewatch/watched_file.rb', line 152 def watch set_state :watched end |
#watched? ⇒ Boolean
172 173 174 |
# File 'lib/filewatch/watched_file.rb', line 172 def watched? @state == :watched end |