feat: portable Windows data layout and launcher package
Separate persistent user data from application code (data/work_tracker.db, data/uploads, data/storage) with idempotent legacy migration and SQLite integrity checks. Add offline packaging scripts for a Windows x11 x64 portable ZIP (embeddable Python, Start/Stop/Backup.bat). Fix launcher: no multiline python -c in BAT and no os.dup2 — uvicorn runs detached with wvt.log; health wait and busy-port diagnostics included.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
"""Run Work Volume Tracker uvicorn in this process.
|
||||
|
||||
Started from Start.bat via `start /b python run_server.py`.
|
||||
|
||||
Stdout/stderr are redirected by replacing Python stream objects only.
|
||||
Do NOT call os.dup2: under `start /b` the inherited Windows stderr HANDLE
|
||||
may already be invalid, and dup2 then causes
|
||||
OSError(9, 'Неверный дескриптор') and 'lost sys.stderr'.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def _open_log(log_path: Path):
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
return open(log_path, "a", encoding="utf-8", buffering=1)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8000
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
log_path = root / "data" / "logs" / "wvt.log"
|
||||
crash_path = root / "data" / "logs" / "wvt-crash.log"
|
||||
pid_path = root / "data" / "wvt.pid"
|
||||
|
||||
log_f = _open_log(log_path)
|
||||
# Replace Python-level streams (not OS file descriptors).
|
||||
sys.stdout = log_f
|
||||
sys.stderr = log_f
|
||||
sys.__stdout__ = log_f
|
||||
sys.__stderr__ = log_f
|
||||
try:
|
||||
sys.stdin = open(os.devnull, "r", encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
print(f"run_server pid={os.getpid()} port={port} root={root}", flush=True)
|
||||
try:
|
||||
pid_path.write_text(str(os.getpid()) + "\n", encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
os.chdir(str(root))
|
||||
|
||||
try:
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run("app.main:app", host="127.0.0.1", port=port, log_level="info")
|
||||
print("run_server: uvicorn returned", flush=True)
|
||||
return 0
|
||||
except Exception:
|
||||
text = traceback.format_exc()
|
||||
for writer in (
|
||||
lambda: crash_path.write_text(text, encoding="utf-8"),
|
||||
lambda: print(text, flush=True),
|
||||
lambda: (log_f.write(text), log_f.flush()),
|
||||
):
|
||||
try:
|
||||
writer()
|
||||
except Exception:
|
||||
pass
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
raise SystemExit(main())
|
||||
except SystemExit:
|
||||
raise
|
||||
except Exception:
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
try:
|
||||
crash = root / "data" / "logs" / "wvt-crash.log"
|
||||
crash.parent.mkdir(parents=True, exist_ok=True)
|
||||
crash.write_text(traceback.format_exc(), encoding="utf-8")
|
||||
except Exception:
|
||||
pass
|
||||
raise
|
||||
Reference in New Issue
Block a user