watchdog
watchdog copied to clipboard
Observer stops handling events after a while
I have a very simple script that observes a directory and sends me some message through rocketchat on FileSystemEvents.
It works as a charm for a while but it stops processing events randomly at some moment. I added a log message that is continuosly printing so I know while loop and sleep are still running... There must be something going on with the FileSystemEventHandler
OS: Ubuntu 18.08 Python version: 3.8.5 watchdog version: 2.1.3
Code:
import time
from pathlib import Path
from loguru import logger
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
from server_status.services.base import ServerStatus
class FileWatcher(ServerStatus):
def __init__(self, directory, recursive=False):
"""Check specific directories and notifies when files created or deleted
"""
self.directory = directory
self.recursive = recursive
self.observer = Observer()
def run(self):
self.observer.schedule(Handler(self), self.directory, recursive=self.recursive)
self.observer.start()
status = f"\n FileWatcher Running in {self.directory}\n"
logger.info(status)
while True:
try:
while True:
time.sleep(10)
logger.info("Still working")
except Exception as e:
logger.info(e)
continue
self.observer.join()
logger.info("Watcher Terminated")
class Handler(FileSystemEventHandler):
def __init__(self, server_status):
self.server_status = server_status
def on_created(self, event):
owner = Path(event.src_path).owner()
icon = ":file_folder: Folder" if event.is_directory else ":pencil: File"
status = f"{icon} {event.src_path} created by user {owner}"
self.server_status.send_status(status)
def on_deleted(self, event):
if event.is_directory:
status = f":wastebasket: folder in {event.src_path} deleted"
self.server_status.send_status(status)
Note that I need to pass class FileWatcher
to Handle
constructor so I can call the send_status
method which sends a message to a RocketChat Channel.
Any idea of what can be happening here?
Getting the same issue. Works like a charm for about 24-36 hours. Then stops responding to events. Running it in PyCharm at the moment over SMB mounts (Hence PollingObserver) and noticing this.
My code:
def filewatcher():
fileEventHandler = PatternMatchingEventHandler(patterns, ignore_patters, ignore_directories, case_sensitive)
fileEventHandler.on_created = fileWatchDog_created
folderObserver = PollingObserver()
folderObserver.schedule(fileEventHandler, path=watchFolder, recursive=go_recursively)
folderObserver.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
folderObserver.stop()
folderObserver.join()
filewatcher()
I'm running into the same issue. was anything found on this?
windows 10 python==3.10 watchdog==2.1.6
Just ran into this issue as well. Guess I'll have to restart the observer once in a while.
Hi, I'm running in the exact same issue (on Linux, with inotify): events stop being triggered after a while, without any error, message, or hint whatsoever. Restarting the whole process solves the issue.
I understand this might be something difficult to troubleshoot: how can I help to dig into this, instrumenting something in watchdog
to provide more clues?
Just saw the same, PollingObserver
and SMB mount as @narkefrakt 😑
Same here
Windows Server 2016 Python 3.11.4 watchdog 3.0.0 SMB mount