Skip to content

3.0.0a3 #127

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
merged 11 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IDOM_DATABASE param
  • Loading branch information
Archmonger committed Feb 21, 2023
commit 3877e8d2f582e8283b942ac0b1b6f7a0bf1b03b3
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Using the following categories, list your changes in this order:

### Fixed

- `view_to_component` will now retain any HTML that was defined in a `<head>` tag.
- `view_to_component` will now retain the contents of a `<head>` tag when rendering.
- React client is now set to `production` rather than `development`.
- `use_query` will now utilize `field.related_name` when postprocessing many-to-one relationships

Expand Down
3 changes: 3 additions & 0 deletions docs/python/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"idom": {"BACKEND": ...},
}

# IDOM works best with a multiprocessing-safe and thread-safe database backend.
IDOM_DATABASE = "default"

# Maximum seconds between reconnection attempts before giving up.
# Use `0` to prevent component reconnection.
IDOM_RECONNECT_MAX = 259200
Expand Down
6 changes: 6 additions & 0 deletions src/django_idom/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.conf import settings
from django.core.cache import DEFAULT_CACHE_ALIAS, BaseCache, caches
from django.db import DEFAULT_DB_ALIAS
from idom.config import IDOM_DEBUG_MODE
from idom.core.types import ComponentConstructor

Expand All @@ -29,6 +30,11 @@
if "idom" in getattr(settings, "CACHES", {})
else caches[DEFAULT_CACHE_ALIAS]
)
IDOM_DATABASE: str = getattr(
settings,
"IDOM_DATABASE",
DEFAULT_DB_ALIAS,
)
IDOM_DEFAULT_QUERY_POSTPROCESSOR: Postprocessor | None = import_dotted_path(
getattr(
settings,
Expand Down
4 changes: 4 additions & 0 deletions src/django_idom/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@


class ComponentParams(models.Model):
"""A model for storing component parameters.
All queries must be routed through `django_idom.config.IDOM_DATABASE`.
"""

uuid = models.UUIDField(primary_key=True, editable=False, unique=True) # type: ignore
data = models.BinaryField(editable=False) # type: ignore
last_accessed = models.DateTimeField(auto_now_add=True) # type: ignore
8 changes: 5 additions & 3 deletions src/django_idom/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def create_cache_key(*args):
def db_cleanup(immediate: bool = False):
"""Deletes expired component parameters from the database.
This function may be expanded in the future to include additional cleanup tasks."""
from .config import IDOM_CACHE, IDOM_RECONNECT_MAX
from .config import IDOM_CACHE, IDOM_DATABASE, IDOM_RECONNECT_MAX
from .models import ComponentParams

cache_key: str = create_cache_key("last_cleaned")
Expand All @@ -324,7 +324,7 @@ def db_cleanup(immediate: bool = False):
expires_by: datetime = timezone.now() - timedelta(seconds=IDOM_RECONNECT_MAX)

# Component params exist in the DB, but we don't know when they were last cleaned
if not cleaned_at_str and ComponentParams.objects.all():
if not cleaned_at_str and ComponentParams.objects.using(IDOM_DATABASE).all():
_logger.warning(
"IDOM has detected component sessions in the database, "
"but no timestamp was found in cache. This may indicate that "
Expand All @@ -334,5 +334,7 @@ def db_cleanup(immediate: bool = False):
# Delete expired component parameters
# Use timestamps in cache (`cleaned_at_str`) as a no-dependency rate limiter
if immediate or not cleaned_at_str or timezone.now() >= clean_needed_by:
ComponentParams.objects.filter(last_accessed__lte=expires_by).delete()
ComponentParams.objects.using(IDOM_DATABASE).filter(
last_accessed__lte=expires_by
).delete()
IDOM_CACHE.set(cache_key, now_str)
10 changes: 8 additions & 2 deletions src/django_idom/websocket/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ async def receive_json(self, content: Any, **_) -> None:

async def _run_dispatch_loop(self):
from django_idom import models
from django_idom.config import IDOM_RECONNECT_MAX, IDOM_REGISTERED_COMPONENTS
from django_idom.config import (
IDOM_DATABASE,
IDOM_RECONNECT_MAX,
IDOM_REGISTERED_COMPONENTS,
)

scope = self.scope
dotted_path = scope["url_route"]["kwargs"]["dotted_path"]
Expand Down Expand Up @@ -91,7 +95,9 @@ async def _run_dispatch_loop(self):
await convert_to_async(db_cleanup)()

# Get the queries from a DB
params_query = await models.ComponentParams.objects.aget(
params_query = await models.ComponentParams.objects.using(
IDOM_DATABASE
).aget(
uuid=uuid,
last_accessed__gt=now - timedelta(seconds=IDOM_RECONNECT_MAX),
)
Expand Down