1259 lines
44 KiB
Python
1259 lines
44 KiB
Python
"""PostgreSQL: пользователи, чаты, участники, сообщения, идемпотентность."""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import os
|
|
import time
|
|
import uuid
|
|
from contextlib import contextmanager
|
|
from datetime import datetime, timezone
|
|
from pathlib import Path
|
|
|
|
import psycopg
|
|
from psycopg.errors import UniqueViolation
|
|
from psycopg_pool import ConnectionPool, PoolTimeout
|
|
|
|
from errors import ApiError
|
|
|
|
_pool: ConnectionPool | None = None
|
|
|
|
|
|
def migrations_dir() -> Path:
|
|
here = Path(__file__).resolve().parent
|
|
for candidate in (here / "migrations", here.parent / "migrations"):
|
|
if candidate.is_dir():
|
|
return candidate
|
|
return here.parent / "migrations"
|
|
|
|
|
|
def database_url() -> str:
|
|
path = os.environ.get("DATABASE_URL_FILE", "").strip()
|
|
if path and Path(path).is_file():
|
|
return Path(path).read_text(encoding="utf-8").strip()
|
|
return os.environ.get("DATABASE_URL", "").strip()
|
|
|
|
|
|
def idempotency_ttl() -> int:
|
|
raw = os.environ.get("IDEMPOTENCY_TTL_SECONDS", "86400")
|
|
try:
|
|
value = int(raw)
|
|
except ValueError:
|
|
return 86400
|
|
return value if value >= 1 else 86400
|
|
|
|
|
|
def init() -> None:
|
|
global _pool
|
|
url = database_url()
|
|
if not url:
|
|
raise RuntimeError("database is not configured")
|
|
_pool = ConnectionPool(
|
|
url,
|
|
min_size=0,
|
|
max_size=12,
|
|
timeout=3,
|
|
open=True,
|
|
kwargs={
|
|
"connect_timeout": 3,
|
|
"autocommit": False,
|
|
"options": "-c statement_timeout=5000",
|
|
},
|
|
)
|
|
|
|
|
|
def migrate_with_retry() -> None:
|
|
last = None
|
|
for _ in range(30):
|
|
try:
|
|
migrate()
|
|
return
|
|
except Exception as exc:
|
|
last = exc
|
|
time.sleep(1)
|
|
raise RuntimeError("migrations failed") from last
|
|
|
|
|
|
def migrate() -> None:
|
|
url = database_url()
|
|
if not url:
|
|
raise RuntimeError("database is not configured")
|
|
with psycopg.connect(url, connect_timeout=3, autocommit=False) as conn:
|
|
conn.execute("SELECT pg_advisory_lock(%s)", (842021,))
|
|
conn.execute(
|
|
"""
|
|
CREATE TABLE IF NOT EXISTS schema_migrations (
|
|
version text PRIMARY KEY,
|
|
applied_at timestamptz NOT NULL DEFAULT now()
|
|
)
|
|
"""
|
|
)
|
|
conn.commit()
|
|
applied = {
|
|
row[0]
|
|
for row in conn.execute("SELECT version FROM schema_migrations").fetchall()
|
|
}
|
|
for path in sorted(migrations_dir().glob("*.sql")):
|
|
if path.name in applied:
|
|
continue
|
|
sql = path.read_text(encoding="utf-8")
|
|
with conn.transaction():
|
|
for statement in sql.split(";"):
|
|
if statement.strip():
|
|
conn.execute(statement)
|
|
conn.execute(
|
|
"INSERT INTO schema_migrations (version) VALUES (%s)",
|
|
(path.name,),
|
|
)
|
|
|
|
|
|
@contextmanager
|
|
def connection():
|
|
if _pool is None:
|
|
raise ApiError(503, "unavailable", "База данных недоступна")
|
|
try:
|
|
with _pool.connection() as conn:
|
|
yield conn
|
|
except ApiError:
|
|
raise
|
|
except (PoolTimeout, psycopg.OperationalError, psycopg.InterfaceError) as exc:
|
|
raise ApiError(503, "unavailable", "База данных недоступна") from exc
|
|
|
|
|
|
def ping() -> bool:
|
|
try:
|
|
with connection() as conn:
|
|
conn.execute("SELECT 1")
|
|
return True
|
|
except ApiError:
|
|
return False
|
|
|
|
|
|
def close() -> None:
|
|
if _pool is not None:
|
|
_pool.close()
|
|
|
|
|
|
def user_json(row) -> dict:
|
|
return {
|
|
"id": str(row[0]),
|
|
"username": row[1],
|
|
"display_name": row[2],
|
|
"created_at": format_ts(row[3]),
|
|
}
|
|
|
|
|
|
def format_ts(value: datetime) -> str:
|
|
value = value.astimezone(timezone.utc)
|
|
return value.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
|
|
|
|
|
|
def create_user(username: str, display_name: str, password_hash: str, created: datetime) -> dict:
|
|
uid = uuid.uuid4()
|
|
try:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
row = conn.execute(
|
|
"""
|
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
RETURNING id, username, display_name, created_at
|
|
""",
|
|
(uid, username, display_name, password_hash, created),
|
|
).fetchone()
|
|
except UniqueViolation as exc:
|
|
raise ApiError(409, "conflict", "username занят") from exc
|
|
return user_json(row)
|
|
|
|
|
|
def user_for_login(username: str):
|
|
with connection() as conn:
|
|
return conn.execute(
|
|
"""
|
|
SELECT id, username, display_name, password_hash, created_at
|
|
FROM users
|
|
WHERE username = %s
|
|
""",
|
|
(username,),
|
|
).fetchone()
|
|
|
|
|
|
def get_user(user_id: str) -> dict | None:
|
|
with connection() as conn:
|
|
row = conn.execute(
|
|
"""
|
|
SELECT id, username, display_name, created_at
|
|
FROM users
|
|
WHERE id = %s
|
|
""",
|
|
(uuid.UUID(user_id),),
|
|
).fetchone()
|
|
if row is None:
|
|
return None
|
|
return user_json(row)
|
|
|
|
|
|
def create_chat(title: str, member_ids: list[str], created: datetime) -> dict:
|
|
cid = uuid.uuid4()
|
|
members = [uuid.UUID(item) for item in member_ids]
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
conn.execute(
|
|
"INSERT INTO chats (id, title, created_at) VALUES (%s, %s, %s)",
|
|
(cid, title, created),
|
|
)
|
|
found = conn.execute(
|
|
"SELECT id FROM users WHERE id = ANY(%s::uuid[])",
|
|
(members,),
|
|
).fetchall()
|
|
if {row[0] for row in found} != set(members):
|
|
raise ApiError(400, "invalid_request", "неизвестный участник")
|
|
for position, member in enumerate(members):
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO chat_members (chat_id, user_id, position)
|
|
VALUES (%s, %s, %s)
|
|
""",
|
|
(cid, member, position),
|
|
)
|
|
return {
|
|
"id": str(cid),
|
|
"title": title,
|
|
"member_ids": list(member_ids),
|
|
"created_at": format_ts(created),
|
|
}
|
|
|
|
|
|
def _chat_row(row) -> dict:
|
|
return {
|
|
"id": str(row[0]),
|
|
"title": row[1],
|
|
"created_at": format_ts(row[2]),
|
|
"member_ids": [str(item) for item in row[3]],
|
|
}
|
|
|
|
|
|
def get_chat(chat_id: str, user_id: str) -> dict:
|
|
with connection() as conn:
|
|
row = conn.execute(
|
|
"""
|
|
SELECT c.id, c.title, c.created_at,
|
|
array_agg(cm.user_id ORDER BY cm.position)
|
|
FROM chats c
|
|
JOIN chat_members mine
|
|
ON mine.chat_id = c.id AND mine.user_id = %s
|
|
JOIN chat_members cm ON cm.chat_id = c.id
|
|
WHERE c.id = %s
|
|
GROUP BY c.id
|
|
""",
|
|
(uuid.UUID(user_id), uuid.UUID(chat_id)),
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "not_found", "Чат не найден")
|
|
chat = _chat_row(row)
|
|
return {
|
|
"id": chat["id"],
|
|
"title": chat["title"],
|
|
"member_ids": chat["member_ids"],
|
|
"created_at": chat["created_at"],
|
|
}
|
|
|
|
|
|
def list_chats(user_id: str, limit: int, cursor) -> dict:
|
|
params: list = [uuid.UUID(user_id)]
|
|
where = ""
|
|
if cursor is not None:
|
|
created, chat_id = cursor
|
|
where = "WHERE (c.created_at, c.id) > (%s, %s)"
|
|
params.extend([created, uuid.UUID(chat_id)])
|
|
params.append(limit + 1)
|
|
sql = f"""
|
|
SELECT c.id, c.title, c.created_at,
|
|
array_agg(cm.user_id ORDER BY cm.position)
|
|
FROM chats c
|
|
JOIN chat_members mine
|
|
ON mine.chat_id = c.id AND mine.user_id = %s
|
|
JOIN chat_members cm ON cm.chat_id = c.id
|
|
{where}
|
|
GROUP BY c.id
|
|
ORDER BY c.created_at, c.id
|
|
LIMIT %s
|
|
"""
|
|
with connection() as conn:
|
|
rows = conn.execute(sql, params).fetchall()
|
|
page = rows[:limit]
|
|
next_cursor = None
|
|
if len(rows) > limit and page:
|
|
last = page[-1]
|
|
next_cursor = (last[2], str(last[0]))
|
|
return {
|
|
"items": [
|
|
{
|
|
"id": str(row[0]),
|
|
"title": row[1],
|
|
"member_ids": [str(item) for item in row[3]],
|
|
"created_at": format_ts(row[2]),
|
|
}
|
|
for row in page
|
|
],
|
|
"next_cursor_parts": next_cursor,
|
|
}
|
|
|
|
|
|
def _lock_key(scope: str) -> int:
|
|
digest = hashlib.sha256(scope.encode("utf-8")).digest()
|
|
return int.from_bytes(digest[:8], "big", signed=True)
|
|
|
|
|
|
def _is_member(conn, chat_id: str, user_id: str) -> bool:
|
|
row = conn.execute(
|
|
"""
|
|
SELECT 1 FROM chat_members
|
|
WHERE chat_id = %s AND user_id = %s
|
|
""",
|
|
(uuid.UUID(chat_id), uuid.UUID(user_id)),
|
|
).fetchone()
|
|
return row is not None
|
|
|
|
|
|
def _attachment_ids(value) -> list:
|
|
if not value:
|
|
return []
|
|
return [str(item) for item in value]
|
|
|
|
|
|
def _load_message(conn, message_id) -> dict:
|
|
row = conn.execute(
|
|
"""
|
|
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
|
COALESCE(
|
|
array_agg(ma.attachment_id ORDER BY ma.position)
|
|
FILTER (WHERE ma.attachment_id IS NOT NULL),
|
|
'{}'::uuid[]
|
|
)
|
|
FROM messages m
|
|
LEFT JOIN message_attachments ma ON ma.message_id = m.id
|
|
WHERE m.id = %s
|
|
GROUP BY m.id
|
|
""",
|
|
(message_id,),
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(500, "internal_error", "Внутренняя ошибка")
|
|
return message_json(row)
|
|
|
|
|
|
def _require_chat_attachments(conn, chat_id: str, attachment_ids: list[str]) -> None:
|
|
if not attachment_ids:
|
|
return
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id::text, chat_id::text, status
|
|
FROM attachments
|
|
WHERE id = ANY(%s::uuid[])
|
|
""",
|
|
(attachment_ids,),
|
|
).fetchall()
|
|
found = {row[0]: (row[1], row[2]) for row in rows}
|
|
for att_id in attachment_ids:
|
|
item = found.get(att_id)
|
|
if item is None or item[0] != chat_id:
|
|
raise ApiError(404, "not_found", "Вложение не найдено")
|
|
if item[1] == "failed":
|
|
raise ApiError(409, "conflict", "Нельзя прикрепить необработанное вложение")
|
|
|
|
|
|
def create_message_idempotent(
|
|
chat_id: str,
|
|
sender_id: str,
|
|
text: str,
|
|
attachment_ids: list[str],
|
|
created: datetime,
|
|
method: str,
|
|
path: str,
|
|
idem_key: str,
|
|
request_hash: str,
|
|
) -> dict:
|
|
"""Один ключ — одно сообщение, в том числе при гонке двух реплик.
|
|
|
|
pg_advisory_xact_lock сериализует одинаковую область.
|
|
INSERT сообщения, связей и ключа коммитятся вместе.
|
|
"""
|
|
scope = "%s|%s|%s|%s" % (sender_id, method, path, idem_key)
|
|
ttl = idempotency_ttl()
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
conn.execute("SELECT pg_advisory_xact_lock(%s::bigint)", (_lock_key(scope),))
|
|
existing = conn.execute(
|
|
"""
|
|
SELECT request_hash, message_id, created_at
|
|
FROM idempotency_keys
|
|
WHERE user_id = %s AND http_method = %s AND path = %s AND idem_key = %s
|
|
""",
|
|
(uuid.UUID(sender_id), method, path, idem_key),
|
|
).fetchone()
|
|
if existing is not None:
|
|
age = conn.execute("SELECT now()").fetchone()[0] - existing[2]
|
|
if age.total_seconds() > ttl:
|
|
conn.execute(
|
|
"""
|
|
DELETE FROM idempotency_keys
|
|
WHERE user_id = %s AND http_method = %s AND path = %s AND idem_key = %s
|
|
""",
|
|
(uuid.UUID(sender_id), method, path, idem_key),
|
|
)
|
|
existing = None
|
|
if existing is not None:
|
|
if existing[0] != request_hash:
|
|
raise ApiError(409, "conflict", "повторный ключ с другим сообщением")
|
|
if not _is_member(conn, chat_id, sender_id):
|
|
raise ApiError(404, "not_found", "Чат не найден")
|
|
return _load_message(conn, existing[1])
|
|
if not _is_member(conn, chat_id, sender_id):
|
|
raise ApiError(404, "not_found", "Чат не найден")
|
|
_require_chat_attachments(conn, chat_id, attachment_ids)
|
|
mid = uuid.uuid4()
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
|
VALUES (%s, %s, %s, %s, %s)
|
|
""",
|
|
(mid, uuid.UUID(chat_id), uuid.UUID(sender_id), text, created),
|
|
)
|
|
for position, att_id in enumerate(attachment_ids):
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO message_attachments (message_id, attachment_id, position)
|
|
VALUES (%s, %s, %s)
|
|
""",
|
|
(mid, uuid.UUID(att_id), position),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO idempotency_keys (
|
|
user_id, http_method, path, idem_key, request_hash, message_id, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s)
|
|
""",
|
|
(uuid.UUID(sender_id), method, path, idem_key, request_hash, mid, created),
|
|
)
|
|
return _load_message(conn, mid)
|
|
|
|
|
|
def list_messages(chat_id: str, user_id: str, limit: int, cursor) -> dict:
|
|
params: list = [uuid.UUID(chat_id), uuid.UUID(chat_id), uuid.UUID(user_id)]
|
|
cursor_sql = ""
|
|
if cursor is not None:
|
|
created, message_id = cursor
|
|
cursor_sql = "AND (m.created_at, m.id) > (%s, %s)"
|
|
params.extend([created, uuid.UUID(message_id)])
|
|
params.append(limit + 1)
|
|
sql = f"""
|
|
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at,
|
|
COALESCE(
|
|
array_agg(ma.attachment_id ORDER BY ma.position)
|
|
FILTER (WHERE ma.attachment_id IS NOT NULL),
|
|
'{{}}'::uuid[]
|
|
)
|
|
FROM messages m
|
|
LEFT JOIN message_attachments ma ON ma.message_id = m.id
|
|
WHERE m.chat_id = %s
|
|
AND EXISTS (
|
|
SELECT 1 FROM chat_members
|
|
WHERE chat_id = %s AND user_id = %s
|
|
)
|
|
{cursor_sql}
|
|
GROUP BY m.id
|
|
ORDER BY m.created_at, m.id
|
|
LIMIT %s
|
|
"""
|
|
with connection() as conn:
|
|
rows = conn.execute(sql, params).fetchall()
|
|
if not rows:
|
|
with connection() as conn:
|
|
member = conn.execute(
|
|
"""
|
|
SELECT 1 FROM chat_members
|
|
WHERE chat_id = %s AND user_id = %s
|
|
""",
|
|
(uuid.UUID(chat_id), uuid.UUID(user_id)),
|
|
).fetchone()
|
|
if member is None:
|
|
raise ApiError(404, "not_found", "Чат не найден")
|
|
page = rows[:limit]
|
|
next_cursor = None
|
|
if len(rows) > limit and page:
|
|
last = page[-1]
|
|
next_cursor = (last[4], str(last[0]))
|
|
return {
|
|
"items": [message_json(row) for row in page],
|
|
"next_cursor_parts": next_cursor,
|
|
}
|
|
|
|
|
|
def message_json(row) -> dict:
|
|
return {
|
|
"id": str(row[0]),
|
|
"chat_id": str(row[1]),
|
|
"sender_id": str(row[2]),
|
|
"text": row[3],
|
|
"created_at": format_ts(row[4]),
|
|
"attachment_ids": _attachment_ids(row[5]),
|
|
}
|
|
|
|
|
|
def is_member(chat_id: str, user_id: str) -> bool:
|
|
try:
|
|
with connection() as conn:
|
|
return _is_member(conn, chat_id, user_id)
|
|
except ApiError:
|
|
raise
|
|
|
|
|
|
def insert_queued_attachment(
|
|
att_id: str,
|
|
chat_id: str,
|
|
owner_id: str,
|
|
filename: str,
|
|
content_type: str,
|
|
size_bytes: int,
|
|
digest: str,
|
|
object_key: str,
|
|
staging_key: str,
|
|
width: int,
|
|
height: int,
|
|
created: datetime,
|
|
job_payload: dict,
|
|
) -> dict:
|
|
"""Оригинал уже в staging. Строка, job и outbox коммитятся одной транзакцией."""
|
|
job_id = uuid.UUID(job_payload["job_id"])
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
if not _is_member(conn, chat_id, owner_id):
|
|
raise ApiError(404, "not_found", "Чат не найден")
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO attachments (
|
|
id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
|
object_key, width, height, created_at, updated_at, status,
|
|
error_code, staging_key, pipeline_version
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,
|
|
'queued', NULL, %s, 1)
|
|
""",
|
|
(
|
|
uuid.UUID(att_id),
|
|
uuid.UUID(chat_id),
|
|
uuid.UUID(owner_id),
|
|
filename,
|
|
content_type,
|
|
size_bytes,
|
|
digest,
|
|
object_key,
|
|
width,
|
|
height,
|
|
created,
|
|
created,
|
|
staging_key,
|
|
),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO attachment_jobs (
|
|
id, attachment_id, pipeline_version, attempt, status,
|
|
created_at, updated_at
|
|
)
|
|
VALUES (%s, %s, 1, %s, 'queued', %s, %s)
|
|
""",
|
|
(job_id, uuid.UUID(att_id), int(job_payload["attempt"]), created, created),
|
|
)
|
|
_insert_outbox(conn, job_id, job_payload, created)
|
|
created_text = format_ts(created)
|
|
return {
|
|
"id": att_id,
|
|
"chat_id": chat_id,
|
|
"owner_id": owner_id,
|
|
"filename": filename,
|
|
"content_type": content_type,
|
|
"size_bytes": size_bytes,
|
|
"sha256": digest,
|
|
"status": "queued",
|
|
"variants": [],
|
|
"error_code": None,
|
|
"created_at": created_text,
|
|
"updated_at": created_text,
|
|
}
|
|
|
|
|
|
def get_attachment(att_id: str, user_id: str) -> dict:
|
|
with connection() as conn:
|
|
row = _attachment_row(conn, att_id, user_id)
|
|
variants = _variants(conn, att_id, row[11])
|
|
return _attachment_public(row, variants)
|
|
|
|
|
|
def get_download(att_id: str, user_id: str, variant: str) -> dict:
|
|
with connection() as conn:
|
|
row = _attachment_row(conn, att_id, user_id)
|
|
status = row[7]
|
|
if status != "ready":
|
|
raise ApiError(409, "conflict", "Вложение ещё не готово")
|
|
if variant == "original":
|
|
return {
|
|
"content_type": row[4],
|
|
"size_bytes": row[5],
|
|
"sha256": row[6],
|
|
"object_key": row[12],
|
|
}
|
|
found = conn.execute(
|
|
"""
|
|
SELECT content_type, size_bytes, sha256, object_key
|
|
FROM attachment_variants
|
|
WHERE attachment_id = %s AND pipeline_version = %s AND name = %s
|
|
""",
|
|
(uuid.UUID(att_id), row[11], variant),
|
|
).fetchone()
|
|
if found is None:
|
|
raise ApiError(404, "not_found", "Вариант не найден")
|
|
return {
|
|
"content_type": found[0],
|
|
"size_bytes": found[1],
|
|
"sha256": found[2],
|
|
"object_key": found[3],
|
|
}
|
|
|
|
|
|
def load_job_source(attachment_id: str) -> dict | None:
|
|
with connection() as conn:
|
|
row = conn.execute(
|
|
"""
|
|
SELECT a.id::text, a.filename, a.content_type, a.size_bytes, a.sha256,
|
|
a.width, a.height, a.object_key, a.staging_key, a.status,
|
|
a.pipeline_version, j.id::text, j.attempt, j.status
|
|
FROM attachments a
|
|
JOIN attachment_jobs j
|
|
ON j.attachment_id = a.id AND j.pipeline_version = a.pipeline_version
|
|
WHERE a.id = %s
|
|
""",
|
|
(uuid.UUID(attachment_id),),
|
|
).fetchone()
|
|
if row is None:
|
|
return None
|
|
return {
|
|
"attachment_id": row[0],
|
|
"filename": row[1],
|
|
"content_type": row[2],
|
|
"size_bytes": row[3],
|
|
"sha256": row[4],
|
|
"width": row[5],
|
|
"height": row[6],
|
|
"object_key": row[7],
|
|
"staging_key": row[8],
|
|
"attachment_status": row[9],
|
|
"pipeline_version": row[10],
|
|
"job_id": row[11],
|
|
"attempt": row[12],
|
|
"job_status": row[13],
|
|
}
|
|
|
|
|
|
def _attachment_row(conn, att_id: str, user_id: str):
|
|
row = conn.execute(
|
|
"""
|
|
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
|
|
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at,
|
|
a.updated_at, a.pipeline_version, a.object_key
|
|
FROM attachments a
|
|
WHERE a.id = %s
|
|
AND EXISTS (
|
|
SELECT 1 FROM chat_members
|
|
WHERE chat_id = a.chat_id AND user_id = %s
|
|
)
|
|
""",
|
|
(uuid.UUID(att_id), uuid.UUID(user_id)),
|
|
).fetchone()
|
|
if row is None:
|
|
raise ApiError(404, "not_found", "Вложение не найдено")
|
|
return row
|
|
|
|
|
|
def _variants(conn, att_id: str, pipeline_version: int) -> list:
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT name, content_type, size_bytes, width, height, sha256
|
|
FROM attachment_variants
|
|
WHERE attachment_id = %s AND pipeline_version = %s
|
|
ORDER BY CASE name
|
|
WHEN 'thumbnail' THEN 1
|
|
WHEN 'crop' THEN 2
|
|
WHEN 'optimized' THEN 3
|
|
ELSE 4
|
|
END
|
|
""",
|
|
(uuid.UUID(att_id), pipeline_version),
|
|
).fetchall()
|
|
return [
|
|
{
|
|
"name": row[0],
|
|
"content_type": row[1],
|
|
"size_bytes": row[2],
|
|
"width": row[3],
|
|
"height": row[4],
|
|
"sha256": row[5],
|
|
}
|
|
for row in rows
|
|
]
|
|
|
|
|
|
def _attachment_public(row, variants: list) -> dict:
|
|
return {
|
|
"id": str(row[0]),
|
|
"chat_id": str(row[1]),
|
|
"owner_id": str(row[2]),
|
|
"filename": row[3],
|
|
"content_type": row[4],
|
|
"size_bytes": row[5],
|
|
"sha256": row[6],
|
|
"status": row[7],
|
|
"variants": variants if row[7] == "ready" else [],
|
|
"error_code": row[8],
|
|
"created_at": format_ts(row[9]),
|
|
"updated_at": format_ts(row[10]),
|
|
}
|
|
|
|
|
|
def _insert_outbox(conn, job_id, payload: dict, available_at: datetime) -> None:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO outbox (id, job_id, payload, created_at, available_at)
|
|
VALUES (%s, %s, %s::jsonb, %s, %s)
|
|
ON CONFLICT (job_id) WHERE published_at IS NULL DO NOTHING
|
|
""",
|
|
(
|
|
uuid.uuid4(),
|
|
job_id if isinstance(job_id, uuid.UUID) else uuid.UUID(str(job_id)),
|
|
json_dumps(payload),
|
|
available_at,
|
|
available_at,
|
|
),
|
|
)
|
|
|
|
|
|
def json_dumps(payload: dict) -> str:
|
|
import json
|
|
return json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
|
|
|
|
|
|
def claim_job(job_id: str, lease_seconds: int) -> dict:
|
|
"""Захватить работу. Готовый или окончательно failed job подтверждается без эффекта."""
|
|
token = uuid.uuid4()
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
row = conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'processing',
|
|
lease_token = %s,
|
|
lease_until = now() + make_interval(secs => %s),
|
|
updated_at = now()
|
|
WHERE id = %s
|
|
AND status IN ('queued', 'processing')
|
|
AND (
|
|
status = 'queued'
|
|
OR lease_until IS NULL
|
|
OR lease_until < now()
|
|
)
|
|
RETURNING attempt, attachment_id::text, pipeline_version
|
|
""",
|
|
(token, int(lease_seconds), uuid.UUID(job_id)),
|
|
).fetchone()
|
|
if row is not None:
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'processing', error_code = NULL, updated_at = now()
|
|
WHERE id = %s AND pipeline_version = %s AND status IN ('queued', 'processing')
|
|
""",
|
|
(uuid.UUID(row[1]), row[2]),
|
|
)
|
|
return {
|
|
"action": "work",
|
|
"lease_token": str(token),
|
|
"attempt": row[0],
|
|
"attachment_id": row[1],
|
|
"pipeline_version": row[2],
|
|
}
|
|
state = conn.execute(
|
|
"SELECT status FROM attachment_jobs WHERE id = %s",
|
|
(uuid.UUID(job_id),),
|
|
).fetchone()
|
|
if state is None or state[0] in ("ready", "failed"):
|
|
return {"action": "ack"}
|
|
return {"action": "requeue"}
|
|
|
|
|
|
def finish_job(job_id: str, lease_token: str, variants: list, pipeline_version: int) -> str:
|
|
"""Атомарно записать варианты и ready. Чужой lease и уже готовый job не затираются."""
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
current = conn.execute(
|
|
"""
|
|
SELECT status, attachment_id::text, pipeline_version
|
|
FROM attachment_jobs
|
|
WHERE id = %s
|
|
FOR UPDATE
|
|
""",
|
|
(uuid.UUID(job_id),),
|
|
).fetchone()
|
|
if current is None:
|
|
return "missing"
|
|
if current[0] == "ready":
|
|
return "ready"
|
|
if current[2] != pipeline_version:
|
|
return "stale"
|
|
owned = conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'ready',
|
|
lease_token = NULL,
|
|
lease_until = NULL,
|
|
error_code = NULL,
|
|
next_attempt_at = NULL,
|
|
updated_at = now()
|
|
WHERE id = %s AND lease_token = %s AND status = 'processing'
|
|
RETURNING attachment_id::text
|
|
""",
|
|
(uuid.UUID(job_id), uuid.UUID(lease_token)),
|
|
).fetchone()
|
|
if owned is None:
|
|
return "lost"
|
|
attachment_id = owned[0]
|
|
for item in variants:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO attachment_variants (
|
|
attachment_id, pipeline_version, name, content_type,
|
|
size_bytes, width, height, sha256, object_key, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, now())
|
|
ON CONFLICT (attachment_id, pipeline_version, name) DO UPDATE
|
|
SET content_type = EXCLUDED.content_type,
|
|
size_bytes = EXCLUDED.size_bytes,
|
|
width = EXCLUDED.width,
|
|
height = EXCLUDED.height,
|
|
sha256 = EXCLUDED.sha256,
|
|
object_key = EXCLUDED.object_key
|
|
WHERE attachment_variants.sha256 IS DISTINCT FROM EXCLUDED.sha256
|
|
""",
|
|
(
|
|
uuid.UUID(attachment_id),
|
|
pipeline_version,
|
|
item["name"],
|
|
item["content_type"],
|
|
item["size_bytes"],
|
|
item["width"],
|
|
item["height"],
|
|
item["sha256"],
|
|
item["object_key"],
|
|
),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'ready', error_code = NULL, updated_at = now()
|
|
WHERE id = %s AND pipeline_version = %s AND status <> 'failed'
|
|
""",
|
|
(uuid.UUID(attachment_id), pipeline_version),
|
|
)
|
|
return "ready"
|
|
|
|
|
|
def fail_job(job_id: str, lease_token: str, error_code: str, attempt: int) -> str:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
owned = conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'failed',
|
|
error_code = %s,
|
|
lease_token = NULL,
|
|
lease_until = NULL,
|
|
next_attempt_at = NULL,
|
|
updated_at = now()
|
|
WHERE id = %s AND lease_token = %s AND status = 'processing'
|
|
RETURNING attachment_id::text, pipeline_version
|
|
""",
|
|
(error_code, uuid.UUID(job_id), uuid.UUID(lease_token)),
|
|
).fetchone()
|
|
if owned is None:
|
|
state = conn.execute(
|
|
"SELECT status FROM attachment_jobs WHERE id = %s",
|
|
(uuid.UUID(job_id),),
|
|
).fetchone()
|
|
if state is not None and state[0] == "ready":
|
|
return "ready"
|
|
return "lost"
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = %s, updated_at = now()
|
|
WHERE id = %s AND status <> 'ready'
|
|
""",
|
|
(error_code, uuid.UUID(owned[0])),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO job_failures (
|
|
id, job_id, attachment_id, attempt, error_code, permanent, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, TRUE, now())
|
|
""",
|
|
(uuid.uuid4(), uuid.UUID(job_id), uuid.UUID(owned[0]), attempt, error_code),
|
|
)
|
|
return "failed"
|
|
|
|
|
|
def retry_job(job_id: str, lease_token: str, error_code: str, payload: dict, available_at: datetime, max_attempts: int) -> str:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
current = conn.execute(
|
|
"""
|
|
SELECT attempt, attachment_id::text, status
|
|
FROM attachment_jobs
|
|
WHERE id = %s AND lease_token = %s AND status = 'processing'
|
|
FOR UPDATE
|
|
""",
|
|
(uuid.UUID(job_id), uuid.UUID(lease_token)),
|
|
).fetchone()
|
|
if current is None:
|
|
state = conn.execute(
|
|
"SELECT status FROM attachment_jobs WHERE id = %s",
|
|
(uuid.UUID(job_id),),
|
|
).fetchone()
|
|
if state is not None and state[0] == "ready":
|
|
return "ready"
|
|
return "lost"
|
|
attempt = current[0]
|
|
attachment_id = current[1]
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO job_failures (
|
|
id, job_id, attachment_id, attempt, error_code, permanent, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, %s, FALSE, now())
|
|
""",
|
|
(uuid.uuid4(), uuid.UUID(job_id), uuid.UUID(attachment_id), attempt, error_code),
|
|
)
|
|
if attempt >= max_attempts:
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'failed', error_code = 'retry_exhausted',
|
|
lease_token = NULL, lease_until = NULL, updated_at = now()
|
|
WHERE id = %s
|
|
""",
|
|
(uuid.UUID(job_id),),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = 'retry_exhausted', updated_at = now()
|
|
WHERE id = %s AND status <> 'ready'
|
|
""",
|
|
(uuid.UUID(attachment_id),),
|
|
)
|
|
return "failed"
|
|
next_attempt = attempt + 1
|
|
payload = dict(payload)
|
|
payload["attempt"] = next_attempt
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'queued',
|
|
attempt = %s,
|
|
lease_token = NULL,
|
|
lease_until = NULL,
|
|
error_code = %s,
|
|
next_attempt_at = %s,
|
|
updated_at = now()
|
|
WHERE id = %s
|
|
""",
|
|
(next_attempt, error_code, available_at, uuid.UUID(job_id)),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'queued', error_code = NULL, updated_at = now()
|
|
WHERE id = %s AND status <> 'ready'
|
|
""",
|
|
(uuid.UUID(attachment_id),),
|
|
)
|
|
_insert_outbox(conn, job_id, payload, available_at)
|
|
return "retry"
|
|
|
|
|
|
def recover_expired_leases(max_attempts: int) -> int:
|
|
"""Истёкший processing возвращается в очередь или становится failed. Эффект не дублируется."""
|
|
changed = 0
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id::text, attachment_id::text, pipeline_version, attempt, created_at
|
|
FROM attachment_jobs
|
|
WHERE status = 'processing' AND lease_until < now()
|
|
FOR UPDATE SKIP LOCKED
|
|
"""
|
|
).fetchall()
|
|
for job_id, attachment_id, pipeline_version, attempt, created_at in rows:
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO job_failures (
|
|
id, job_id, attachment_id, attempt, error_code, permanent, created_at
|
|
)
|
|
VALUES (%s, %s, %s, %s, 'lease_expired', FALSE, now())
|
|
""",
|
|
(uuid.uuid4(), uuid.UUID(job_id), uuid.UUID(attachment_id), attempt),
|
|
)
|
|
if attempt >= max_attempts:
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'failed', error_code = 'retry_exhausted',
|
|
lease_token = NULL, lease_until = NULL, updated_at = now()
|
|
WHERE id = %s
|
|
""",
|
|
(uuid.UUID(job_id),),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'failed', error_code = 'retry_exhausted', updated_at = now()
|
|
WHERE id = %s AND status = 'processing'
|
|
""",
|
|
(uuid.UUID(attachment_id),),
|
|
)
|
|
else:
|
|
next_attempt = attempt + 1
|
|
payload = {
|
|
"schema_version": 1,
|
|
"job_id": job_id,
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": attachment_id,
|
|
"pipeline_version": pipeline_version,
|
|
"attempt": next_attempt,
|
|
"created_at": format_ts(created_at),
|
|
}
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'queued', attempt = %s, lease_token = NULL,
|
|
lease_until = NULL, next_attempt_at = now(), updated_at = now()
|
|
WHERE id = %s
|
|
""",
|
|
(next_attempt, uuid.UUID(job_id)),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'queued', updated_at = now()
|
|
WHERE id = %s AND status = 'processing'
|
|
""",
|
|
(uuid.UUID(attachment_id),),
|
|
)
|
|
_insert_outbox(conn, job_id, payload, datetime.now(timezone.utc))
|
|
changed += 1
|
|
return changed
|
|
|
|
|
|
def reconcile_queued(stale_seconds: int) -> int:
|
|
"""Если wake-up потерялся, создать ещё один outbox. Повторная доставка остаётся идемпотентной."""
|
|
inserted = 0
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT j.id::text, j.attachment_id::text, j.pipeline_version, j.attempt, j.created_at
|
|
FROM attachment_jobs j
|
|
WHERE j.status = 'queued'
|
|
AND (j.next_attempt_at IS NULL OR j.next_attempt_at <= now())
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM outbox o
|
|
WHERE o.job_id = j.id AND o.published_at IS NULL
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM outbox o
|
|
WHERE o.job_id = j.id
|
|
AND o.published_at > now() - make_interval(secs => %s)
|
|
)
|
|
FOR UPDATE OF j SKIP LOCKED
|
|
""",
|
|
(int(stale_seconds),),
|
|
).fetchall()
|
|
now = datetime.now(timezone.utc)
|
|
for job_id, attachment_id, pipeline_version, attempt, created_at in rows:
|
|
payload = {
|
|
"schema_version": 1,
|
|
"job_id": job_id,
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": attachment_id,
|
|
"pipeline_version": pipeline_version,
|
|
"attempt": attempt,
|
|
"created_at": format_ts(created_at),
|
|
}
|
|
_insert_outbox(conn, job_id, payload, now)
|
|
inserted += 1
|
|
return inserted
|
|
|
|
|
|
def take_outbox(limit: int) -> list:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
rows = conn.execute(
|
|
"""
|
|
WITH picked AS (
|
|
SELECT id
|
|
FROM outbox
|
|
WHERE published_at IS NULL AND available_at <= now()
|
|
ORDER BY available_at
|
|
FOR UPDATE SKIP LOCKED
|
|
LIMIT %s
|
|
)
|
|
UPDATE outbox AS o
|
|
SET publish_attempts = o.publish_attempts + 1
|
|
FROM picked
|
|
WHERE o.id = picked.id
|
|
RETURNING o.id::text, o.payload
|
|
""",
|
|
(int(limit),),
|
|
).fetchall()
|
|
items = []
|
|
for row_id, payload in rows:
|
|
if isinstance(payload, str):
|
|
import json
|
|
payload = json.loads(payload)
|
|
items.append((row_id, payload))
|
|
return items
|
|
|
|
|
|
def mark_outbox_published(outbox_id: str) -> None:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
conn.execute(
|
|
"UPDATE outbox SET published_at = now() WHERE id = %s AND published_at IS NULL",
|
|
(uuid.UUID(outbox_id),),
|
|
)
|
|
|
|
|
|
def clear_staging_key(attachment_id: str) -> None:
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
conn.execute(
|
|
"UPDATE attachments SET staging_key = NULL WHERE id = %s",
|
|
(uuid.UUID(attachment_id),),
|
|
)
|
|
|
|
|
|
def staging_ready_for_cleanup(limit: int) -> list:
|
|
with connection() as conn:
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id::text, staging_key
|
|
FROM attachments
|
|
WHERE status = 'ready' AND staging_key IS NOT NULL
|
|
LIMIT %s
|
|
""",
|
|
(int(limit),),
|
|
).fetchall()
|
|
return [(row[0], row[1]) for row in rows]
|
|
|
|
|
|
def expired_failed_staging(retention_seconds: int, limit: int) -> list:
|
|
with connection() as conn:
|
|
rows = conn.execute(
|
|
"""
|
|
SELECT id::text, staging_key
|
|
FROM attachments
|
|
WHERE status = 'failed'
|
|
AND staging_key IS NOT NULL
|
|
AND updated_at < now() - make_interval(secs => %s)
|
|
LIMIT %s
|
|
""",
|
|
(int(retention_seconds), int(limit)),
|
|
).fetchall()
|
|
return [(row[0], row[1]) for row in rows]
|
|
|
|
|
|
def replay_attachment(att_id: str, user_id: str) -> dict:
|
|
"""Явный replay. Уже ready job не запускается повторно."""
|
|
with connection() as conn:
|
|
with conn.transaction():
|
|
row = _attachment_row(conn, att_id, user_id)
|
|
status = row[7]
|
|
pipeline_version = row[11]
|
|
if status == "ready":
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO job_replays (id, job_id, attachment_id, outcome, created_at)
|
|
SELECT %s, j.id, a.id, 'ignored_ready', now()
|
|
FROM attachments a
|
|
JOIN attachment_jobs j
|
|
ON j.attachment_id = a.id AND j.pipeline_version = a.pipeline_version
|
|
WHERE a.id = %s
|
|
""",
|
|
(uuid.uuid4(), uuid.UUID(att_id)),
|
|
)
|
|
variants = _variants(conn, att_id, pipeline_version)
|
|
return _attachment_public(row, variants)
|
|
if status in ("queued", "processing"):
|
|
raise ApiError(409, "conflict", "Задание уже выполняется")
|
|
job = conn.execute(
|
|
"""
|
|
SELECT id::text, attempt, created_at
|
|
FROM attachment_jobs
|
|
WHERE attachment_id = %s AND pipeline_version = %s
|
|
FOR UPDATE
|
|
""",
|
|
(uuid.UUID(att_id), pipeline_version),
|
|
).fetchone()
|
|
if job is None:
|
|
raise ApiError(404, "not_found", "Задание не найдено")
|
|
if job[1] >= 1000:
|
|
raise ApiError(409, "conflict", "Слишком много повторов")
|
|
next_attempt = job[1] + 1
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachment_jobs
|
|
SET status = 'queued', attempt = %s, error_code = NULL,
|
|
lease_token = NULL, lease_until = NULL, next_attempt_at = now(),
|
|
updated_at = now()
|
|
WHERE id = %s AND status = 'failed'
|
|
""",
|
|
(next_attempt, uuid.UUID(job[0])),
|
|
)
|
|
conn.execute(
|
|
"""
|
|
UPDATE attachments
|
|
SET status = 'queued', error_code = NULL, updated_at = now()
|
|
WHERE id = %s AND status = 'failed'
|
|
""",
|
|
(uuid.UUID(att_id),),
|
|
)
|
|
payload = {
|
|
"schema_version": 1,
|
|
"job_id": job[0],
|
|
"type": "attachment.process.v1",
|
|
"attachment_id": att_id,
|
|
"pipeline_version": pipeline_version,
|
|
"attempt": next_attempt,
|
|
"created_at": format_ts(job[2]),
|
|
}
|
|
_insert_outbox(conn, job[0], payload, datetime.now(timezone.utc))
|
|
conn.execute(
|
|
"""
|
|
INSERT INTO job_replays (id, job_id, attachment_id, outcome, created_at)
|
|
VALUES (%s, %s, %s, 'queued', now())
|
|
""",
|
|
(uuid.uuid4(), uuid.UUID(job[0]), uuid.UUID(att_id)),
|
|
)
|
|
return get_attachment(att_id, user_id)
|