video_monitor/app/apps.py
2026-08-30 22:23:12 +08:00

58 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from django.apps import AppConfig
class AppConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'app'
def ready(self):
# SQLite 优化:每个新连接执行 PRAGMA
# - journal_mode=DELETE传统 rollback journal 模式,只产生一个 monitor.sqlite3 文件
# - busy_timeout=5000写冲突等 5s 而非立即报错
# - cache_size=-6553664MB 页缓存
# - temp_store=MEMORY临时表用内存
from django.db.backends.signals import connection_created
def _setup_sqlite_pragma(sender, connection, **kwargs):
if connection.vendor != 'sqlite':
return
with connection.cursor() as cur:
cur.execute('PRAGMA journal_mode=DELETE;')
cur.execute('PRAGMA busy_timeout=5000;')
cur.execute('PRAGMA cache_size=-65536;')
cur.execute('PRAGMA temp_store=MEMORY;')
connection_created.connect(_setup_sqlite_pragma)
try:
from app.utils.schema_upgrade import ensure_biz_algorithm_line_count_columns
ensure_biz_algorithm_line_count_columns()
except Exception as e:
import logging
logging.getLogger("app.bootstrap").warning("schema upgrade: %s", e)
try:
from django.contrib.auth.models import Group
for role in ("system_admin", "algorithm_admin", "operator", "viewer"):
Group.objects.get_or_create(name=role)
from app.utils.Credentials import migrate_existing_credentials
changed = migrate_existing_credentials()
if changed:
import logging
logging.getLogger("app.bootstrap").info("encrypted credentials in %d database rows", changed)
except Exception as e:
import logging
logging.getLogger("app.bootstrap").warning("security data upgrade: %s", e)
mode = os.environ.get("MONITOR_SERVICE_MODE", "disabled").strip().lower()
# Backward-compatible opt-in, still protected by the cross-process lock.
if mode == "disabled" and os.environ.get("MONITOR_BOOTSTRAP_SERVICES", "").lower() in (
"1", "true", "yes", "on"
):
mode = "embedded"
if mode == "embedded":
from app.services.lifecycle import get_service_manager
get_service_manager().start()