feat: add settings tabs, auto backup, and show latest 5 backups

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-02 09:15:21 +08:00
parent 52ebbfbeae
commit 458cc42dd5
12 changed files with 621 additions and 190 deletions
+30 -3
View File
@@ -1,4 +1,4 @@
"""Worker 入口:周期回填到期结算指数。"""
"""Worker 入口:周期回填到期结算指数 + 自动备份"""
from __future__ import annotations
@@ -10,8 +10,9 @@ from typing import Any
from apps.collector.okx_rest import OkxRestClient
from apps.worker.settle import backfill_settlements
from packages.config import get_settings
from packages.config import get_settings, reload_settings
from packages.db import Repository
from packages.db.backup import run_auto_backup, should_auto_backup
logging.basicConfig(
level=logging.INFO,
@@ -29,13 +30,35 @@ def _handle_signal(signum: int, _frame: Any) -> None:
_STOP = True
def _maybe_auto_backup() -> None:
s = reload_settings()
if not should_auto_backup(
s.backup_dir_path,
enabled=s.backup_auto_enabled,
interval_hours=s.backup_interval_hours,
):
return
try:
dest = run_auto_backup(
s.db_path,
s.backup_dir_path,
tz=s.tz,
keep=s.backup_keep_count,
)
log.info("auto backup ok: %s", dest.name)
except Exception as e: # noqa: BLE001
log.exception("auto backup failed: %s", e)
def run() -> int:
settings = get_settings()
interval = max(60, int(settings.settle_backfill_interval_sec))
log.info(
"start settle backfill interval=%ss db=%s",
"start settle backfill interval=%ss db=%s backup_auto=%s every=%sh",
interval,
settings.db_path,
settings.backup_auto_enabled,
settings.backup_interval_hours,
)
repo = Repository(settings.db_path)
client = OkxRestClient(
@@ -43,6 +66,8 @@ def run() -> int:
proxy=settings.okx_proxy or None,
)
try:
# 启动时先尝试一次自动备份(若到期)
_maybe_auto_backup()
while not _STOP:
try:
result = backfill_settlements(
@@ -62,6 +87,8 @@ def run() -> int:
except Exception as e: # noqa: BLE001
log.exception("backfill loop failed: %s", e)
_maybe_auto_backup()
end = time.monotonic() + interval
while not _STOP and time.monotonic() < end:
time.sleep(min(1.0, end - time.monotonic()))