Skip to content

Commit

Permalink
watch_safely
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgeny Arshinov authored and earshinov committed Mar 28, 2024
1 parent 9298703 commit 3eb321c
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions src/django_watchfiles/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
from __future__ import annotations

import fnmatch
import logging
import threading
import time
from pathlib import Path
from typing import Any
from typing import Callable
from typing import Generator
from typing import Iterable
from typing import Optional
from typing import Set
from typing import Tuple
from typing import TypeVar

import watchfiles
from django.utils import autoreload

logger = logging.getLogger("django_watchfiles")


# Duplicate `FileChange` type from `watchfiles`, which is not exported
FileChange = Tuple[watchfiles.Change, str]


T = TypeVar("T")


def watch_safely(f: Callable[[], Iterable[T]], default: T) -> Iterable[T]:
"""
Yield from `f()`, but when it fails, yield `default` once, log the exception and retry,
unless there are 2 exceptions within 1 second, in which case the exception is raised.
"""
ts: float | None = None
while True:
try:
yield from f()
except Exception as e:
current_ts = time.monotonic()
if ts is not None and current_ts - ts < 1.0:
# Consider 2 exceptions within 1 second critical and exit to avoid endlessly looping
raise
logger.warn(e, exc_info=True)
ts = current_ts
yield default


class MutableWatcher:
"""
Expand All @@ -34,17 +68,21 @@ def set_roots(self, roots: set[Path]) -> None:
def stop(self) -> None:
self.stop_event.set()

def __iter__(self) -> Generator[Any, None, None]: # TODO: better type
def __iter__(self) -> Generator[set[FileChange], None, None]:
no_changes: set[FileChange] = set()
while True:
self.change_event.clear()
for changes in watchfiles.watch(
*self.roots,
watch_filter=self.filter,
stop_event=self.stop_event,
debounce=False,
rust_timeout=100,
yield_on_timeout=True,
ignore_permission_denied=True,
for changes in watch_safely(
lambda: watchfiles.watch(
*self.roots,
watch_filter=self.filter,
stop_event=self.stop_event,
debounce=False,
rust_timeout=100,
yield_on_timeout=True,
ignore_permission_denied=True,
),
default=no_changes,
):
if self.change_event.is_set():
break
Expand Down

0 comments on commit 3eb321c

Please sign in to comment.