final vers

This commit is contained in:
2026-11-12 22:50:05 +03:00
parent 9899ee4676
commit 2639c08944
2 changed files with 31 additions and 18 deletions
+29 -13
View File
@@ -75,28 +75,25 @@ def same_request(record, job_id, actor, reason):
}
async def replay_failed_job(
session, storage, settings, job_id, *, request_id, actor, reason
):
def validate_replay_request(actor: str, reason: str) -> None:
"""Проверить данные оператора для записи аудита."""
if (
re.fullmatch(r"[A-Za-z0-9._-]{1,64}", actor) is None
or reason not in REPLAY_REASONS
):
raise JobOperationError("invalid_audit_fields")
row = await jobs.lock_job(session, job_id)
if row is None:
raise JobOperationError("unknown_job")
record = await job_admin.replay_record(session, request_id)
if record is not None:
result = same_request(record, job_id, actor, reason)
await session.rollback()
return result
job, attachment = row
def validate_replay_job(job, attachment) -> None:
"""Разрешить новый replay только для failed с сохранённым исходником."""
if attachment.status != "failed":
raise JobOperationError("job_not_failed")
if job.staging_key is None:
raise JobOperationError("source_expired")
# Та же блокировка защищает исходник от cleanup до окончания проверки и COMMIT.
async def verify_replay_source(storage, settings, job, attachment) -> None:
"""Проверить размер и SHA-256 исходника в S3 под блокировкой job."""
work = JobWork(
job.id,
attachment.id,
@@ -117,6 +114,25 @@ async def replay_failed_job(
if error.code in {"NoSuchKey", "NoSuchObject"}:
raise JobOperationError("source_missing") from None
raise
async def replay_failed_job(
session, storage, settings, job_id, *, request_id, actor, reason
):
"""Организовать replay; блокировка, rollback и commit остаются здесь."""
validate_replay_request(actor, reason)
row = await jobs.lock_job(session, job_id)
if row is None:
raise JobOperationError("unknown_job")
record = await job_admin.replay_record(session, request_id)
if record is not None:
result = same_request(record, job_id, actor, reason)
await session.rollback()
return result
job, attachment = row
validate_replay_job(job, attachment)
# Блокировка сохраняется во время проверки S3, аудита и изменения job.
await verify_replay_source(storage, settings, job, attachment)
limit = job.attempt + settings.job_max_attempts
inserted = await job_admin.insert_replay(
session,