thzdaq-daemon 0.0.6b4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """ThzDAQ bench daemon: composition of core use cases and their HTTP + SSE transport."""
@@ -0,0 +1,69 @@
1
+ """python -m thzdaq_daemon (thzdaq-daemon; a built ThzDAQ.exe --serve): the bench
2
+ daemon. Clients start it themselves (thzdaq_api.host.connect_or_start); by hand
3
+ it runs in the terminal with its log there."""
4
+
5
+ import argparse
6
+ import logging
7
+ import sys
8
+ from pathlib import Path
9
+ from typing import List, Optional
10
+
11
+ from thzdaq_api.host import DaemonFiles, program_dir
12
+
13
+ from thzdaq_daemon.process import Daemon, Options
14
+
15
+
16
+ def main(argv: Optional[List[str]] = None) -> int:
17
+ parser = argparse.ArgumentParser(
18
+ prog="thzdaq-daemon", description="ThzDAQ bench daemon"
19
+ )
20
+ parser.add_argument(
21
+ "--home",
22
+ help="its folder: daemon.json and data/ (default: the program's folder)",
23
+ )
24
+ parser.add_argument(
25
+ "--station",
26
+ help="the bench profile to work with: a YAML file, or a name in stations/",
27
+ )
28
+ parser.add_argument("--data", help="measurements folder (default: data/ in home)")
29
+ parser.add_argument(
30
+ "--simulate", action="store_true", help="the simulator daemon: no instruments"
31
+ )
32
+ parser.add_argument("--port", type=int, default=0, help="0: any free port")
33
+ parser.add_argument(
34
+ "--idle-timeout",
35
+ type=float,
36
+ help="seconds without clients and measurements before it ends; 0: never",
37
+ )
38
+ args = parser.parse_args(argv)
39
+ files = DaemonFiles.of(args.simulate)
40
+ streams_to_log(files)
41
+ logging.basicConfig(
42
+ level=logging.INFO,
43
+ # Lanes and runs are threads: the log names them
44
+ format="%(asctime)s %(levelname)s [%(threadName)s] %(name)s: %(message)s",
45
+ stream=sys.stderr,
46
+ )
47
+ options = Options(
48
+ home=Path(args.home) if args.home else program_dir(),
49
+ files=files,
50
+ station=args.station,
51
+ data=Path(args.data) if args.data else None,
52
+ simulate=args.simulate,
53
+ port=args.port,
54
+ idle_timeout=args.idle_timeout,
55
+ )
56
+ return Daemon(options).run()
57
+
58
+
59
+ def streams_to_log(files: DaemonFiles) -> None:
60
+ """A windowed build (ThzDAQ.exe --serve) started by hand has no stdout and
61
+ stderr: what it says goes to its log, as when a client starts it."""
62
+ if sys.stderr is not None:
63
+ return
64
+ files.directory.mkdir(parents=True, exist_ok=True)
65
+ sys.stdout = sys.stderr = open(files.log, "a", encoding="utf-8", buffering=1)
66
+
67
+
68
+ if __name__ == "__main__":
69
+ sys.exit(main())
@@ -0,0 +1,69 @@
1
+ """The daemon's composition root: the station of a profile on the clock of this
2
+ process, what it keeps of its instruments (states, its state.json), the lanes
3
+ of its instruments, the workspaces with a measurement store
4
+ each, the bench's own measurements (measurements/ of home), and the use cases
5
+ with the daemon's process as their DaemonControl."""
6
+
7
+ import time
8
+ from dataclasses import dataclass
9
+ from pathlib import Path
10
+ from typing import Optional
11
+
12
+ from thzdaq_api.custom import LIBRARY
13
+ from thzdaq_core.adapters.instrument_states import JsonInstrumentStates
14
+ from thzdaq_core.adapters.json_store import MeasurementStore
15
+ from thzdaq_core.adapters.library import FolderLibrary
16
+ from thzdaq_core.adapters.processes import Processes
17
+ from thzdaq_core.adapters.yaml_profiles import YamlProfiles, load_station
18
+ from thzdaq_core.application.assembly import Assembled, assemble
19
+ from thzdaq_core.application.bench import Bench
20
+ from thzdaq_core.application.kept import KeptStates
21
+ from thzdaq_api.lanes import Lanes
22
+ from thzdaq_core.application.ports import DaemonControl
23
+ from thzdaq_core.application.workspaces import Workspaces
24
+ from thzdaq_core.domain.station.config import StationConfig
25
+ from thzdaq_core.domain.station.station import Station
26
+
27
+ UNNAMED = "bench" # the profile of a daemon started without one
28
+
29
+
30
+ @dataclass
31
+ class Composed:
32
+ bench: Bench
33
+ lanes: Lanes
34
+ assembled: Assembled
35
+
36
+ def close(self) -> None:
37
+ self.assembled.close()
38
+ self.lanes.stop()
39
+ self.bench.station.close()
40
+
41
+
42
+ def compose(
43
+ station: Optional[str],
44
+ home: Path,
45
+ data: Path,
46
+ simulate: bool,
47
+ control: DaemonControl,
48
+ states: Path,
49
+ lively: bool = True,
50
+ ) -> Composed:
51
+ """station: a profile's file, or a name of home's stations/; home: the
52
+ daemon's own workspace, its measurements in data; states: the file of
53
+ what it keeps of its instruments; lively: simulators drift and get noisy
54
+ as a bench does (tests keep exact values)."""
55
+ workspaces = Workspaces(home, data, MeasurementStore)
56
+ path = workspaces.profile(station) if station else None
57
+ config = load_station(path) if path else StationConfig(UNNAMED)
58
+ kept = KeptStates(JsonInstrumentStates(states))
59
+ bench_station = Station(
60
+ config, pause=time.sleep, simulate=simulate, lively=simulate and lively,
61
+ on_open=kept.opened,
62
+ ) # fmt: skip
63
+ lanes = Lanes()
64
+ bench = Bench(bench_station, lanes, path)
65
+ assembled = assemble(
66
+ bench, workspaces, YamlProfiles(), control, Processes(),
67
+ FolderLibrary(workspaces.home / LIBRARY),
68
+ ) # fmt: skip
69
+ return Composed(bench, lanes, assembled)
@@ -0,0 +1,286 @@
1
+ """The daemon's process: one per machine (the simulator's is another), found by
2
+ its host file, ended by a stop, an idle timeout or a signal.
3
+
4
+ It lives while clients listen, measurements or scripts run; after the last of them it
5
+ waits idle_timeout seconds (0: forever) and ends. A client that finds it gone
6
+ starts it again (thzdaq_api.host.connect_or_start).
7
+ """
8
+
9
+ import logging
10
+ import os
11
+ import secrets
12
+ import signal
13
+ import sys
14
+ import threading
15
+ import time
16
+ from collections import deque
17
+ from dataclasses import dataclass
18
+ from pathlib import Path
19
+ from typing import Deque, Dict, List, Optional, Tuple
20
+
21
+ import msgspec
22
+ import psutil
23
+ from thzdaq_api.daemon import (
24
+ API_VERSION,
25
+ ClientInfo,
26
+ DaemonSettings,
27
+ Health,
28
+ HostInfo,
29
+ ProcessInfo,
30
+ )
31
+ from thzdaq_api.events import StationChanged
32
+ from thzdaq_api.host import (
33
+ DaemonFiles,
34
+ app_version,
35
+ log_tail,
36
+ remove_host,
37
+ spawn,
38
+ start_command,
39
+ write_host,
40
+ )
41
+ from thzdaq_api.lock import FileLock
42
+ from thzdaq_api.operation import Rights
43
+ from thzdaq_api.rights import Decision
44
+ from thzdaq_api.transport import HEARTBEAT_S
45
+ from thzdaq_core.application.clock import wall_time
46
+ from thzdaq_api.measurements import DATA
47
+
48
+ from thzdaq_daemon.compose import Composed, compose
49
+ from thzdaq_daemon.server import Clients, Server
50
+
51
+ logger = logging.getLogger(__name__)
52
+
53
+ IDLE_S = 600.0 # default idle timeout
54
+ LOCK_S = 10.0 # a restarted daemon waits this long for the old one to let go
55
+ ERRORS = 20 # the last errors of its log its status tells
56
+
57
+
58
+ class Settings(msgspec.Struct):
59
+ """What the daemon keeps between runs, in its home folder (daemon.json; the
60
+ simulator's in daemon-sim.json: its profile is not the bench's)."""
61
+
62
+ station: Optional[str] = None # the last profile it worked with
63
+ idle_timeout: float = IDLE_S
64
+ agents: Dict[Rights, Decision] = {} # what agents may do, as a person set it
65
+
66
+ @classmethod
67
+ def load(cls, path: Path) -> "Settings":
68
+ try:
69
+ return msgspec.json.decode(path.read_bytes(), type=cls)
70
+ except FileNotFoundError:
71
+ return cls()
72
+
73
+ def save(self, path: Path) -> None:
74
+ partial = path.with_suffix(".partial")
75
+ partial.write_bytes(msgspec.json.format(msgspec.json.encode(self)))
76
+ os.replace(partial, path)
77
+
78
+
79
+ @dataclass
80
+ class Options:
81
+ home: Path # daemon.json; data/ unless given
82
+ files: DaemonFiles # the real daemon's or the simulator's
83
+ station: Optional[str] = None
84
+ data: Optional[Path] = None
85
+ simulate: bool = False
86
+ port: int = 0
87
+ idle_timeout: Optional[float] = None # None: as daemon.json says
88
+ idle_check: float = HEARTBEAT_S # how often it looks whether it is idle
89
+ lively: bool = True
90
+
91
+ @property
92
+ def settings_path(self) -> Path:
93
+ return self.home / ("daemon-sim.json" if self.simulate else "daemon.json")
94
+
95
+ @property
96
+ def state_path(self) -> Path:
97
+ """What it keeps of its instruments (a grid's angle): its own file."""
98
+ return self.home / ("state-sim.json" if self.simulate else "state.json")
99
+
100
+ def arguments(self, station: Optional[str]) -> List[str]:
101
+ """How to start a daemon like this one again."""
102
+ found = ["--home", str(self.home), "--port", "0"]
103
+ if station:
104
+ found += ["--station", station]
105
+ if self.data is not None:
106
+ found += ["--data", str(self.data)]
107
+ if self.simulate:
108
+ found.append("--simulate")
109
+ if self.idle_timeout is not None:
110
+ found += ["--idle-timeout", str(self.idle_timeout)]
111
+ return found
112
+
113
+
114
+ class RecentErrors(logging.Handler):
115
+ """The last errors logged in the process, a line each, for its status: a
116
+ daemon tells what went wrong without anyone opening its log."""
117
+
118
+ def __init__(self, size: int = ERRORS):
119
+ super().__init__(logging.ERROR)
120
+ self.recent: Deque[str] = deque(maxlen=size)
121
+
122
+ def emit(self, record: logging.LogRecord) -> None:
123
+ when = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(record.created))
124
+ line = f"{when} {record.name}: {record.getMessage()}"
125
+ error = record.exc_info[1] if record.exc_info else None
126
+ if error is not None:
127
+ line += f" ({type(error).__name__}: {error})"
128
+ self.recent.append(line)
129
+
130
+
131
+ class Daemon:
132
+ """Implements the DaemonControl port for the use cases."""
133
+
134
+ def __init__(self, options: Options):
135
+ self.options = options
136
+ self.saved = Settings.load(options.settings_path)
137
+ self.started = wall_time()
138
+ self.listening = Clients()
139
+ self.server: Optional[Server] = None
140
+ self.composed: Optional[Composed] = None
141
+ self.restart = False
142
+ self.errors = RecentErrors()
143
+ self._stopping = threading.Event()
144
+
145
+ @property
146
+ def idle_timeout(self) -> float:
147
+ given = self.options.idle_timeout
148
+ return self.saved.idle_timeout if given is None else given
149
+
150
+ def run(self) -> int:
151
+ files = self.options.files
152
+ lock = FileLock(files.lock)
153
+ if not lock.acquire(LOCK_S):
154
+ logger.error(f"Another daemon runs (it holds {files.lock})")
155
+ return 1
156
+ logging.getLogger().addHandler(self.errors)
157
+ try:
158
+ self._serve()
159
+ finally:
160
+ logging.getLogger().removeHandler(self.errors)
161
+ lock.release()
162
+ if self.restart:
163
+ station = self.composed.bench.path if self.composed else None
164
+ spawn(start_command() + self.options.arguments(station), files)
165
+ return 0
166
+
167
+ def _serve(self) -> None:
168
+ options = self.options
169
+ station = options.station or self.saved.station
170
+ data = options.data or options.home / DATA
171
+ logger.info(
172
+ f"Starting (pid {os.getpid()}, station {station}, data {data}"
173
+ f"{' on simulators' if options.simulate else ''})"
174
+ )
175
+ self.composed = compose(
176
+ station, options.home, data, options.simulate, self,
177
+ options.state_path, options.lively,
178
+ ) # fmt: skip
179
+ station = self.composed.bench.path # a name given is a file of home
180
+ if station:
181
+ self.saved.station = station
182
+ self.saved.save(options.settings_path)
183
+ assembled = self.composed.assembled
184
+ assembled.events.subscribe(self._remember)
185
+ self.server = Server(
186
+ assembled.dispatcher,
187
+ assembled.events,
188
+ assembled.daemon,
189
+ secrets.token_urlsafe(32),
190
+ Health(os.getpid(), API_VERSION, app_version(), options.simulate),
191
+ self.listening,
192
+ options.port,
193
+ )
194
+ serving = threading.Thread(
195
+ target=self.server.serve_forever, name="http", daemon=True
196
+ )
197
+ serving.start()
198
+ write_host(options.files, self._host(station))
199
+ logger.info(f"Serving on port {self.server.port}")
200
+ self._on_signals()
201
+ watching = threading.Thread(target=self._watch, name="idle", daemon=True)
202
+ watching.start()
203
+ try:
204
+ self._stopping.wait()
205
+ finally:
206
+ logger.info("Stopping")
207
+ remove_host(options.files, os.getpid())
208
+ self.server.stop()
209
+ self.composed.close()
210
+ logger.info("Stopped")
211
+
212
+ def _host(self, station: Optional[str]) -> HostInfo:
213
+ return HostInfo(
214
+ pid=os.getpid(),
215
+ port=self.server.port,
216
+ token=self.server.token,
217
+ api_version=API_VERSION,
218
+ app_version=app_version(),
219
+ executable=sys.executable,
220
+ started=self.started,
221
+ station=station,
222
+ simulate=self.options.simulate,
223
+ )
224
+
225
+ def _on_signals(self) -> None:
226
+ if threading.current_thread() is not threading.main_thread():
227
+ return # a daemon in a thread (tests) is stopped by its owner
228
+ for name in ("SIGTERM", "SIGINT", "SIGBREAK"):
229
+ if hasattr(signal, name):
230
+ signal.signal(getattr(signal, name), lambda *_: self.stop(False))
231
+
232
+ def _watch(self) -> None:
233
+ """End after idle_timeout with no client and no measurement."""
234
+ quiet_since: Optional[float] = None
235
+ while not self._stopping.wait(self.options.idle_check):
236
+ timeout = self.idle_timeout
237
+ assembled = self.composed.assembled
238
+ busy = (
239
+ assembled.runs.active()
240
+ or assembled.scripts.running()
241
+ or not self.listening.idle_for()
242
+ )
243
+ if busy or timeout <= 0:
244
+ quiet_since = None
245
+ continue
246
+ now = time.monotonic()
247
+ quiet_since = now if quiet_since is None else quiet_since
248
+ if now - quiet_since >= timeout:
249
+ logger.info(f"Idle for {timeout:g} s: stopping")
250
+ self.stop(False)
251
+
252
+ def _remember(self, event) -> None:
253
+ """The profile worked with last is where the next start begins."""
254
+ if isinstance(event.body, StationChanged) and event.body.path:
255
+ self.saved.station = event.body.path
256
+ self.saved.save(self.options.settings_path)
257
+
258
+ # DaemonControl
259
+ def process(self) -> ProcessInfo:
260
+ return ProcessInfo(
261
+ os.getpid(), API_VERSION, app_version(), sys.executable, self.started,
262
+ self.server.port if self.server else 0, self.options.simulate,
263
+ memory=psutil.Process().memory_info().rss,
264
+ threads=threading.active_count(),
265
+ errors=tuple(self.errors.recent),
266
+ ) # fmt: skip
267
+
268
+ def clients(self) -> Tuple[ClientInfo, ...]:
269
+ return self.listening.all()
270
+
271
+ def stop(self, restart: bool) -> None:
272
+ self.restart = restart
273
+ self._stopping.set()
274
+
275
+ def settings(self) -> DaemonSettings:
276
+ return DaemonSettings(self.idle_timeout, dict(self.saved.agents))
277
+
278
+ def change(self, settings: DaemonSettings) -> None:
279
+ if settings.idle_timeout != self.idle_timeout:
280
+ self.saved.idle_timeout = settings.idle_timeout
281
+ self.options.idle_timeout = None # what it was started with gives way
282
+ self.saved.agents = dict(settings.agents)
283
+ self.saved.save(self.options.settings_path)
284
+
285
+ def log(self, lines: int) -> Tuple[str, ...]:
286
+ return tuple(log_tail(self.options.files, lines).splitlines())
@@ -0,0 +1,242 @@
1
+ """HTTP + SSE over the dispatcher, on 127.0.0.1 only.
2
+
3
+ POST /ops/<name> calls an operation (JSON in, JSON out, a Failure with an HTTP
4
+ status when it fails; X-Thzdaq-Seq names the last event when it ended).
5
+ GET /events streams the events from Last-Event-ID (or from now), with a
6
+ heartbeat when nothing happens. GET /health and /status tell about the daemon.
7
+
8
+ Every request carries the token of the host file; one with a foreign Host
9
+ (DNS rebinding) or with a browser's Origin is refused: a web page open on the
10
+ bench computer must not set a bias.
11
+ """
12
+
13
+ import hmac
14
+ import logging
15
+ import threading
16
+ import time
17
+ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
18
+ from typing import Dict, List, Optional, Tuple
19
+
20
+ import msgspec
21
+ from thzdaq_api import wire
22
+ from thzdaq_api.daemon import ClientInfo, Health
23
+ from thzdaq_api.errors import ErrorCode, Failure, LabError
24
+ from thzdaq_api.events import Event
25
+ from thzdaq_api.transport import (
26
+ CLIENT,
27
+ EVENTS,
28
+ HEALTH,
29
+ HEARTBEAT,
30
+ HEARTBEAT_S,
31
+ JSON,
32
+ LAST_EVENT,
33
+ LOCALHOST,
34
+ OPERATIONS,
35
+ ORIGIN,
36
+ PID,
37
+ SEQ,
38
+ STATUS,
39
+ TOKEN,
40
+ WINDOW,
41
+ bearer,
42
+ frame,
43
+ )
44
+ from thzdaq_core.application.clock import wall_time
45
+ from thzdaq_core.application.daemon import DaemonService
46
+ from thzdaq_core.application.dispatch import Dispatcher
47
+ from thzdaq_core.application.events import EventBus
48
+
49
+ logger = logging.getLogger(__name__)
50
+
51
+ HTTP_STATUS = {
52
+ ErrorCode.INVALID_ARGUMENTS: 400,
53
+ ErrorCode.UNKNOWN_OPERATION: 404,
54
+ ErrorCode.NOT_FOUND: 404,
55
+ ErrorCode.NOT_ALLOWED: 403,
56
+ ErrorCode.INSTRUMENT: 502,
57
+ ErrorCode.OUT_OF_LIMITS: 422,
58
+ ErrorCode.CONFLICT: 409,
59
+ ErrorCode.DENIED: 403,
60
+ ErrorCode.INTERNAL: 500,
61
+ ErrorCode.UNREACHABLE: 503,
62
+ ErrorCode.UNAUTHORIZED: 401,
63
+ ErrorCode.WRONG_VERSION: 400,
64
+ }
65
+ SOCKET_S = 10.0 # a client that takes no bytes this long is gone
66
+
67
+
68
+ class Clients:
69
+ """Who listens to the events now; idle is how long nobody has."""
70
+
71
+ def __init__(self):
72
+ self._clients: Dict[int, ClientInfo] = {}
73
+ self._next = 0
74
+ self._lock = threading.Lock()
75
+ self._left = time.monotonic()
76
+
77
+ def add(self, client: ClientInfo) -> int:
78
+ with self._lock:
79
+ self._next += 1
80
+ self._clients[self._next] = client
81
+ return self._next
82
+
83
+ def remove(self, key: int) -> None:
84
+ with self._lock:
85
+ self._clients.pop(key, None)
86
+ if not self._clients:
87
+ self._left = time.monotonic()
88
+
89
+ def all(self) -> Tuple[ClientInfo, ...]:
90
+ with self._lock:
91
+ return tuple(self._clients.values())
92
+
93
+ def idle_for(self) -> float:
94
+ """Seconds since the last client left; 0 while one listens."""
95
+ with self._lock:
96
+ return 0.0 if self._clients else time.monotonic() - self._left
97
+
98
+
99
+ class Server(ThreadingHTTPServer):
100
+ # Request threads are joined on close, so an operation that stops the
101
+ # daemon still answers; streams end within a heartbeat once stopping
102
+ daemon_threads = False
103
+ block_on_close = True
104
+
105
+ def __init__(
106
+ self,
107
+ dispatcher: Dispatcher,
108
+ events: EventBus,
109
+ service: DaemonService,
110
+ token: str,
111
+ health: Health,
112
+ clients: Clients,
113
+ port: int = 0,
114
+ ):
115
+ super().__init__((LOCALHOST, port), _Handler)
116
+ self.dispatcher = dispatcher
117
+ self.events = events
118
+ self.service = service
119
+ self.token = token
120
+ self.health = health
121
+ self.clients = clients
122
+ self.stopping = threading.Event()
123
+
124
+ @property
125
+ def port(self) -> int:
126
+ return self.server_address[1]
127
+
128
+ def stop(self) -> None:
129
+ """Stop serving; the call that asked for it answers first."""
130
+ self.stopping.set()
131
+ self.events.wake()
132
+ self.shutdown()
133
+ self.server_close()
134
+
135
+
136
+ class _Handler(BaseHTTPRequestHandler):
137
+ server: Server
138
+ protocol_version = "HTTP/1.0"
139
+ timeout = SOCKET_S
140
+
141
+ def log_message(self, format, *args) -> None:
142
+ logger.debug(f"{self.address_string()} {format % args}")
143
+
144
+ def do_GET(self) -> None:
145
+ if not self._allowed():
146
+ return
147
+ if self.path == HEALTH:
148
+ self._answer(200, msgspec.json.encode(self.server.health))
149
+ elif self.path == STATUS:
150
+ self._answer(200, msgspec.json.encode(self.server.service.status()))
151
+ elif self.path == EVENTS:
152
+ self._stream()
153
+ else:
154
+ self._fail(ErrorCode.NOT_FOUND, f"No {self.path}")
155
+
156
+ def do_POST(self) -> None:
157
+ if not self._allowed():
158
+ return
159
+ if not self.path.startswith(OPERATIONS):
160
+ self._fail(ErrorCode.NOT_FOUND, f"No {self.path}")
161
+ return
162
+ name = self.path[len(OPERATIONS) :]
163
+ body = self.rfile.read(int(self.headers.get("Content-Length", 0)))
164
+ origin = self.headers.get(ORIGIN, "client")
165
+ try:
166
+ data = self.server.dispatcher.dispatch_json(name, body, origin)
167
+ except LabError as e:
168
+ self._answer(HTTP_STATUS[e.code], wire.encode(e.failure()))
169
+ else:
170
+ self._answer(200, data)
171
+
172
+ def _allowed(self) -> bool:
173
+ port = self.server.port
174
+ if self.headers.get("Host") not in (f"{LOCALHOST}:{port}", f"localhost:{port}"):
175
+ self._fail(ErrorCode.NOT_ALLOWED, "Not for this host")
176
+ return False
177
+ if self.headers.get("Origin") is not None:
178
+ self._fail(ErrorCode.NOT_ALLOWED, "Not for a web page")
179
+ return False
180
+ given = self.headers.get(TOKEN, "")
181
+ if not hmac.compare_digest(given.encode(), bearer(self.server.token).encode()):
182
+ self._fail(ErrorCode.UNAUTHORIZED, "Not this daemon's token")
183
+ return False
184
+ return True
185
+
186
+ def _answer(self, status: int, data: bytes) -> None:
187
+ self.send_response(status)
188
+ self.send_header("Content-Type", JSON)
189
+ self.send_header("Content-Length", str(len(data)))
190
+ self.send_header(SEQ, str(self.server.events.seq))
191
+ self.end_headers()
192
+ self.wfile.write(data)
193
+
194
+ def _fail(self, code: ErrorCode, message: str) -> None:
195
+ self._answer(HTTP_STATUS[code], wire.encode(Failure(code, message)))
196
+
197
+ def _stream(self) -> None:
198
+ events = self.server.events
199
+ last = self.headers.get(LAST_EVENT)
200
+ position = events.seq if last is None else int(last)
201
+ client = ClientInfo(
202
+ kind=self.headers.get(CLIENT, "client"),
203
+ pid=int(self.headers.get(PID, 0)),
204
+ origin=self.headers.get(ORIGIN, "client"),
205
+ since=wall_time(),
206
+ window=self.headers.get(WINDOW, ""),
207
+ )
208
+ key = self.server.clients.add(client)
209
+ try:
210
+ self.send_response(200)
211
+ self.send_header("Content-Type", "text/event-stream")
212
+ self.send_header("Cache-Control", "no-cache")
213
+ self.end_headers()
214
+ # What the client missed first, then a heartbeat: it is caught up
215
+ position = self._send(events.wait(position, 0), position)
216
+ self._beat(position)
217
+ while not self.server.stopping.is_set():
218
+ found = events.wait(position, HEARTBEAT_S)
219
+ if found:
220
+ position = self._send(found, position)
221
+ else:
222
+ self._beat(position)
223
+ except OSError:
224
+ pass # the client went
225
+ finally:
226
+ self.server.clients.remove(key)
227
+
228
+ def _send(self, found: List[Event], position: int) -> int:
229
+ for event in found:
230
+ self.wfile.write(_frame(event))
231
+ self.wfile.flush()
232
+ return found[-1].seq if found else position
233
+
234
+ def _beat(self, position: int) -> None:
235
+ beat = self.server.service.heartbeat(position)
236
+ self.wfile.write(b": ping\n\n" + frame(HEARTBEAT, msgspec.json.encode(beat)))
237
+ self.wfile.flush()
238
+
239
+
240
+ def _frame(event: Event) -> bytes:
241
+ kind: Optional[str] = type(event.body).__struct_config__.tag
242
+ return frame(kind or "", wire.encode(event), event.seq)
@@ -0,0 +1,30 @@
1
+ Metadata-Version: 2.5
2
+ Name: thzdaq-daemon
3
+ Version: 0.0.6b4
4
+ Summary: ThzDAQ bench daemon: holds the instruments and serves the thzdaq-api contract
5
+ Project-URL: Homepage, https://github.com/yarvod/ThzDAQ
6
+ Project-URL: Documentation, https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md
7
+ Project-URL: Issues, https://github.com/yarvod/ThzDAQ/issues
8
+ Project-URL: Releases, https://github.com/yarvod/ThzDAQ/releases
9
+ Author: yarvod
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Topic :: Scientific/Engineering :: Physics
19
+ Requires-Python: >=3.11
20
+ Requires-Dist: psutil>=5.9
21
+ Requires-Dist: thzdaq-api==0.0.6b4
22
+ Requires-Dist: thzdaq-core==0.0.6b4
23
+ Description-Content-Type: text/markdown
24
+
25
+ # thzdaq-daemon
26
+
27
+ The bench daemon: the server-side composition root (station, stores, use cases) and the
28
+ HTTP + SSE transport of the `thzdaq-api` contract, with its process lifecycle. It has no
29
+ operation logic of its own. Install it as `thzdaq[daemon]`; see
30
+ [docs/daemon.md](https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md).
@@ -0,0 +1,10 @@
1
+ thzdaq_daemon/__init__.py,sha256=1FelOJaXRTAoS2U2nG0GCrL5uAqDXD6b3A_c3gT5XOA,89
2
+ thzdaq_daemon/__main__.py,sha256=bvuAPzE0RAeiUMNuenN4MBSTYM_S65UDT8JTaumyfbo,2376
3
+ thzdaq_daemon/compose.py,sha256=OtkkHqL9XIBSt_bXA9syPo-BQdH-0quKHUWI7zLva1w,2632
4
+ thzdaq_daemon/process.py,sha256=MZKAemTWyNt8srhKxLnkaE3H4pdn7QWVQojxLxLRbp4,10359
5
+ thzdaq_daemon/server.py,sha256=Ekt674hGq6mENsUuE7oUIEv2Boz0Y3_KFNU0mYLNZTA,8105
6
+ thzdaq_daemon-0.0.6b4.dist-info/METADATA,sha256=EBP0V9Q24gnjPEzqzoL4JbDuqbIqESMlwlggbamSIjE,1328
7
+ thzdaq_daemon-0.0.6b4.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
8
+ thzdaq_daemon-0.0.6b4.dist-info/entry_points.txt,sha256=813oGJHpq6ZbmO-FSDQpPcmBKvDH-PqO2DTqmR99nZA,62
9
+ thzdaq_daemon-0.0.6b4.dist-info/licenses/LICENSE,sha256=jXHKk1r0G-rRjWXmfQVbjBzee356QkXdR3C8GExo_V0,1063
10
+ thzdaq_daemon-0.0.6b4.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ thzdaq-daemon = thzdaq_daemon.__main__:main
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 yarvod
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.