上一版用 `manager.polling()` 判"有没有人在收消息",那是进程内状态:请求落到持有者 回答 true、落到备用进程回答 false,页面上就会每两秒闪一次"已停止"。 改成查数据库里的 advisory lock(`pg_locks`,objsubid=2 的两段式锁),谁抢到租约所有 进程都看得见。`lease_held()` 自己开一条短会话查,不复用请求会话——接口里常在 `await session.commit()` 之后才拼返回值,那时原事务已经关了。
This commit is contained in:
@@ -377,3 +377,17 @@ async def test_startup_sees_logged_in_channels(client: AsyncClient) -> None:
|
||||
|
||||
ids = await wechat_bot.active_channel_ids()
|
||||
assert channel_id in ids, "启动时看不见已登录通道,重启后机器人就静默失联了"
|
||||
|
||||
|
||||
async def test_polling_flag_is_visible_to_every_process() -> None:
|
||||
"""页面的"接收中"要跨进程一致:看数据库里的锁,而不是本进程的任务表。"""
|
||||
channel_id = uuid.uuid4()
|
||||
assert await wechat_bot.lease_held(channel_id) is False
|
||||
|
||||
lease = await wechat_bot.acquire_lease(channel_id)
|
||||
assert lease is not None
|
||||
try:
|
||||
assert await wechat_bot.lease_held(channel_id) is True
|
||||
finally:
|
||||
await lease.close()
|
||||
assert await wechat_bot.lease_held(channel_id) is False
|
||||
|
||||
Reference in New Issue
Block a user