getnikola / nikola

A static website and blog generator
https://getnikola.com/
MIT License
2.6k stars 444 forks source link

Make rebuild/reload act only when file is modified. #3677

Closed gipi closed 1 year ago

gipi commented 1 year ago

Improve the event handler using directly the watchdog library's own handlers and trigger the actions only when a file is modified.

Should fix #3638

Pull Request Checklist

Description

The original implementation triggered rebuild and reload on each possible filesystem event that is time consuming and can cause infinite loops, it's probably better to limit the rebuilding on file modification.

gipi commented 1 year ago

I'll complete all the steps in the checklist once I'm sure the change is ok, maybe I could add some tests.

Kwpolska commented 1 year ago

I was able to reproduce this issue, but only with the latest version of watchdog. The issue seems to be caused by file open and close events (which weren’t raised before, and in fact, there is no separate method for file open events).

The on_modified event is not enough for us, as we also want to trigger rebuilds when files are created or deleted. I think we should keep the on_any_event handler, but filter out the opened/closed events. I think a better change would be to keep overriding dispatch, but instead filter the event types:

def dispatch(self, event):
    """Dispatch events to handler."""
    if event.event_type in {"opened", "closed"}:
        return
    self.loop.call_soon_threadsafe(asyncio.ensure_future, self.function(event))

Can you check if this fixes things for you as well? If yes, please change your pull request to the above.

gipi commented 1 year ago

I changed the patch as indicated and it seems to fix my issue.

gipi commented 1 year ago

Probably on_any_event() should be removed then.