created tasks for grade 4
This commit is contained in:
@@ -9,6 +9,9 @@ from tempfile import TemporaryDirectory
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from minio.error import MinioException
|
||||
from opentelemetry.context import Context
|
||||
from opentelemetry.trace import SpanKind
|
||||
from opentelemetry.trace.propagation.tracecontext import TraceContextTextMapPropagator
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from urllib3.exceptions import HTTPError as StorageHTTPError
|
||||
|
||||
@@ -44,6 +47,7 @@ async def publish_next_job(
|
||||
*,
|
||||
attachment_id: UUID | None = None,
|
||||
before_publish: Callable[[UUID], None] | None = None,
|
||||
telemetry: Telemetry | None = None,
|
||||
) -> bool:
|
||||
"""Отметка publish следует за durable publisher confirm; crash допускает тот же job-дубль."""
|
||||
job = await outbox.next_unpublished_job(session, attachment_id=attachment_id)
|
||||
@@ -51,15 +55,20 @@ async def publish_next_job(
|
||||
if job is not None:
|
||||
if before_publish is not None:
|
||||
before_publish(job.attachment_id)
|
||||
await broker.publish_job(
|
||||
JobMessage(
|
||||
job_id=job.id,
|
||||
attachment_id=job.attachment_id,
|
||||
pipeline_version=job.pipeline_version,
|
||||
attempt=job.attempt + 1,
|
||||
created_at=job.created_at,
|
||||
parent = TraceContextTextMapPropagator().extract(job.trace_context or {}, context=Context())
|
||||
with telemetry.span(
|
||||
"rabbitmq.publish", context=parent, kind=SpanKind.PRODUCER,
|
||||
attributes={"messaging.system": "rabbitmq", "job.attempt": job.attempt + 1},
|
||||
) if telemetry else nullcontext():
|
||||
await broker.publish_job(
|
||||
JobMessage(
|
||||
job_id=job.id,
|
||||
attachment_id=job.attachment_id,
|
||||
pipeline_version=job.pipeline_version,
|
||||
attempt=job.attempt + 1,
|
||||
created_at=job.created_at,
|
||||
)
|
||||
)
|
||||
)
|
||||
outbox.mark_published(job)
|
||||
await session.commit()
|
||||
return published
|
||||
@@ -145,11 +154,13 @@ async def build_result(
|
||||
storage: MinioStorage,
|
||||
work: JobWork,
|
||||
after_object: Callable[[UUID, str], None] | None = None,
|
||||
telemetry: Telemetry | None = None,
|
||||
) -> bool:
|
||||
"""Фиксированные ключи и блокировка job защищают PUT от устаревшей попытки."""
|
||||
with TemporaryDirectory(prefix="messenger-job-") as directory:
|
||||
source = Path(directory) / "original"
|
||||
await fetch_original(storage, work, source)
|
||||
with telemetry.span("s3.read_staging") if telemetry else nullcontext():
|
||||
await fetch_original(storage, work, source)
|
||||
result = await render_variants(storage, work, source, Path(directory))
|
||||
row = await jobs.lock_job(session, work.job_id)
|
||||
if row is None:
|
||||
@@ -165,33 +176,39 @@ async def build_result(
|
||||
return False
|
||||
# Блокировка остаётся до завершения PUT/COMMIT; reaper использует SKIP LOCKED.
|
||||
# При отмене S3 adapter дожидается SDK-потока до освобождения блокировки.
|
||||
await storage.put_file(
|
||||
work.original_key,
|
||||
source,
|
||||
size_bytes=work.size_bytes,
|
||||
content_type=work.content_type,
|
||||
sha256=work.sha256,
|
||||
)
|
||||
if after_object is not None:
|
||||
after_object(work.attachment_id, "original")
|
||||
for variant in result.variants:
|
||||
with telemetry.span("s3.write_result") if telemetry else nullcontext():
|
||||
await storage.put_file(
|
||||
work.variant_key(variant.name),
|
||||
Path(directory) / VARIANT_FILENAMES[variant.name],
|
||||
size_bytes=variant.size_bytes,
|
||||
content_type=variant.content_type,
|
||||
sha256=variant.sha256,
|
||||
work.original_key,
|
||||
source,
|
||||
size_bytes=work.size_bytes,
|
||||
content_type=work.content_type,
|
||||
sha256=work.sha256,
|
||||
)
|
||||
if after_object is not None:
|
||||
after_object(work.attachment_id, variant.name)
|
||||
jobs.complete_job(
|
||||
job,
|
||||
attachment,
|
||||
work,
|
||||
[variant.model_dump() for variant in result.variants],
|
||||
await jobs.database_time(session),
|
||||
)
|
||||
await session.commit()
|
||||
after_object(work.attachment_id, "original")
|
||||
for variant in result.variants:
|
||||
await storage.put_file(
|
||||
work.variant_key(variant.name),
|
||||
Path(directory) / VARIANT_FILENAMES[variant.name],
|
||||
size_bytes=variant.size_bytes,
|
||||
content_type=variant.content_type,
|
||||
sha256=variant.sha256,
|
||||
)
|
||||
if after_object is not None:
|
||||
after_object(work.attachment_id, variant.name)
|
||||
with telemetry.span("postgres.commit_ready") if telemetry else nullcontext():
|
||||
ready_at = await jobs.database_time(session)
|
||||
accepted_at = job.created_at
|
||||
jobs.complete_job(
|
||||
job,
|
||||
attachment,
|
||||
work,
|
||||
[variant.model_dump() for variant in result.variants],
|
||||
ready_at,
|
||||
)
|
||||
await session.commit()
|
||||
if telemetry and telemetry.enabled:
|
||||
telemetry.time_to_ready.record(max(0, (ready_at - accepted_at).total_seconds()))
|
||||
return True
|
||||
|
||||
|
||||
@@ -205,7 +222,8 @@ async def process_attachment_job(
|
||||
before_processing: Callable[[UUID], None] | None = None,
|
||||
telemetry: Telemetry | None = None,
|
||||
) -> bool:
|
||||
claim = await claim_job(session, message, settings)
|
||||
with telemetry.span("postgres.claim_job") if telemetry else nullcontext():
|
||||
claim = await claim_job(session, message, settings)
|
||||
acknowledge = claim.acknowledge
|
||||
if claim.work is not None:
|
||||
work = claim.work
|
||||
@@ -214,7 +232,7 @@ async def process_attachment_job(
|
||||
async with asyncio.timeout(settings.job_timeout_seconds):
|
||||
if before_processing is not None:
|
||||
before_processing(work.attachment_id)
|
||||
acknowledge = await build_result(session, storage, work, after_object)
|
||||
acknowledge = await build_result(session, storage, work, after_object, telemetry)
|
||||
result["outcome"] = "success" if acknowledge else "deferred"
|
||||
except (
|
||||
ProcessingError,
|
||||
|
||||
Reference in New Issue
Block a user