astrbot_plugin_imagestudio/database/repositories/job_repository.py

402 行
13 KiB
Python

"""Repository for generation tasks and queue status."""
from __future__ import annotations
import json
from typing import Any
from backend.dto.job_dto import GenerationTask
from database.db_helper import Database
TASK_COLUMNS = """
task_id,
profile_id,
workflow_id,
node_id,
task_type,
status,
model_route,
progress,
idempotency_key,
request_json,
result_json,
error_message,
metadata_json,
created_at,
updated_at
"""
class JobRepository:
def __init__(self, database: Database):
self.database = database
@property
def placeholder(self) -> str:
return self.database._placeholder
def create(self, payload: dict[str, Any]) -> GenerationTask:
placeholder = self.placeholder
conn = self.database.connect()
try:
conn.execute(
f"""
INSERT INTO generation_tasks (
task_id,
profile_id,
workflow_id,
node_id,
task_type,
status,
model_route,
progress,
idempotency_key,
request_json,
result_json,
error_message,
metadata_json
) VALUES (
{placeholder}, {placeholder}, {placeholder}, {placeholder},
{placeholder}, {placeholder}, {placeholder}, {placeholder},
{placeholder}, {placeholder}, {placeholder}, {placeholder},
{placeholder}
)
""",
(
payload["task_id"],
payload.get("profile_id"),
payload.get("workflow_id"),
payload.get("node_id"),
payload.get("task_type", "generation"),
payload.get("status", "created"),
payload.get("model_route"),
int(payload.get("progress", 0)),
payload.get("idempotency_key"),
_json_or_none(payload.get("request")),
_json_or_none(payload.get("result")),
payload.get("error_message"),
_json_or_none(payload.get("metadata")),
),
)
conn.commit()
finally:
conn.close()
task = self.get(payload["task_id"])
if task is None:
raise RuntimeError("generation task was inserted but could not be read back")
return task
def get(self, task_id: str) -> GenerationTask | None:
placeholder = self.placeholder
conn = self.database.connect()
try:
row = conn.execute(
f"SELECT {TASK_COLUMNS} FROM generation_tasks WHERE task_id = {placeholder}",
(task_id,),
).fetchone()
finally:
conn.close()
return _row_to_task(row) if row else None
def get_by_idempotency_key(self, idempotency_key: str) -> GenerationTask | None:
placeholder = self.placeholder
conn = self.database.connect()
try:
row = conn.execute(
f"SELECT {TASK_COLUMNS} FROM generation_tasks WHERE idempotency_key = {placeholder}",
(idempotency_key,),
).fetchone()
finally:
conn.close()
return _row_to_task(row) if row else None
def list(
self,
*,
status: str | None = None,
workflow_id: str | None = None,
profile_id: str | None = None,
page: int = 1,
page_size: int = 30,
) -> tuple[list[GenerationTask], int]:
placeholder = self.placeholder
where: list[str] = []
params: list[Any] = []
if status:
where.append(f"status = {placeholder}")
params.append(status)
if workflow_id:
where.append(f"workflow_id = {placeholder}")
params.append(workflow_id)
if profile_id:
where.append(f"profile_id = {placeholder}")
params.append(profile_id)
where_clause = f"WHERE {' AND '.join(where)}" if where else ""
normalized_page = _page(page)
normalized_page_size = _page_size(page_size)
offset = (normalized_page - 1) * normalized_page_size
conn = self.database.connect()
try:
total_row = conn.execute(
f"SELECT COUNT(*) AS total FROM generation_tasks {where_clause}",
tuple(params),
).fetchone()
rows = conn.execute(
f"""
SELECT {TASK_COLUMNS}
FROM generation_tasks
{where_clause}
ORDER BY created_at DESC
LIMIT {placeholder} OFFSET {placeholder}
""",
tuple(params + [normalized_page_size, offset]),
).fetchall()
finally:
conn.close()
return [_row_to_task(row) for row in rows], int(total_row["total"] if total_row else 0)
def update(self, task_id: str, payload: dict[str, Any]) -> GenerationTask | None:
placeholder = self.placeholder
assignments, params = _update_assignments(payload, placeholder)
if not assignments:
return self.get(task_id)
assignments.append("updated_at = CURRENT_TIMESTAMP")
params.append(task_id)
conn = self.database.connect()
try:
conn.execute(
f"""
UPDATE generation_tasks
SET {", ".join(assignments)}
WHERE task_id = {placeholder}
""",
tuple(params),
)
conn.commit()
finally:
conn.close()
return self.get(task_id)
def update_if_status(
self,
task_id: str,
expected_statuses: set[str] | tuple[str, ...] | list[str],
payload: dict[str, Any],
) -> GenerationTask | None:
"""Update a task only while it is still in an expected state.
The status predicate and mutation run in one SQL statement. This is the
compare-and-set primitive used by retry orchestration and queue workers
to prevent two callers from claiming the same task.
"""
normalized_statuses = tuple(
sorted({str(status).strip() for status in expected_statuses if str(status).strip()})
)
if not normalized_statuses:
return None
placeholder = self.placeholder
assignments, params = _update_assignments(payload, placeholder)
if not assignments:
task = self.get(task_id)
return task if task and task.status in normalized_statuses else None
assignments.append("updated_at = CURRENT_TIMESTAMP")
status_placeholders = ", ".join(placeholder for _ in normalized_statuses)
params.extend([task_id, *normalized_statuses])
conn = self.database.connect()
try:
cursor = conn.execute(
f"""
UPDATE generation_tasks
SET {", ".join(assignments)}
WHERE task_id = {placeholder}
AND status IN ({status_placeholders})
""",
tuple(params),
)
changed = int(cursor.rowcount or 0) > 0
conn.commit()
finally:
conn.close()
return self.get(task_id) if changed else None
def update_if_status_unless_workflow_cancelled(
self,
task_id: str,
expected_statuses: set[str] | tuple[str, ...] | list[str],
payload: dict[str, Any],
) -> GenerationTask | None:
"""CAS-update a task while its owning workflow is not cancelled."""
normalized_statuses = tuple(
sorted({str(status).strip() for status in expected_statuses if str(status).strip()})
)
if not normalized_statuses:
return None
placeholder = self.placeholder
assignments, params = _update_assignments(payload, placeholder)
if not assignments:
task = self.get(task_id)
if task is None or task.status not in normalized_statuses:
return None
if task.workflow_id:
conn = self.database.connect()
try:
row = conn.execute(
f"SELECT status FROM profile_workflows WHERE workflow_id = {placeholder}",
(task.workflow_id,),
).fetchone()
finally:
conn.close()
if row is None or str(row["status"]) == "cancelled":
return None
return task
assignments.append("updated_at = CURRENT_TIMESTAMP")
status_placeholders = ", ".join(placeholder for _ in normalized_statuses)
params.extend([task_id, *normalized_statuses, "cancelled"])
conn = self.database.connect()
try:
cursor = conn.execute(
f"""
UPDATE generation_tasks
SET {", ".join(assignments)}
WHERE task_id = {placeholder}
AND status IN ({status_placeholders})
AND (
workflow_id IS NULL
OR EXISTS (
SELECT 1
FROM profile_workflows
WHERE profile_workflows.workflow_id = generation_tasks.workflow_id
AND profile_workflows.status <> {placeholder}
)
)
""",
tuple(params),
)
changed = int(cursor.rowcount or 0) > 0
conn.commit()
finally:
conn.close()
return self.get(task_id) if changed else None
def count_by_status(self) -> dict[str, int]:
conn = self.database.connect()
try:
rows = conn.execute(
"""
SELECT status, COUNT(*) AS total
FROM generation_tasks
GROUP BY status
"""
).fetchall()
finally:
conn.close()
return {row["status"]: int(row["total"]) for row in rows}
def recent(self, limit: int = 10) -> list[GenerationTask]:
placeholder = self.placeholder
conn = self.database.connect()
try:
rows = conn.execute(
f"""
SELECT {TASK_COLUMNS}
FROM generation_tasks
ORDER BY created_at DESC
LIMIT {placeholder}
""",
(_limit(limit),),
).fetchall()
finally:
conn.close()
return [_row_to_task(row) for row in rows]
def _row_to_task(row) -> GenerationTask:
return GenerationTask(
task_id=row["task_id"],
profile_id=row["profile_id"],
workflow_id=row["workflow_id"],
node_id=row["node_id"],
task_type=row["task_type"],
status=row["status"],
model_route=row["model_route"],
progress=int(row["progress"] or 0),
idempotency_key=row["idempotency_key"],
request=_json_loads(row["request_json"]),
result=_json_loads(row["result_json"]),
error_message=row["error_message"],
metadata=_json_loads(row["metadata_json"]),
created_at=str(row["created_at"]),
updated_at=str(row["updated_at"]),
)
def _json_or_none(value: Any | None) -> str | None:
if value is None:
return None
return json.dumps(value, ensure_ascii=False, sort_keys=True)
def _update_assignments(payload: dict[str, Any], placeholder: str) -> tuple[list[str], list[Any]]:
allowed = {
"status",
"model_route",
"progress",
"request",
"result",
"error_message",
"metadata",
}
assignments: list[str] = []
params: list[Any] = []
for key in allowed:
if key not in payload:
continue
column = key
value = payload[key]
if key == "request":
column = "request_json"
value = _json_or_none(value)
elif key == "result":
column = "result_json"
value = _json_or_none(value)
elif key == "metadata":
column = "metadata_json"
value = _json_or_none(value)
elif key == "progress":
value = int(value)
assignments.append(f"{column} = {placeholder}")
params.append(value)
return assignments, params
def _limit(value: int) -> int:
try:
limit = int(value)
except (TypeError, ValueError):
limit = 10
return min(max(limit, 1), 100)
def _page(value: Any) -> int:
try:
page = int(value)
except (TypeError, ValueError):
page = 1
return max(page, 1)
def _page_size(value: Any) -> int:
try:
page_size = int(value)
except (TypeError, ValueError):
page_size = 30
return min(max(page_size, 1), 100)
def _json_loads(value: str | None) -> dict[str, Any]:
if not value:
return {}
return json.loads(value)