forked from mtusi2026/mtusi2026lab6
refactor code
This commit is contained in:
+20
-16
@@ -94,17 +94,17 @@ def variants(data: bytes, max_pixels: int = 40_000_000) -> list[tuple[str, bytes
|
||||
]
|
||||
|
||||
|
||||
async def process(store: PostgresStore, s3: S3Store, payload: dict, lease_seconds: int) -> None:
|
||||
async def process(store: PostgresStore, s3: S3Store, payload: dict, lease_seconds: int) -> str:
|
||||
# Excludes cleanup for the whole lifetime of S3 effects, including stale attempts.
|
||||
async with store.attachment_transaction():
|
||||
await process_locked(store, s3, payload, lease_seconds)
|
||||
return await process_locked(store, s3, payload, lease_seconds)
|
||||
|
||||
|
||||
async def process_locked(store: PostgresStore, s3: S3Store, payload: dict, lease_seconds: int) -> None:
|
||||
async def process_locked(store: PostgresStore, s3: S3Store, payload: dict, lease_seconds: int) -> str:
|
||||
attachment_id = UUID(payload["attachment_id"])
|
||||
attachment = await store.claim_attachment_job(attachment_id, lease_seconds)
|
||||
if attachment is None:
|
||||
return
|
||||
return "skipped"
|
||||
payload['_claim_time'] = attachment['claim_time']
|
||||
payload['attempt'] = attachment.get('attempt', payload.get('attempt', 1))
|
||||
attempt_key = uuid4().hex
|
||||
@@ -133,13 +133,18 @@ async def process_locked(store: PostgresStore, s3: S3Store, payload: dict, lease
|
||||
os._exit(70)
|
||||
committed = await store.complete_attachment_job(attachment_id, original_key, metadata, attachment['claim_time'])
|
||||
if committed:
|
||||
if attachment.get('created_at') is not None:
|
||||
tel.ready_duration.record(max(0, (datetime.now(timezone.utc) - attachment['created_at']).total_seconds()), {"job_type": "attachment"})
|
||||
try:
|
||||
duration = await store.ready_seconds(attachment_id)
|
||||
if duration is not None:
|
||||
tel.ready_duration.record(duration, {"job_type": "attachment"})
|
||||
except Exception:
|
||||
tel.log.warning("telemetry.unavailable")
|
||||
try:
|
||||
await s3.delete(attachment["staging_key"])
|
||||
except Exception:
|
||||
logging.warning('staging cleanup deferred attachment_id=%s', attachment_id)
|
||||
|
||||
return "success" if committed else "skipped"
|
||||
|
||||
def settle_delivery(channel, delivery_tag, future):
|
||||
"""Only a successful durable disposition permits acknowledgement."""
|
||||
@@ -169,7 +174,7 @@ def main() -> None:
|
||||
s3 = S3Store(S3Settings.load())
|
||||
await store.connect()
|
||||
try:
|
||||
await process(store, s3, payload, max(1, int(os.environ.get("JOB_TIMEOUT_SECONDS", "60"))))
|
||||
return await process(store, s3, payload, max(1, int(os.environ.get("JOB_TIMEOUT_SECONDS", "60"))))
|
||||
finally:
|
||||
s3.client.close()
|
||||
await store.close()
|
||||
@@ -198,15 +203,15 @@ def main() -> None:
|
||||
except (ValueError, KeyError, TypeError):
|
||||
try:
|
||||
asyncio.run(publish_dlq({'failure': 'invalid_job'}))
|
||||
return True, False
|
||||
return True, "failed"
|
||||
except Exception:
|
||||
return False, False
|
||||
return False, "failed"
|
||||
try:
|
||||
asyncio.run(handle_async(payload))
|
||||
outcome = asyncio.run(handle_async(payload))
|
||||
except Exception as error:
|
||||
logging.warning("job failed attachment_id=%s", payload['attachment_id'])
|
||||
if '_claim_time' not in payload:
|
||||
return False, False
|
||||
return False, "failed"
|
||||
try:
|
||||
attachment_id = UUID(payload["attachment_id"])
|
||||
attempt = int(payload.get("attempt", 1))
|
||||
@@ -214,17 +219,16 @@ def main() -> None:
|
||||
asyncio.run(schedule_retry(attachment_id, delay, payload['_claim_time'],
|
||||
str(error) if isinstance(error, PermanentFailure) else None))
|
||||
except Exception:
|
||||
return False, False
|
||||
return True, False
|
||||
return True, True
|
||||
return False, "failed"
|
||||
return True, "failed"
|
||||
return True, outcome
|
||||
|
||||
def handle(body, headers):
|
||||
started = time.monotonic()
|
||||
tel.busy.add(1)
|
||||
with tel.tracer.start_as_current_span("attachment.consume", context=tel.extract(headers), kind=tel.SpanKind.CONSUMER, record_exception=False, set_status_on_exception=False) as span:
|
||||
try:
|
||||
acknowledged, succeeded = handle_job(body)
|
||||
outcome = "success" if succeeded else "failed"
|
||||
acknowledged, outcome = handle_job(body)
|
||||
return acknowledged
|
||||
except Exception:
|
||||
outcome = "failed"
|
||||
|
||||
Reference in New Issue
Block a user