Retry ETTN creation on Nova Poshta rate limit instead of failing
Checkbox relays Nova Poshta's "To many requests" (20000401501) as a 4xx third_party.generic error, not a 429, so bursts of receipt requests ended up as failed receipts. Such responses are now CheckboxRateLimitedError: the receipt stays pending and the worker job is retried with arq.Retry after the "Try again after N seconds" delay plus backoff. NP timeouts relayed the same way are treated as unavailable (unknown outcome). The HTTP client also sends Checkbox requests one at a time with a CHECKBOX_MIN_REQUEST_INTERVAL_MS pause and signs the cashier in once for concurrent jobs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -38,6 +38,7 @@ from app.services.checkbox.client import (
|
||||
CheckboxClient,
|
||||
CheckboxCredentials,
|
||||
CheckboxError,
|
||||
CheckboxRateLimitedError,
|
||||
CheckboxUnavailableError,
|
||||
)
|
||||
from app.services.crm.client import CrmClient, CrmError
|
||||
@@ -387,7 +388,10 @@ def _apply_ettn(receipt: Receipt, ettn: EttnOut) -> None:
|
||||
async def create_ettn_for_receipt(
|
||||
session: AsyncSession, client: CheckboxClient, receipt_id: uuid.UUID
|
||||
) -> Receipt | None:
|
||||
"""Отправляет `pending`-чек в Checkbox. Идемпотентна — безопасно вызывать повторно."""
|
||||
"""Отправляет `pending`-чек в Checkbox. Идемпотентна — безопасно вызывать повторно.
|
||||
|
||||
На лимит частоты пробрасывает `CheckboxRateLimitedError`, чек остаётся `pending`.
|
||||
"""
|
||||
receipt = await session.get(Receipt, receipt_id, with_for_update=True)
|
||||
if receipt is None or receipt.status != ReceiptStatus.PENDING:
|
||||
return receipt
|
||||
@@ -403,6 +407,12 @@ async def create_ettn_for_receipt(
|
||||
await client.find_ettn(creds, receipt.waybill_number) if receipt.error else None
|
||||
)
|
||||
ettn = existing or await client.create_ettn(creds, receipt.request_body)
|
||||
except CheckboxRateLimitedError as exc:
|
||||
# Запрос отклонён, чек точно не создан: остаётся `pending` без пометки
|
||||
# «исход неизвестен». Повтор с задержкой — забота вызывающего (worker).
|
||||
await session.commit() # снять блокировку строки
|
||||
log.info("ettn_rate_limited", receipt_id=str(receipt_id), retry_after=exc.retry_after)
|
||||
raise
|
||||
except CheckboxUnavailableError as exc:
|
||||
receipt.error = str(exc)
|
||||
await session.commit()
|
||||
@@ -435,7 +445,10 @@ async def retry_pending_receipts(session: AsyncSession, client: CheckboxClient)
|
||||
)
|
||||
)
|
||||
for receipt_id in receipt_ids:
|
||||
await create_ettn_for_receipt(session, client, receipt_id)
|
||||
try:
|
||||
await create_ettn_for_receipt(session, client, receipt_id)
|
||||
except CheckboxRateLimitedError:
|
||||
break # остальные — в следующем проходе cron'а
|
||||
|
||||
|
||||
# --- Отмена и опрос ----------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user