Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observer stops handling events after a while #829

Open
mmeendez8 opened this issue Aug 20, 2021 · 6 comments
Open

Observer stops handling events after a while #829

mmeendez8 opened this issue Aug 20, 2021 · 6 comments

Comments

@mmeendez8
Copy link

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?

@narkefrakt
Copy link

narkefrakt commented Nov 16, 2021

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()


@binyaz
Copy link

binyaz commented Jan 25, 2022

I'm running into the same issue.
was anything found on this?

windows 10
python==3.10
watchdog==2.1.6

@Mahrkeenerh
Copy link

Just ran into this issue as well. Guess I'll have to restart the observer once in a while.

@mgoutelle
Copy link

mgoutelle commented Jun 8, 2023

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?

@davetapley
Copy link

Just saw the same, PollingObserver and SMB mount as @narkefrakt 😑

@ImHoffy
Copy link

ImHoffy commented Jul 12, 2023

Same here

Windows Server 2016
Python 3.11.4
watchdog 3.0.0
SMB mount

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants