History is kept by flow id: runs, cascades, rollups and events file the id in their flow column and nodes as <flow id>.<node>, the API answers flow plus flow_name and shows and takes nodes as <key>.<node>; events carry flow_id from the moment they are published; run queue items, the stage cache and cached curves keep ids and keys apart; endpoints, the brain graph, device bindings and search name the id to open
This commit is contained in:
@@ -13,7 +13,7 @@ import asyncio
|
||||
import logging
|
||||
import threading
|
||||
from collections import deque
|
||||
from collections.abc import AsyncIterator
|
||||
from collections.abc import AsyncIterator, Callable
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any
|
||||
|
||||
@@ -118,6 +118,11 @@ class EventBus:
|
||||
#: Subscribers that lost an event since they last asked. Touched on
|
||||
#: the event loop only, by the flush and the subscribers.
|
||||
self._dropped: set[asyncio.Queue[dict[str, Any]]] = set()
|
||||
#: The id of the flow holding a key, once the store exists. An event
|
||||
#: naming a flow by its key gets its ``flow_id`` here, on the way in,
|
||||
#: so what holds the event later is not left to look up a key that
|
||||
#: may have moved on by then.
|
||||
self.flow_id_of: Callable[[str], str] | None = None
|
||||
|
||||
def bind(self, loop: asyncio.AbstractEventLoop) -> None:
|
||||
"""Attach the bus to the running event loop (called once at startup)."""
|
||||
@@ -125,6 +130,11 @@ class EventBus:
|
||||
|
||||
def publish(self, event: dict[str, Any]) -> None:
|
||||
"""Publish an event from any thread."""
|
||||
resolve = self.flow_id_of
|
||||
if resolve is not None and "flow" in event and "flow_id" not in event:
|
||||
# An event about the engine as a whole names no flow, and no id.
|
||||
flow = str(event["flow"] or "")
|
||||
event["flow_id"] = resolve(flow) if flow else ""
|
||||
kind = event.get("type")
|
||||
if kind == "node_log":
|
||||
# deque.append is atomic, so worker threads need no lock here.
|
||||
|
||||
Reference in New Issue
Block a user