thzdaq-daemon 0.0.6b4__tar.gz

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,25 @@
1
+ .idea
2
+ venv
3
+ .venv
4
+ .uv-cache
5
+ build
6
+ dist
7
+ *__pycache__
8
+ *.log
9
+ *.DS_Store
10
+ *.spec
11
+ !ThzDAQ.spec
12
+ *.ipynb
13
+ .env
14
+ /data/
15
+ # What a daemon run from sources keeps between runs
16
+ /daemon.json
17
+ /daemon-sim.json
18
+ # Window layout and perspectives of a local run
19
+ /settings.ini
20
+ # What a daemon run from sources keeps of instruments (a grid's angle)
21
+ /state.json
22
+ /state-sim.json
23
+ # The program's own measurements and the profiles' previous versions
24
+ /measurements/
25
+ stations/.history/
@@ -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.
@@ -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,6 @@
1
+ # thzdaq-daemon
2
+
3
+ The bench daemon: the server-side composition root (station, stores, use cases) and the
4
+ HTTP + SSE transport of the `thzdaq-api` contract, with its process lifecycle. It has no
5
+ operation logic of its own. Install it as `thzdaq[daemon]`; see
6
+ [docs/daemon.md](https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md).
@@ -0,0 +1,43 @@
1
+ [project]
2
+ name = "thzdaq-daemon"
3
+ version = "0.0.6b4"
4
+ description = "ThzDAQ bench daemon: holds the instruments and serves the thzdaq-api contract"
5
+ readme = "README.md"
6
+ requires-python = ">=3.11"
7
+ license = "MIT"
8
+ license-files = ["LICENSE"]
9
+ authors = [{ name = "yarvod" }]
10
+ classifiers = [
11
+ "Development Status :: 3 - Alpha",
12
+ "Intended Audience :: Science/Research",
13
+ "Operating System :: OS Independent",
14
+ "Programming Language :: Python :: 3",
15
+ "Programming Language :: Python :: 3 :: Only",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Topic :: Scientific/Engineering :: Physics",
18
+ ]
19
+ dependencies = [
20
+ "psutil>=5.9",
21
+ "thzdaq-api==0.0.6b4",
22
+ "thzdaq-core==0.0.6b4",
23
+ ]
24
+
25
+ [project.urls]
26
+ Homepage = "https://github.com/yarvod/ThzDAQ"
27
+ Documentation = "https://github.com/yarvod/ThzDAQ/blob/master/docs/daemon.md"
28
+ Issues = "https://github.com/yarvod/ThzDAQ/issues"
29
+ Releases = "https://github.com/yarvod/ThzDAQ/releases"
30
+
31
+ [project.scripts]
32
+ thzdaq-daemon = "thzdaq_daemon.__main__:main"
33
+
34
+ [tool.uv.sources]
35
+ thzdaq-api = { workspace = true }
36
+ thzdaq-core = { workspace = true }
37
+
38
+ [build-system]
39
+ requires = ["hatchling>=1.27"]
40
+ build-backend = "hatchling.build"
41
+
42
+ [tool.hatch.build.targets.wheel]
43
+ packages = ["src/thzdaq_daemon"]
@@ -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())