56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
"""履约状态 Outbox 写入。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from typing import Any
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ...models import (
|
||
PlatformFulfillmentTask,
|
||
)
|
||
from .events import publish_after_commit
|
||
from .order_site_notifier import (
|
||
binding_state_hash,
|
||
enqueue_order_site_binding_state,
|
||
)
|
||
from .views import customer_binding_out
|
||
|
||
|
||
def _emit_order_site_binding_state(
|
||
db: Session,
|
||
task_id: int,
|
||
*,
|
||
extra: dict[str, Any] | None = None,
|
||
) -> bool:
|
||
"""当前事务内写 Outbox;HTTP 投递由独立 worker 完成。"""
|
||
task = (
|
||
db.query(PlatformFulfillmentTask)
|
||
.filter(PlatformFulfillmentTask.id == int(task_id))
|
||
.with_for_update()
|
||
.one()
|
||
)
|
||
payload = customer_binding_out(db, task_id)
|
||
if extra:
|
||
payload.update(extra)
|
||
state_hash = binding_state_hash(payload)
|
||
if not extra and (task.last_emitted_state_hash or "") == state_hash:
|
||
return False
|
||
|
||
next_state_version = int(task.state_version or 0) + 1
|
||
payload["stateVersion"] = next_state_version
|
||
enqueued = enqueue_order_site_binding_state(
|
||
db,
|
||
payload,
|
||
)
|
||
if enqueued:
|
||
task.state_version = next_state_version
|
||
task.last_emitted_state_hash = state_hash
|
||
db.flush()
|
||
publish_after_commit(
|
||
db,
|
||
scope="tasks",
|
||
action="binding_state_emitted",
|
||
)
|
||
return enqueued
|