@@ -127,6 +127,7 @@ def seek_to_end(self):
127127 for fid , data in self ._file_map .iteritems ():
128128 self ._logger .debug ("[{0}] - getting start position {1}" .format (fid , data ['file' ].name ))
129129 start_position = self ._beaver_config .get_field ('start_position' , data ['file' ].name )
130+ is_active = data ['active' ]
130131
131132 if self ._sincedb_path :
132133 sincedb_start_position = self ._sincedb_start_position (data ['file' ], fid = fid )
@@ -151,13 +152,21 @@ def seek_to_end(self):
151152 except UnicodeDecodeError :
152153 self ._logger .debug ("[{0}] - UnicodeDecodeError raised for {1} with encoding {2}" .format (fid , data ['file' ].name , data ['encoding' ]))
153154 data ['file' ] = self .open (data ['file' ].name , encoding = encoding )
155+ if not data ['file' ]:
156+ self .unwatch (data ['file' ], fid )
157+ is_active = False
158+ break
159+
154160 data ['encoding' ] = encoding
155161
156162 if line_count != start_position :
157163 self ._logger .debug ("[{0}] - file at different position than {1}, assuming manual truncate for {2}" .format (fid , start_position , data ['file' ].name ))
158164 data ['file' ].seek (0 , os .SEEK_SET )
159165 start_position == "beginning"
160166
167+ if not is_active :
168+ continue
169+
161170 if start_position == "beginning" :
162171 continue
163172
@@ -172,8 +181,16 @@ def seek_to_end(self):
172181 except UnicodeDecodeError :
173182 self ._logger .debug ("[{0}] - UnicodeDecodeError raised for {1} with encoding {2}" .format (fid , data ['file' ].name , data ['encoding' ]))
174183 data ['file' ] = self .open (data ['file' ].name , encoding = encoding )
184+ if not data ['file' ]:
185+ self .unwatch (data ['file' ], fid )
186+ is_active = False
187+ break
188+
175189 data ['encoding' ] = encoding
176190
191+ if not is_active :
192+ continue
193+
177194 current_position = data ['file' ].tell ()
178195 self ._logger .debug ("[{0}] - line count {1} for {2}" .format (fid , line_count , data ['file' ].name ))
179196 self ._sincedb_update_position (data ['file' ], fid = fid , lines = line_count , force_update = True )
@@ -365,58 +382,69 @@ def _ensure_files_are_good(self, current_time):
365382 fname = data ['file' ].name
366383 data ['file' ].close ()
367384 file = self .open (fname , encoding = data ['encoding' ])
368- file .seek (position )
369- self ._file_map [fid ]['file' ] = file
385+ if file :
386+ file .seek (position )
387+ self ._file_map [fid ]['file' ] = file
370388
371389 def unwatch (self , file , fid ):
372390 """file no longer exists; if it has been renamed
373391 try to read it for the last time in case the
374392 log rotator has written something in it.
375393 """
376394 try :
377- self .readfile (fid , file )
395+ if file :
396+ self .readfile (fid , file )
378397 except IOError :
379398 # Silently ignore any IOErrors -- file is gone
380399 pass
381- self ._logger .info ("[{0}] - un-watching logfile {1}" .format (fid , file .name ))
400+
401+ if file :
402+ self ._logger .info ("[{0}] - un-watching logfile {1}" .format (fid , file .name ))
403+ else :
404+ self ._logger .info ("[{0}] - un-watching logfile" .format (fid ))
405+
382406 del self ._file_map [fid ]
383407
384408 def watch (self , fname ):
385409 """Opens a file for log tailing"""
386410 try :
387411 file = self .open (fname , encoding = self ._beaver_config .get_field ('encoding' , fname ))
388- fid = self .get_file_id (os .stat (fname ))
412+ if file :
413+ fid = self .get_file_id (os .stat (fname ))
389414 except EnvironmentError , err :
390415 if err .errno != errno .ENOENT :
391416 raise
392417 else :
393- self ._logger .info ("[{0}] - watching logfile {1}" .format (fid , fname ))
394- self ._file_map [fid ] = {
395- 'encoding' : self ._beaver_config .get_field ('encoding' , fname ),
396- 'file' : file ,
397- 'line' : 0 ,
398- 'update_time' : None ,
399- }
400-
401- @classmethod
402- def open (cls , fname , encoding = None ):
418+ if file :
419+ self ._logger .info ("[{0}] - watching logfile {1}" .format (fid , fname ))
420+ self ._file_map [fid ] = {
421+ 'encoding' : self ._beaver_config .get_field ('encoding' , fname ),
422+ 'file' : file ,
423+ 'line' : 0 ,
424+ 'update_time' : None ,
425+ 'active' : True ,
426+ }
427+
428+ def open (self , filename , encoding = None ):
403429 """Opens a file with the appropriate call"""
404- if IS_GZIPPED_FILE .search (fname ):
405- file = gzip .open (fname , "rb" )
406- else :
407- if encoding :
408- file = io .open (fname , "r" , encoding = encoding )
430+ try :
431+ if IS_GZIPPED_FILE .search (filename ):
432+ _file = gzip .open (filename , "rb" )
409433 else :
410- file = io .open (fname , "r" )
411-
412- return file
434+ file_encoding = self ._beaver_config .get_field ('encoding' , filename )
435+ if encoding :
436+ _file = io .open (filename , "r" , encoding = encoding )
437+ elif file_encoding :
438+ _file = io .open (filename , "r" , encoding = file_encoding )
439+ else :
440+ _file = io .open (filename , "r" )
441+ except IOError , e :
442+ self ._logger .warning (str (e ))
443+ _file = None
413444
414- @staticmethod
415- def get_file_id (st ):
416- return "%xg%x" % (st .st_dev , st .st_ino )
445+ return _file
417446
418- @classmethod
419- def tail (cls , fname , encoding , window , position = None ):
447+ def tail (self , fname , encoding , window , position = None ):
420448 """Read last N lines from file fname."""
421449 if window <= 0 :
422450 raise ValueError ('invalid window %r' % window )
@@ -427,15 +455,21 @@ def tail(cls, fname, encoding, window, position=None):
427455
428456 for enc in encodings :
429457 try :
430- f = cls .open (fname , encoding = enc )
431- return cls .tail_read (f , window , position = position )
458+ f = self .open (fname , encoding = enc )
459+ if not f :
460+ return []
461+ return self .tail_read (f , window , position = position )
432462 except IOError , err :
433463 if err .errno == errno .ENOENT :
434464 return []
435465 raise
436466 except UnicodeDecodeError :
437467 pass
438468
469+ @staticmethod
470+ def get_file_id (st ):
471+ return "%xg%x" % (st .st_dev , st .st_ino )
472+
439473 @classmethod
440474 def tail_read (cls , f , window , position = None ):
441475 BUFSIZ = 1024
0 commit comments