refactor code

This commit is contained in:
Матвей
2026-11-06 11:12:38 +03:00
parent c885462c04
commit 7fe48e38c1
50 changed files with 5674 additions and 564 deletions
+37
View File
@@ -341,6 +341,9 @@ class PostgresStore:
await (connection or self.pool).execute(
"INSERT INTO attachment_jobs (job_id, attachment_id, trace_context) VALUES ($1, $2, $3::jsonb)", job_id, attachment_id, json.dumps(tel.carrier())
)
await (connection or self.pool).execute(
"INSERT INTO attachment_slo (attachment_id) VALUES ($1)", attachment_id
)
payload = {
"schema_version": 1, "job_id": str(job_id), "type": "attachment.process.v1",
"attachment_id": str(attachment_id), "pipeline_version": 1, "attempt": 1,
@@ -438,10 +441,44 @@ class PostgresStore:
attachment_id, original_key,
)
await connection.execute("UPDATE attachment_jobs SET status = 'ready', lease_until = NULL, updated_at = now() WHERE attachment_id = $1", attachment_id)
await connection.execute(
"UPDATE attachment_slo SET ready_xid=pg_current_xact_id()::xid "
"WHERE attachment_id=$1 AND ready_xid IS NULL", attachment_id
)
return True
except DB_ERRORS as error:
raise RuntimeError("PostgreSQL is unavailable") from error
async def ready_seconds(self, attachment_id: UUID):
# Both ends are DB commit timestamps, not transaction start or a worker's clock.
return await self.pool.fetchval("""
SELECT EXTRACT(EPOCH FROM (
COALESCE(ready_at, pg_xact_commit_timestamp(ready_xid)) -
COALESCE(accepted_at, pg_xact_commit_timestamp(accepted_xid))))::float8
FROM attachment_slo WHERE attachment_id=$1
""", attachment_id)
async def sample_ready_slo(self):
# Materialize promptly: PostgreSQL does not retain commit timestamps forever.
async with self.pool.acquire() as connection, connection.transaction():
await connection.execute("""
UPDATE attachment_slo SET
accepted_at=COALESCE(accepted_at, pg_xact_commit_timestamp(accepted_xid)),
ready_at=COALESCE(ready_at, pg_xact_commit_timestamp(ready_xid))
WHERE accepted_at IS NULL OR (ready_xid IS NOT NULL AND ready_at IS NULL)
""")
await connection.execute("""
UPDATE attachment_slo SET bad=(ready_at IS NULL OR ready_at>accepted_at+interval '30 seconds')
WHERE bad IS NULL AND accepted_at<=now()-interval '30 seconds'
AND (ready_xid IS NULL OR ready_at IS NOT NULL)
""")
return await connection.fetchrow("""
SELECT count(bad) AS eligible, count(*) FILTER (WHERE bad) AS bad,
count(*) FILTER (WHERE accepted_at IS NULL OR
(ready_xid IS NOT NULL AND ready_at IS NULL)) AS unknown
FROM attachment_slo
""")
async def retry_attachment_job(self, attachment_id: UUID, max_attempts: int, delay_seconds: float, claim_time, permanent_code=None) -> bool:
"""Return true only when a bounded at-least-once retry was recorded."""
try: