mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-22 21:32:26 +01:00
The tutor's generic relay transport (layout rules, sealer, REST client, SSE, mirror, unlock) moved here unchanged in format, plus a generic RequestServer (kind allowlist, sealed-only requests, lease, visible handler failures) and a presence heartbeat for a second consumer (todoImplementer). The tutor's contract vectors pin the byte format. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
40 lines
1.1 KiB
Python
40 lines
1.1 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki. MIT License.
|
|
"""The worker's heartbeat at ``presence/workers/{worker}``.
|
|
|
|
Clients judge the worker alive from when beats stop arriving (``beat`` is an
|
|
RTDB server timestamp, kept clear beside the sealed unit), never by
|
|
comparing it to their own clock.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import socket
|
|
from typing import TYPE_CHECKING, Final
|
|
|
|
from rtdb_relay.rtdb import SERVER_TIME
|
|
|
|
if TYPE_CHECKING:
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
from rtdb_relay.rtdb import Rtdb
|
|
|
|
BEAT_S: Final = 30.0
|
|
|
|
|
|
def node(version: str) -> dict[str, object]:
|
|
"""One beat: server time, the host and the worker's build."""
|
|
return {"beat": SERVER_TIME, "host": socket.gethostname(), "version": version}
|
|
|
|
|
|
async def beat_forever(
|
|
rtdb: Rtdb,
|
|
worker: str,
|
|
version: str,
|
|
sleep: Callable[[float], Awaitable[None]] = asyncio.sleep,
|
|
) -> None:
|
|
"""Write a beat every :data:`BEAT_S` until cancelled."""
|
|
while True:
|
|
await rtdb.put(f"presence/workers/{worker}", node(version))
|
|
await sleep(BEAT_S)
|