2026-08-30 22:22:11 +08:00
|
|
|
|
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=-65536:64MB 页缓存
|
|
|
|
|
|
# - 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)
|
|
|
|
|
|
|
2026-09-04 18:16:14 +08:00
|
|
|
|
from monitor_runtime.paths import DESKTOP
|
|
|
|
|
|
if DESKTOP:
|
|
|
|
|
|
return # Desktop bootstrap migrates before accessing business tables.
|
|
|
|
|
|
|
2026-08-30 22:22:11 +08:00
|
|
|
|
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()
|