added jobs handling.
This commit is contained in:
+115
@@ -56,6 +56,121 @@ def create_job(
|
||||
return job_id
|
||||
|
||||
|
||||
def retry_failed_job(job_id: int, user: dict[str, Any]):
|
||||
run_migrations()
|
||||
created_by_user_id = user.get("id") if user else None
|
||||
created_by_username = user.get("username") if user else None
|
||||
created_by_display_name = user.get("display_name") if user else None
|
||||
|
||||
con = get_connection()
|
||||
source_job = con.execute(
|
||||
"""
|
||||
SELECT id
|
||||
FROM jobs
|
||||
WHERE id = ?
|
||||
AND status = 'failed'
|
||||
""",
|
||||
(job_id,),
|
||||
).fetchone()
|
||||
if not source_job:
|
||||
con.close()
|
||||
return None
|
||||
|
||||
cur = con.execute(
|
||||
"""
|
||||
INSERT INTO jobs (
|
||||
type,
|
||||
target_type,
|
||||
target_id,
|
||||
payload_json,
|
||||
status,
|
||||
created_by_user_id,
|
||||
created_by_username,
|
||||
created_by_display_name,
|
||||
source
|
||||
)
|
||||
SELECT
|
||||
type,
|
||||
target_type,
|
||||
target_id,
|
||||
payload_json,
|
||||
'queued',
|
||||
?,
|
||||
?,
|
||||
?,
|
||||
'portal_retry'
|
||||
FROM jobs
|
||||
WHERE id = ?
|
||||
AND status = 'failed'
|
||||
""",
|
||||
(created_by_user_id, created_by_username, created_by_display_name, job_id),
|
||||
)
|
||||
if cur.rowcount != 1:
|
||||
con.close()
|
||||
return None
|
||||
|
||||
new_job_id = cur.lastrowid
|
||||
con.commit()
|
||||
con.close()
|
||||
|
||||
append_job_log(new_job_id, "system", f"Job queued as retry of #{job_id}.")
|
||||
return new_job_id
|
||||
|
||||
|
||||
def cancel_job(job_id: int):
|
||||
run_migrations()
|
||||
con = get_connection()
|
||||
|
||||
row = con.execute(
|
||||
"""
|
||||
SELECT status
|
||||
FROM jobs
|
||||
WHERE id = ?
|
||||
""",
|
||||
(job_id,),
|
||||
).fetchone()
|
||||
if not row:
|
||||
con.close()
|
||||
return None
|
||||
|
||||
status = (row["status"] or "").lower()
|
||||
if status == "queued":
|
||||
new_status = "cancelled"
|
||||
cur = con.execute(
|
||||
"""
|
||||
UPDATE jobs
|
||||
SET status = 'cancelled',
|
||||
finished_at = CURRENT_TIMESTAMP
|
||||
WHERE id = ?
|
||||
AND status = 'queued'
|
||||
""",
|
||||
(job_id,),
|
||||
)
|
||||
elif status == "running":
|
||||
new_status = "cancelled_requested"
|
||||
cur = con.execute(
|
||||
"""
|
||||
UPDATE jobs
|
||||
SET status = 'cancelled_requested'
|
||||
WHERE id = ?
|
||||
AND status = 'running'
|
||||
""",
|
||||
(job_id,),
|
||||
)
|
||||
else:
|
||||
con.close()
|
||||
return None
|
||||
|
||||
if cur.rowcount != 1:
|
||||
con.close()
|
||||
return None
|
||||
|
||||
con.commit()
|
||||
con.close()
|
||||
append_job_log(job_id, "system", f"Job status changed from {status} to {new_status}.")
|
||||
return {"previous_status": status, "status": new_status}
|
||||
|
||||
|
||||
def update_job_status(
|
||||
job_id: int,
|
||||
status: str,
|
||||
|
||||
Reference in New Issue
Block a user