From 4d6c98c35c4525d8f3280161048dec571f28b379 Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Thu, 18 May 2023 14:23:30 -0700 Subject: [PATCH] Warning if poor performance is detected --- CHANGELOG.md | 4 +++- src/reactpy_django/utils.py | 10 ++++++++++ src/reactpy_django/websocket/consumer.py | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 55a8c0f5..08ee1489 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,7 +34,9 @@ Using the following categories, list your changes in this order: ## [Unreleased] -- Nothing (yet) +### Added + +- Added warning if poor system/cache/database performance is detected. ## [3.1.0] - 2023-05-06 diff --git a/src/reactpy_django/utils.py b/src/reactpy_django/utils.py index 86425552..c44429ee 100644 --- a/src/reactpy_django/utils.py +++ b/src/reactpy_django/utils.py @@ -316,6 +316,7 @@ def db_cleanup(immediate: bool = False): from .config import REACTPY_CACHE, REACTPY_DATABASE, REACTPY_RECONNECT_MAX from .models import ComponentSession + clean_started_at = datetime.now() cache_key: str = create_cache_key("last_cleaned") now_str: str = datetime.strftime(timezone.now(), DATE_FORMAT) cleaned_at_str: str = caches[REACTPY_CACHE].get(cache_key) @@ -340,3 +341,12 @@ def db_cleanup(immediate: bool = False): last_accessed__lte=expires_by ).delete() caches[REACTPY_CACHE].set(cache_key, now_str, timeout=None) + + # Check if cleaning took abnormally long + clean_duration = datetime.now() - clean_started_at + if clean_duration.total_seconds() > 1: + _logger.warning( + "ReactPy has taken %s seconds to clean up expired component sessions. " + "This may indicate a performance issue with your system, cache, or database.", + clean_duration.total_seconds(), + ) diff --git a/src/reactpy_django/websocket/consumer.py b/src/reactpy_django/websocket/consumer.py index 3679f00a..aab994d3 100644 --- a/src/reactpy_django/websocket/consumer.py +++ b/src/reactpy_django/websocket/consumer.py @@ -77,7 +77,7 @@ async def _run_dispatch_loop(self): carrier=ComponentWebsocket(self.close, self.disconnect, dotted_path), ) now = timezone.now() - component_args: Sequence[Any] = tuple() + component_args: Sequence[Any] = () component_kwargs: MutableMapping[str, Any] = {} # Verify the component has already been registered