Features: 1) Add initialization timestamp tracking to prevent redundant instance initialization; 2) Include .initialized file in .gitignore for production environments;
Fixes: 1) Remove unnecessary pragma from `install_aiohttp_webhook` definition; Extra: 1) Add logging for `.initialized` read/write failures; 2) General cleanup and formatting improvements in initialization logic;
This commit is contained in:
parent
9034551502
commit
a96aab33cb
3 changed files with 29 additions and 2 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
|
@ -164,4 +164,7 @@ cypress/screenshots/
|
||||||
.env
|
.env
|
||||||
|
|
||||||
# Development stuff
|
# Development stuff
|
||||||
test.ipynb
|
test.ipynb
|
||||||
|
|
||||||
|
# Production stuff
|
||||||
|
.initialized
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
|
from datetime import datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
@ -130,6 +131,24 @@ class Command(BaseCommand):
|
||||||
def handle(self, *args: list[Any], **options: dict[Any, Any]) -> None:
|
def handle(self, *args: list[Any], **options: dict[Any, Any]) -> None:
|
||||||
self.stdout.write("Initializing must-have instances...")
|
self.stdout.write("Initializing must-have instances...")
|
||||||
|
|
||||||
|
initialized_path = settings.BASE_DIR / ".initialized"
|
||||||
|
|
||||||
|
stored_date: datetime | None = None
|
||||||
|
if initialized_path.exists():
|
||||||
|
try:
|
||||||
|
content = initialized_path.read_text(encoding="utf-8").strip()
|
||||||
|
if content:
|
||||||
|
stored_date = datetime.fromisoformat(content)
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Failed to parse .initialized content: %s", exc)
|
||||||
|
|
||||||
|
if stored_date is None:
|
||||||
|
stored_date = datetime.min
|
||||||
|
|
||||||
|
if not (settings.RELEASE_DATE > stored_date):
|
||||||
|
self.stdout.write(self.style.WARNING("Initialization skipped: already up-to-date."))
|
||||||
|
return
|
||||||
|
|
||||||
Vendor.objects.get_or_create(name="INNER")
|
Vendor.objects.get_or_create(name="INNER")
|
||||||
|
|
||||||
user_support, is_user_support_created = Group.objects.get_or_create(name="User Support")
|
user_support, is_user_support_created = Group.objects.get_or_create(name="User Support")
|
||||||
|
|
@ -160,4 +179,9 @@ class Command(BaseCommand):
|
||||||
valid_codes = [code for code, _ in settings.LANGUAGES]
|
valid_codes = [code for code, _ in settings.LANGUAGES]
|
||||||
(User.objects.filter(Q(language="") | ~Q(language__in=valid_codes)).update(language=settings.LANGUAGE_CODE))
|
(User.objects.filter(Q(language="") | ~Q(language__in=valid_codes)).update(language=settings.LANGUAGE_CODE))
|
||||||
|
|
||||||
|
try:
|
||||||
|
initialized_path.write_text(settings.RELEASE_DATE.isoformat(), encoding="utf-8")
|
||||||
|
except Exception as exc:
|
||||||
|
logger.error("Failed to update .initialized file: %s", exc)
|
||||||
|
|
||||||
self.stdout.write(self.style.SUCCESS("Successfully initialized must-have instances!"))
|
self.stdout.write(self.style.SUCCESS("Successfully initialized must-have instances!"))
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ async def forward_thread_message_to_assigned_staff(thread_uuid: str, text: str)
|
||||||
logger.warning("Failed to forward Telegram message for thread %s: %s", _tid, exc)
|
logger.warning("Failed to forward Telegram message for thread %s: %s", _tid, exc)
|
||||||
|
|
||||||
|
|
||||||
def install_aiohttp_webhook(app) -> None: # pragma: no cover - integration helper
|
def install_aiohttp_webhook(app) -> None:
|
||||||
if not is_telegram_enabled():
|
if not is_telegram_enabled():
|
||||||
logger.warning("Telegram forwarder not installed: disabled")
|
logger.warning("Telegram forwarder not installed: disabled")
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue