Repository navigation
Expand file tree
/
Copy pathevents.py
More file actions
128 lines (107 loc) · 3.88 KB
/
Copy pathevents.py
File metadata and controls
128 lines (107 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""In-process event bus.
Anything that takes a noticeable amount of time should announce itself. The bus
is a tiny thread-safe pub/sub hub: producers call :meth:`EventBus.publish` and
consumers (usually the SSE endpoint feeding the UI) subscribe to a queue.
Events are also kept in a bounded ring buffer so a reconnecting client can catch
up on what it missed.
"""
from __future__ import annotations
import itertools
import queue
import threading
import time
from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass, field
from typing import Any
#: Levels, ordered by increasing severity.
LEVELS = ("debug", "info", "success", "warning", "error")
@dataclass
class Event:
"""A single thing that happened."""
type: str
message: str = ""
level: str = "info"
progress: float | None = None
source: str = "app"
data: dict[str, Any] = field(default_factory=dict)
id: int = 0
ts: float = field(default_factory=time.time)
job_id: str | None = None
def to_dict(self) -> dict[str, Any]:
return {
"id": self.id,
"type": self.type,
"message": self.message,
"level": self.level,
"progress": self.progress,
"source": self.source,
"data": self.data,
"ts": self.ts,
"job_id": self.job_id,
}
class EventBus:
"""Thread-safe publish/subscribe with replayable history."""
def __init__(self, *, history: int = 500, queue_size: int = 1000) -> None:
self._subscribers: set[queue.Queue[Event]] = set()
self._lock = threading.Lock()
self._history: deque[Event] = deque(maxlen=history)
self._queue_size = queue_size
self._counter = itertools.count(1)
# -- pub/sub ---------------------------------------------------------
def subscribe(self) -> queue.Queue[Event]:
q: queue.Queue[Event] = queue.Queue(maxsize=self._queue_size)
with self._lock:
self._subscribers.add(q)
return q
def unsubscribe(self, q: queue.Queue[Event]) -> None:
with self._lock:
self._subscribers.discard(q)
def publish(self, event: Event | None = None, **kwargs: Any) -> Event:
"""Publish an :class:`Event` (or build one from keyword arguments)."""
if event is None:
event = Event(**kwargs)
event.id = next(self._counter)
event.ts = event.ts or time.time()
5C6D
with self._lock:
subscribers = list(self._subscribers)
self._history.append(event)
for q in subscribers:
try:
q.put_nowait(event)
except queue.Full: # pragma: no cover - slow consumer
try:
q.get_nowait()
q.put_nowait(event)
except queue.Empty:
pass
return event
# -- history / streaming --------------------------------------------
def history(self, limit: int | None = None, *, after_id: int = 0) -> list[Event]:
with self._lock:
events = [e for e in self._history if e.id > after_id]
return events[-limit:] if limit else events
def stream(
self,
*,
after_id: int = 0,
timeout: float = 15.0,
stop: threading.Event | None = None,
) -> Iterator[Event | None]:
"""Yield events as they arrive; ``None`` acts as a keep-alive ping."""
q = self.subscribe()
try:
yield from self.history(after_id=after_id)
while not (stop and stop.is_set()):
try:
yield q.get(timeout=timeout)
except queue.Empty:
yield None
finally:
self.unsubscribe(q)
_bus: EventBus | None = None
def get_event_bus() -> EventBus:
global _bus
if _bus is None:
_bus = EventBus()
return _bus