urgent fixes2

This commit is contained in:
Mateusz Gruszczyński
2026-05-25 22:32:36 +02:00
parent 0ee0f3424c
commit 81d9556443
3 changed files with 14 additions and 7 deletions

View File

@@ -443,11 +443,13 @@ def evaluate(profile: dict, settings: dict | None = None, now: datetime | None =
} }
def enforce(profile: dict, force: bool = False) -> dict: def enforce(profile: dict, force: bool = False, user_id: int | None = None) -> dict:
profile_id = int(profile.get("id") or 0) profile_id = int(profile.get("id") or 0)
settings = get_settings(profile_id) user_id = user_id or int(profile.get("user_id") or default_user_id())
# Note: Background planner runs without Flask session state, so settings are resolved with the profile owner.
settings = get_settings(profile_id, user_id)
if not settings.get("enabled"): if not settings.get("enabled"):
return {"ok": True, "enabled": False, "profile_id": profile_id, "history": history(profile_id, 20), "history_total": history_count(profile_id), "preview": preview(profile)} return {"ok": True, "enabled": False, "profile_id": profile_id, "history": history(profile_id, 20), "history_total": history_count(profile_id), "preview": preview(profile, user_id=user_id)}
now = time.monotonic() now = time.monotonic()
interval = int(settings.get("check_interval_seconds") or 30) interval = int(settings.get("check_interval_seconds") or 30)
if not force and now - _LAST_RUN.get(profile_id, 0) < interval: if not force and now - _LAST_RUN.get(profile_id, 0) < interval:
@@ -497,13 +499,14 @@ def enforce(profile: dict, force: bool = False) -> dict:
_append_history(profile_id, "resumed_torrents", {"count": len(hashes), "dry_run": dry_run}) _append_history(profile_id, "resumed_torrents", {"count": len(hashes), "dry_run": dry_run})
result["history"] = history(profile_id, 20) result["history"] = history(profile_id, 20)
result["history_total"] = history_count(profile_id) result["history_total"] = history_count(profile_id)
result["preview"] = preview(profile) result["preview"] = preview(profile, user_id=user_id)
return result return result
def preview(profile: dict) -> dict: def preview(profile: dict, user_id: int | None = None) -> dict:
profile_id = int(profile.get("id") or 0) profile_id = int(profile.get("id") or 0)
settings = get_settings(profile_id) user_id = user_id or int(profile.get("user_id") or default_user_id())
settings = get_settings(profile_id, user_id)
decision = evaluate(profile, settings) decision = evaluate(profile, settings)
return { return {
"profile_id": profile_id, "profile_id": profile_id,

View File

@@ -64,7 +64,7 @@ def _run_slow_profile_tasks(socketio, profile: dict, profile_id: int) -> None:
except Exception as exc: except Exception as exc:
_emit_profile(socketio, "automation_update", {"ok": False, "profile_id": profile_id, "error": str(exc)}, profile_id) _emit_profile(socketio, "automation_update", {"ok": False, "profile_id": profile_id, "error": str(exc)}, profile_id)
try: try:
plan_result = download_planner.enforce(profile, force=False) plan_result = download_planner.enforce(profile, force=False, user_id=profile_user_id)
if plan_result.get("enabled") and not plan_result.get("skipped"): if plan_result.get("enabled") and not plan_result.get("skipped"):
_emit_profile(socketio, "download_plan_update", plan_result, profile_id) _emit_profile(socketio, "download_plan_update", plan_result, profile_id)
except Exception as exc: except Exception as exc:

View File

@@ -292,6 +292,8 @@ def _run(job_id: str):
return return
sem = None sem = None
ordered_lock = None ordered_lock = None
job = {}
payload = {}
try: try:
job = _job_row(job_id) job = _job_row(job_id)
if not job or job["status"] == "cancelled": if not job or job["status"] == "cancelled":
@@ -330,6 +332,8 @@ def _run(job_id: str):
_set_job(job_id, "done", result=result, finished=True) _set_job(job_id, "done", result=result, finished=True)
operation_logs.record_job_event(profile["id"], job["action"], "done", payload, result=result or {}, job_id=job_id, user_id=int(job.get("user_id") or 0)) operation_logs.record_job_event(profile["id"], job["action"], "done", payload, result=result or {}, job_id=job_id, user_id=int(job.get("user_id") or 0))
_emit("operation_finished", {"job_id": job_id, "action": job["action"], "profile_id": profile["id"], "hashes": payload.get("hashes") or [], "hash_count": len(payload.get("hashes") or []), "bulk": len(payload.get("hashes") or []) > 1, "result": result, **event_meta}) _emit("operation_finished", {"job_id": job_id, "action": job["action"], "profile_id": profile["id"], "hashes": payload.get("hashes") or [], "hash_count": len(payload.get("hashes") or []), "bulk": len(payload.get("hashes") or []) > 1, "result": result, **event_meta})
# Note: Completed jobs must publish a fresh torrent snapshot/patch so removed or moved torrents disappear without a page reload.
_emit_torrent_refresh(profile, str(job["action"] or ""))
_emit("job_update", {"id": job_id, "profile_id": profile["id"], "status": "done", "result": result}) _emit("job_update", {"id": job_id, "profile_id": profile["id"], "status": "done", "result": result})
except Exception as exc: except Exception as exc:
fresh = _job_row(job_id) or {} fresh = _job_row(job_id) or {}