The Watchdog library does not work long-term

58 views Asked by At

I have a python script that uses watchdog library to monitor a folder on the server and when a file is created it does something with it. The script runs as a service with nssm, and it basically works fine for a couple of hours, then it doesn't do anything although the service is running and I don't find any error or warnings in EventViewer.

My code looks like this which is a simple implementation of watchdog library: ''' import time import logging import signal

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from logging.handlers import TimedRotatingFileHandler

from project import Program
from config import Config


class MonitorFolder(FileSystemEventHandler):

    def __init__(self, config: Config) -> None:

        self.logfile_path = config.config.get('settings','logfile_path')   
        self.monitor_path = str(config.config.get('settings','monitor_path')).replace(r'\\\\',r'\\')
        #----------------Logging---------------------------------       
        ##FileHandler
        logger=logging.getLogger()
        logger.setLevel(logging.INFO)    
        handler=TimedRotatingFileHandler(filename = self.logfile_path, when ='D', interval = 1)
        formatter=logging.Formatter('%(levelname)s %(asctime)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        # ##ConsoleHandler
        console=logging.StreamHandler()
        console.setFormatter (formatter)
        console.setLevel(logging.INFO)
        logging.getLogger().addHandler(console)
        #----------------Logging---------------------------------
    
    def on_created(self, event):
        
        if ('Archive' in event.src_path or 'Error' in event.src_path ):
            return
         
        logging.info(f'Folder monitor received "{event.src_path}" event for "{event.event_type}"')

        time.sleep(5)
        
        prog = Program(event.src_path, config)        
        prog.main()

    

if __name__ == "__main__":

    config = Config()
    event_handler = MonitorFolder(config)
    observer = Observer()
    observer.schedule(event_handler, path = event_handler.monitor_path, recursive = True)

    logging.info("Monitoring started...")

    observer.start()
    try:
        while(True):
            time.sleep(1)
            
    except Exception as e:
        logging.error(e)
        observer.stop()
    observer.join()

'''

Also, I don't have any error whatsoever on the app logfile. Does anyone have a similar problem?

0

There are 0 answers