-
-
Notifications
You must be signed in to change notification settings - Fork 23
Robust auto-cleaning functionality #222
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
Merged
Archmonger
merged 28 commits into
reactive-python:main
from
Archmonger:robust-cleanup-config
Feb 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
2a031df
Quick draft of auto-cleaning functionality
Archmonger f8b99fc
remove auto_clean from config name
Archmonger 685db22
Add type hinting to _cached_static_contents function
Archmonger eba80d3
ignore_config
Archmonger 2e089de
docs cleanup
Archmonger 4ea41f5
CLEAN_NEEDED_BY caching
Archmonger 836d719
fix link
Archmonger dbf7e12
fix tests
Archmonger e1bd185
minor docs wordsmithing
Archmonger e2c088f
Merge remote-tracking branch 'upstream/main' into robust-cleanup-config
Archmonger 43685b8
Add management command
Archmonger 6c1ecf0
add changelog entries
Archmonger ef90460
Add checks for new config values
Archmonger 5a57807
better interface for management command
Archmonger 75670a8
ignore settings.py attributes for management command calls
Archmonger 3594cee
add a check for common misspelling
Archmonger da855dd
Add comment for a bugfix
Archmonger 107b64c
Auto-Clean Settings docs
Archmonger c6b8e13
fix a few stale docs lines
Archmonger 43ee1ac
remove dead LOC
Archmonger 2799ed0
Django Query Postprocessor docs cleanup
Archmonger e8fe1da
allow REACTPY_CLEAN_INTERVAL to be None
Archmonger 08e1025
prepare for management command docs
Archmonger 32ceea6
management command docs
Archmonger 3385887
minor docs styling change
Archmonger e0c771d
avoid django timezone module
Archmonger 05554dc
fix tests
Archmonger ad62a68
fix tests in a different way
Archmonger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
from typing import TYPE_CHECKING | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.utils import timezone | ||
|
||
from reactpy_django.utils import get_pk | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
if TYPE_CHECKING: | ||
from reactpy_django.models import Config | ||
|
||
|
||
def clean_all(immediate: bool = False, manual_clean=True): | ||
from reactpy_django.config import ( | ||
REACTPY_AUTO_CLEAN_SESSIONS, | ||
REACTPY_AUTO_CLEAN_USER_DATA, | ||
) | ||
from reactpy_django.models import Config | ||
|
||
config = Config.load() | ||
if immediate or is_clean_needed(config): | ||
if manual_clean or REACTPY_AUTO_CLEAN_SESSIONS: | ||
clean_sessions() | ||
if manual_clean or REACTPY_AUTO_CLEAN_USER_DATA: | ||
clean_user_data() | ||
config.cleaned_at = timezone.now() | ||
config.save() | ||
|
||
|
||
def clean_sessions(): | ||
"""Deletes expired component sessions from the database. | ||
As a performance optimization, this is only run once every REACTPY_SESSION_MAX_AGE seconds. | ||
""" | ||
from reactpy_django.config import REACTPY_DEBUG_MODE, REACTPY_SESSION_MAX_AGE | ||
from reactpy_django.models import ComponentSession | ||
|
||
start_time = timezone.now() | ||
expiration_date = timezone.now() - timedelta(seconds=REACTPY_SESSION_MAX_AGE) | ||
ComponentSession.objects.filter(last_accessed__lte=expiration_date).delete() | ||
|
||
if REACTPY_DEBUG_MODE: | ||
inspect_clean_duration(start_time, "component sessions") | ||
|
||
|
||
def clean_user_data(): | ||
"""Delete any user data that is not associated with an existing `User`. | ||
This is a safety measure to ensure that we don't have any orphaned data in the database. | ||
|
||
Our `UserDataModel` is supposed to automatically get deleted on every `User` delete signal. | ||
However, we can't use Django to enforce this relationship since ReactPy can be configured to | ||
use any database. | ||
""" | ||
from reactpy_django.config import REACTPY_DEBUG_MODE | ||
from reactpy_django.models import UserDataModel | ||
|
||
start_time = timezone.now() | ||
user_model = get_user_model() | ||
all_users = user_model.objects.all() | ||
all_user_pks = all_users.values_list(user_model._meta.pk.name, flat=True) # type: ignore | ||
UserDataModel.objects.exclude(user_pk__in=all_user_pks).delete() | ||
|
||
if REACTPY_DEBUG_MODE: | ||
inspect_clean_duration(start_time, "user data") | ||
|
||
|
||
def is_clean_needed(config: Config | None = None) -> bool: | ||
from reactpy_django.config import REACTPY_SESSION_MAX_AGE | ||
from reactpy_django.models import Config | ||
|
||
config = config or Config.load() | ||
cleaned_at = config.cleaned_at | ||
clean_needed_by = cleaned_at + timedelta(seconds=REACTPY_SESSION_MAX_AGE) | ||
|
||
return timezone.now() >= clean_needed_by | ||
|
||
|
||
def inspect_clean_duration(start_time: datetime, task_name: str): | ||
clean_duration = timezone.now() - start_time | ||
if clean_duration.total_seconds() > 1: | ||
_logger.warning( | ||
"ReactPy has taken %s seconds to clean %s. " | ||
"This may indicate a performance issue with your system, cache, or database.", | ||
clean_duration.total_seconds(), | ||
task_name, | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.