automatyzacje-comit4

This commit is contained in:
Mateusz Gruszczyński
2026-05-07 09:06:47 +02:00
parent e99d19ece0
commit 7a4bda98a2
2 changed files with 118 additions and 31 deletions

View File

@@ -54,25 +54,33 @@ def _job_row(job_id: str):
return conn.execute("SELECT rowid AS _rowid, * FROM jobs WHERE id=?", (job_id,)).fetchone()
def _is_ordered_action(action_name: str) -> bool:
return action_name in {"move", "remove"}
def _job_payload(row) -> dict:
try:
return json.loads((row or {}).get("payload_json") or "{}")
except Exception:
return {}
def _is_ordered_job(row) -> bool:
payload = _job_payload(row)
# Note: Move/remove remain ordered, and automation-created jobs can opt in so effect order is visible and predictable.
return str((row or {}).get("action") or "") in {"move", "remove"} or bool(payload.get("automation_ordered"))
def _has_prior_ordered_jobs(profile_id: int, rowid: int) -> bool:
with connect() as conn:
row = conn.execute(
rows = conn.execute(
"""
SELECT 1
SELECT rowid AS _rowid, action, payload_json
FROM jobs
WHERE profile_id=?
AND rowid<?
AND action IN ('move', 'remove')
AND status IN ('pending', 'running')
LIMIT 1
ORDER BY rowid
""",
(profile_id, rowid),
).fetchone()
return bool(row)
).fetchall()
return any(_is_ordered_job(row) for row in rows)
def _wait_for_prior_ordered_jobs(job_id: str, profile_id: int, rowid: int) -> bool:
@@ -140,7 +148,7 @@ def _run(job_id: str):
return
profile_id = int(profile["id"])
ordered_lock = None
if _is_ordered_action(str(job["action"])):
if _is_ordered_job(job):
if not _wait_for_prior_ordered_jobs(job_id, profile_id, int(job["_rowid"])):
return
ordered_lock = _get_exclusive_lock(profile_id)