Two defects that only surface against the real DeepSeek API, both of which break the cost model rather than the code: deepseek-v4-flash enables thinking mode by default. The same single-turn question took 13.5s with it on and 1.8s with it off, and the thinking tokens are billed at the output rate. That misses NFR-PERF-02b (15s per turn) and inflates every conversational call. SceneConfig now carries an explicit `thinking` flag, all four V1 scenes set it to false, and the provider always sends the parameter rather than relying on the upstream default. Reasoning tokens are recorded separately in Usage.tokens_reasoning so the cost of ever turning it back on is visible. DeepSeek moves to peak/off-peak pricing on 2026-08-17, with peak (Beijing 09:00-12:00 and 14:00-18:00) at twice the off-peak rate. Reports are generated mostly during peak hours, so a single blended rate would leave ob_ai_call -- the only input to V2 pricing -- off by nearly a factor of two. PeakAwareRateTable resolves the rate at call time; cost is computed and stored then, never recomputed later at whatever the current rate happens to be. Also here: - Replace the live API key in the redaction test with a synthetic one. The test that proves we redact secrets was itself a secret leak waiting for its first commit. - Correct LLM_COST_RATES_JSON, which held placeholder rates. - Make ids.SnowflakeGenerator take an injectable clock so rollover and clock-rollback are testable without patching a slots class. - Rename exceptions to the -Error suffix (N818) and clear the remaining ruff, mypy and import-linter findings, including two dead lines: a duplicate pytestmark assignment and a no-op assert on a None return. Gates: ruff, mypy (96 files incl. tests), 4/4 import contracts, detect-secrets, 166 unit + 3 integration tests, migration up/down round-trip.
70 lines
2.5 KiB
Python
Executable File
70 lines
2.5 KiB
Python
Executable File
"""Celery task definitions.
|
|
|
|
Only the scaffold's ``ping`` task lives here. It proves the whole async path:
|
|
API creates ``tk_task`` → queue → worker transitions running → succeeded →
|
|
task centre reflects it. Real long-running tasks (report / tryon / matting /
|
|
render) follow exactly this shape and additionally call
|
|
``billing.commit()``/``refund()`` and ``compliance.publish_asset()``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from typing import Any
|
|
|
|
from apps.worker.celery_app import celery_app
|
|
from packages.domain.task.state import TaskStatus
|
|
from packages.shared.context import bind_context
|
|
from packages.shared.db import get_session_factory
|
|
from packages.shared.logging import get_logger
|
|
from packages.task.service import TaskService
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
|
async def _run_ping(task_id: int, echo: str | None = None) -> dict[str, Any]:
|
|
factory = get_session_factory()
|
|
async with factory() as session:
|
|
service = TaskService(session)
|
|
task = await service.transition(
|
|
task_id=task_id, target=TaskStatus.RUNNING, stage_text="执行中"
|
|
)
|
|
payload = task.payload or {}
|
|
message = echo or str(payload.get("echo", "pong"))
|
|
await session.commit()
|
|
|
|
finished = await service.transition(
|
|
task_id=task_id,
|
|
target=TaskStatus.SUCCEEDED,
|
|
stage_text="已完成",
|
|
result={"echo": message},
|
|
)
|
|
await session.commit()
|
|
return {"task_id": str(finished.task_id), "echo": message}
|
|
|
|
|
|
async def _fail_ping(task_id: int, error: str) -> None:
|
|
factory = get_session_factory()
|
|
async with factory() as session:
|
|
service = TaskService(session)
|
|
await service.transition(
|
|
task_id=task_id,
|
|
target=TaskStatus.FAILED,
|
|
stage_text="执行失败",
|
|
error_message=error,
|
|
)
|
|
await session.commit()
|
|
|
|
|
|
# Celery 的 task 装饰器没有类型标注,会把被装饰函数擦成 untyped
|
|
@celery_app.task(name="meida.task.ping", bind=True, max_retries=2) # type: ignore[untyped-decorator]
|
|
def ping_task(self: Any, task_id: int, echo: str | None = None) -> dict[str, Any]:
|
|
bind_context(task_id=task_id)
|
|
try:
|
|
return asyncio.run(_run_ping(task_id, echo))
|
|
except Exception as exc:
|
|
logger.exception("ping_task_failed", task_id=task_id)
|
|
# Real tasks additionally refund the quota reservation here (FR-BILL-05).
|
|
asyncio.run(_fail_ping(task_id, f"{type(exc).__name__}: {exc}"))
|
|
raise
|