perf(fulfillment): 修复 Outbox 状态版本去重、跨进程实时推送与数据库默认配置
This commit is contained in:
@@ -2,14 +2,23 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from fastapi import (
|
||||
APIRouter,
|
||||
Depends,
|
||||
HTTPException,
|
||||
Query,
|
||||
WebSocket,
|
||||
WebSocketDisconnect,
|
||||
)
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..database import get_db
|
||||
from ..deps import require_permission
|
||||
from ..deps import authenticate_websocket, require_permission
|
||||
from ..models import User
|
||||
from ..permissions import get_user_permissions
|
||||
from ..schemas import (
|
||||
PlatformFulfillmentCollabSyncRequest,
|
||||
PlatformFulfillmentCompleteRequest,
|
||||
@@ -24,10 +33,38 @@ from ..services import douyu_collab_service
|
||||
from ..services.audit_service import record_audit
|
||||
from ..services.douyu_collab_events import collab_events
|
||||
from ..services.platform_fulfillment import service
|
||||
from ..services.platform_fulfillment.events import (
|
||||
ensure_worker_event_relay,
|
||||
platform_fulfillment_events,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix="/api/platform-fulfillment", tags=["直播账号履约"])
|
||||
|
||||
|
||||
@router.websocket("/ws")
|
||||
async def ws_platform_fulfillment(websocket: WebSocket):
|
||||
"""直播账号履约工作台实时失效通知:前端收到后静默重拉列表与详情。"""
|
||||
user = authenticate_websocket(websocket)
|
||||
if not user or "platform_fulfillment:view" not in get_user_permissions(user):
|
||||
await websocket.close(code=1008, reason="未授权")
|
||||
return
|
||||
await websocket.accept()
|
||||
queue = platform_fulfillment_events.subscribe()
|
||||
ensure_worker_event_relay()
|
||||
try:
|
||||
await websocket.send_json(platform_fulfillment_events.ready_event())
|
||||
while True:
|
||||
try:
|
||||
event = await asyncio.wait_for(queue.get(), timeout=25)
|
||||
await websocket.send_json(event)
|
||||
except TimeoutError:
|
||||
await websocket.send_json(platform_fulfillment_events.ping_event())
|
||||
except (WebSocketDisconnect, RuntimeError):
|
||||
pass
|
||||
finally:
|
||||
platform_fulfillment_events.unsubscribe(queue)
|
||||
|
||||
|
||||
@router.get("/settings")
|
||||
def get_settings(
|
||||
db: Session = Depends(get_db),
|
||||
@@ -132,6 +169,11 @@ def sync_tasks(
|
||||
)
|
||||
|
||||
invalidate_task_list_count_cache()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.sync",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -587,6 +629,11 @@ def complete_task(
|
||||
detail={"note": req.note},
|
||||
)
|
||||
db.commit()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.complete",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -607,6 +654,11 @@ def release_task(
|
||||
detail={},
|
||||
)
|
||||
db.commit()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.release",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -631,6 +683,11 @@ def release_task_to_manual(
|
||||
detail={"reason": req.reason},
|
||||
)
|
||||
db.commit()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.release_manual",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -655,6 +712,11 @@ def mark_manual(
|
||||
detail={"reason": req.reason},
|
||||
)
|
||||
db.commit()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.manual",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -679,6 +741,11 @@ def cancel_task(
|
||||
detail={"reason": req.reason},
|
||||
)
|
||||
db.commit()
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.cancel",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
@@ -708,6 +775,11 @@ def restore_cancelled_task(
|
||||
action="order.restore_from_platform_fulfillment",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
platform_fulfillment_events.publish(
|
||||
scope="tasks",
|
||||
action="task.restore",
|
||||
actor_id=int(current.id),
|
||||
)
|
||||
from ..services.platform_fulfillment.auto_allocator import wake_auto_allocator
|
||||
|
||||
wake_auto_allocator([task_id])
|
||||
|
||||
Reference in New Issue
Block a user