perf(douyu): 拆分任务槽并改造绑定扫码调度
This commit is contained in:
@@ -14,9 +14,10 @@ from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ...database import SessionLocal
|
||||
from ...database import WorkerSessionLocal
|
||||
from ...models import IntegrationOutbox
|
||||
from .security import build_webhook_signature
|
||||
|
||||
@@ -74,19 +75,29 @@ def enqueue_integration_event(
|
||||
if existing is not None:
|
||||
return False
|
||||
|
||||
db.add(
|
||||
IntegrationOutbox(
|
||||
event_id=event_id,
|
||||
destination=destination,
|
||||
event_type=event_type,
|
||||
dedupe_key=normalized_dedupe,
|
||||
payload_json=body,
|
||||
status="pending",
|
||||
attempts=0,
|
||||
next_attempt_at=datetime.now(UTC),
|
||||
created_at=datetime.now(UTC),
|
||||
)
|
||||
)
|
||||
try:
|
||||
with db.begin_nested():
|
||||
db.add(
|
||||
IntegrationOutbox(
|
||||
event_id=event_id,
|
||||
destination=destination,
|
||||
event_type=event_type,
|
||||
dedupe_key=normalized_dedupe,
|
||||
payload_json=body,
|
||||
status="pending",
|
||||
attempts=0,
|
||||
next_attempt_at=datetime.now(UTC),
|
||||
created_at=datetime.now(UTC),
|
||||
)
|
||||
)
|
||||
db.flush()
|
||||
except IntegrityError:
|
||||
# A concurrent transaction may have inserted the same stable dedupe key
|
||||
# after our pre-check. The nested transaction keeps the outer business
|
||||
# transaction usable and treats the event as already enqueued.
|
||||
if normalized_dedupe:
|
||||
return False
|
||||
raise
|
||||
return True
|
||||
|
||||
|
||||
@@ -118,7 +129,7 @@ def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
|
||||
|
||||
|
||||
def _claim_due_events(limit: int) -> list[_ClaimedEvent]:
|
||||
db = SessionLocal()
|
||||
db = WorkerSessionLocal()
|
||||
now = datetime.now(UTC)
|
||||
try:
|
||||
(
|
||||
@@ -202,7 +213,7 @@ def _deliver(event: _ClaimedEvent, base_url: str, integration_key: str) -> None:
|
||||
|
||||
|
||||
def _mark_sent(event_id: str) -> None:
|
||||
db = SessionLocal()
|
||||
db = WorkerSessionLocal()
|
||||
try:
|
||||
(
|
||||
db.query(IntegrationOutbox)
|
||||
@@ -229,7 +240,7 @@ def _mark_failed(event: _ClaimedEvent, error: Exception) -> None:
|
||||
)
|
||||
dead = attempts >= max_attempts
|
||||
message = str(error)[:2000]
|
||||
db = SessionLocal()
|
||||
db = WorkerSessionLocal()
|
||||
try:
|
||||
(
|
||||
db.query(IntegrationOutbox)
|
||||
|
||||
Reference in New Issue
Block a user