Поправили заполлнение БД + поправили гонку сообщений при отправке в Битрикс

This commit is contained in:
mi
2026-07-17 11:46:12 +03:00
parent 7457bc5c0b
commit d1a0f3791d
13 changed files with 562 additions and 84 deletions
+17 -3
View File
@@ -159,7 +159,7 @@ def retry_delay(attempt: int, maximum: int) -> float:
def safely_retryable(exc: Exception) -> bool:
return isinstance(exc, httpx.ConnectError) or (
return isinstance(exc, (httpx.ConnectError, httpx.TimeoutException)) or (
isinstance(exc, httpx.HTTPStatusError) and exc.response.status_code in {429, 502, 503, 504}
)
@@ -721,6 +721,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
request_fingerprint=fp,
payload_json=body,
status="sending",
lease_until=now() + timedelta(seconds=cfg.bitrix_http_timeout_sec + 5),
)
session.add(row)
try:
@@ -775,6 +776,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
else:
current.status = "ambiguous"
current.last_error_code = "bitrix_delivery_ambiguous"
current.lease_until = None
await session.commit()
raise HTTPException(
503,
@@ -1101,7 +1103,12 @@ async def reconcile_setup(app: FastAPI) -> dict[str, bool]:
return result
async def claim_one(session: AsyncSession, model, statuses: list[str]):
async def claim_one(
session: AsyncSession,
model,
statuses: list[str],
claimed_status: str | None = None,
):
row = await session.scalar(
select(model)
.where(
@@ -1114,6 +1121,8 @@ async def claim_one(session: AsyncSession, model, statuses: list[str]):
.limit(1)
)
if row:
if claimed_status is not None:
row.status = claimed_status
row.lease_until = now() + timedelta(seconds=30)
row.attempt_count += 1
await session.commit()
@@ -1239,7 +1248,12 @@ async def process_ack(app: FastAPI) -> None:
async def process_outbound(app: FastAPI) -> None:
async with app.state.sessions() as session:
row = await claim_one(session, OutboundMessage, ["retry"])
row = await claim_one(
session,
OutboundMessage,
["retry"],
claimed_status="sending",
)
if not row:
return
try: