plesty-server 0.4.2__tar.gz → 0.4.4__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.
Files changed (57) hide show
  1. {plesty_server-0.4.2 → plesty_server-0.4.4}/PKG-INFO +4 -4
  2. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/home.py +32 -2
  3. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/job.py +82 -9
  4. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/process.py +4 -0
  5. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/presenter/bench.py +106 -19
  6. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/control.py +56 -1
  7. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/launch.py +111 -2
  8. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/run_report.py +15 -3
  9. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/supervisor.py +35 -17
  10. plesty_server-0.4.4/plesty/server/view/app.py +205 -0
  11. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/view/control.py +42 -0
  12. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/view/dialogs.py +4 -1
  13. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/view/window.py +241 -38
  14. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/view/workers.py +52 -0
  15. {plesty_server-0.4.2 → plesty_server-0.4.4}/pyproject.toml +4 -4
  16. plesty_server-0.4.2/plesty/server/view/app.py +0 -97
  17. {plesty_server-0.4.2 → plesty_server-0.4.4}/LICENSE +0 -0
  18. {plesty_server-0.4.2 → plesty_server-0.4.4}/LICENSES/LGPL-3.0-or-later.txt +0 -0
  19. {plesty_server-0.4.2 → plesty_server-0.4.4}/README.md +0 -0
  20. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/__init__.py +0 -0
  21. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/__main__.py +0 -0
  22. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/console.qss +0 -0
  23. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/env.example +0 -0
  24. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/fleet.example.yaml +0 -0
  25. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/plesty-mark.svg +0 -0
  26. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/report.css +0 -0
  27. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/assets/report.html +0 -0
  28. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/cli/__init__.py +0 -0
  29. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/cli/main.py +0 -0
  30. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/config.yaml +0 -0
  31. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/host.py +0 -0
  32. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/__init__.py +0 -0
  33. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/agent.py +0 -0
  34. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/catalogue.py +0 -0
  35. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/device.py +0 -0
  36. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/device_manager.py +0 -0
  37. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/envfile.py +0 -0
  38. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/errors.py +0 -0
  39. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/experiment.py +0 -0
  40. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/field_test.py +0 -0
  41. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/fleet.py +0 -0
  42. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/peer.py +0 -0
  43. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/ports.py +0 -0
  44. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/purge.py +0 -0
  45. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/model/traffic.py +0 -0
  46. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/presenter/__init__.py +0 -0
  47. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/reexec.py +0 -0
  48. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/__init__.py +0 -0
  49. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/agent.py +0 -0
  50. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/catalogue.py +0 -0
  51. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/experiments.py +0 -0
  52. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/gitbin.py +0 -0
  53. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/installer.py +0 -0
  54. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/jobs.py +0 -0
  55. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/nodes.py +0 -0
  56. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/services/problem_report.py +0 -0
  57. {plesty_server-0.4.2 → plesty_server-0.4.4}/plesty/server/view/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: plesty-server
3
- Version: 0.4.2
3
+ Version: 0.4.4
4
4
  Summary: The PLESTY bench application: installs, runs and field-tests device servers.
5
5
  Project-URL: Homepage, https://gitlab.com/plesty/core/plesty-bench
6
6
  Project-URL: Documentation, https://plesty.gitlab.io/core/plesty-bench/
@@ -11,12 +11,12 @@ License-File: LICENSE
11
11
  License-File: LICENSES/LGPL-3.0-or-later.txt
12
12
  Requires-Python: >=3.12
13
13
  Requires-Dist: click>=8.1
14
- Requires-Dist: plesty-bench==0.4.2
15
- Requires-Dist: plesty-lib[report]>=0.5.0
14
+ Requires-Dist: plesty-bench==0.4.4
15
+ Requires-Dist: plesty-lib[report]>=0.5.2
16
16
  Requires-Dist: pyyaml>=6.0
17
17
  Requires-Dist: pyzmq>=26
18
18
  Provides-Extra: gui
19
- Requires-Dist: plesty-lib[gui]>=0.5.0; extra == 'gui'
19
+ Requires-Dist: plesty-lib[gui]>=0.5.2; extra == 'gui'
20
20
  Requires-Dist: pyside6==6.11.2; extra == 'gui'
21
21
  Description-Content-Type: text/markdown
22
22
 
@@ -14,6 +14,7 @@ from __future__ import annotations
14
14
 
15
15
  import os
16
16
  from dataclasses import dataclass
17
+ from datetime import datetime
17
18
  from pathlib import Path
18
19
 
19
20
  from plesty.server import load_config
@@ -136,8 +137,37 @@ class Home:
136
137
  return self.venvs / name
137
138
 
138
139
  def log(self, name: str) -> Path:
139
- """Log file of the device *name*."""
140
- return self.logs / f"{name}.log"
140
+ """The current log file of the device *name*.
141
+
142
+ Each server start, and each Clear log, opens a new file named with its
143
+ time (:meth:`new_log`); ``logs/<name>.current`` names the one in use.
144
+ Without a pointer — a log written before there were any — it is the
145
+ single ``logs/<name>.log`` every start used to append to.
146
+ """
147
+ pointer = self.logs / f"{name}.current"
148
+ try:
149
+ current = self.logs / pointer.read_text(encoding="utf-8").strip()
150
+ except OSError:
151
+ return self.logs / f"{name}.log"
152
+ return current if current.name and current.is_file() else self.logs / f"{name}.log"
153
+
154
+ def new_log(self, name: str) -> Path:
155
+ """Start a new log file for *name*, make it the current one, and return it.
156
+
157
+ Named ``<name>-<YYYYMMDD-HHMMSS>.log``. Older files stay: a clear never
158
+ truncates a file a running server writes, which is what left an 11.7 MB
159
+ log no reader could get through (#23).
160
+ """
161
+ self.logs.mkdir(parents=True, exist_ok=True)
162
+ stamp = datetime.now().strftime("%Y%m%d-%H%M%S")
163
+ path = self.logs / f"{name}-{stamp}.log"
164
+ count = 1
165
+ while path.exists():
166
+ path = self.logs / f"{name}-{stamp}-{count}.log"
167
+ count += 1
168
+ path.touch()
169
+ (self.logs / f"{name}.current").write_text(path.name + "\n", encoding="utf-8")
170
+ return path
141
171
 
142
172
  def state_file(self, name: str) -> Path:
143
173
  """Process-state file of the device *name*."""
@@ -32,6 +32,10 @@ from plesty.server.model.home import Home
32
32
  #: Bytes one :meth:`JobStore.log` call returns at most.
33
33
  LOG_CHUNK = 65536
34
34
 
35
+ #: Bytes a log view opens on, and loads at a time as it is scrolled up: many
36
+ #: screens of lines, and still one quick read and paint.
37
+ LOG_END = 262144
38
+
35
39
  #: Exit code recorded when the process vanished without leaving one.
36
40
  LOST_EXIT = -1
37
41
 
@@ -248,28 +252,97 @@ class JobStore:
248
252
  data = fh.read(limit)
249
253
  return data.decode("utf-8", errors="replace"), offset + len(data)
250
254
 
255
+ def log_end(self, job_id: str, limit: int = LOG_END) -> tuple[str, int, int]:
256
+ """The end of the job log: at most its last *limit* bytes, from a line start.
257
+
258
+ What a log view opens on, so a long log is not read and painted from
259
+ its first byte to reach the last (#28).
260
+
261
+ Args:
262
+ job_id: The job.
263
+ limit: Most bytes to return.
264
+
265
+ Returns:
266
+ The text, the byte it starts at (``0`` when it is the whole log),
267
+ and the byte it ends at — where reading on continues.
268
+ """
269
+ path = Path(self.require(job_id).log)
270
+ if not path.is_file():
271
+ return "", 0, 0
272
+ with path.open("rb") as fh:
273
+ fh.seek(0, os.SEEK_END)
274
+ end = fh.tell()
275
+ start = max(0, end - limit)
276
+ fh.seek(start)
277
+ data = fh.read(end - start)
278
+ start, data = _from_line_start(start, data)
279
+ return data.decode("utf-8", errors="replace"), start, end
280
+
281
+ def log_before(self, job_id: str, end: int, limit: int = LOG_END) -> tuple[str, int]:
282
+ """The part of the job log before byte *end*: at most *limit* bytes, from a line start.
283
+
284
+ Args:
285
+ job_id: The job.
286
+ end: Where the part already shown begins.
287
+ limit: Most bytes to return.
288
+
289
+ Returns:
290
+ The text and the byte it starts at; ``("", 0)`` at the top.
291
+ """
292
+ path = Path(self.require(job_id).log)
293
+ if end <= 0 or not path.is_file():
294
+ return "", 0
295
+ start = max(0, end - limit)
296
+ with path.open("rb") as fh:
297
+ fh.seek(start)
298
+ data = fh.read(end - start)
299
+ start, data = _from_line_start(start, data)
300
+ return data.decode("utf-8", errors="replace"), start
301
+
251
302
  def tail(self, job_id: str, lines: int = 50) -> str:
252
303
  """The last *lines* lines of the job log (empty when there is none yet)."""
253
304
  job = self.require(job_id)
254
305
  return tail_file(Path(job.log), lines)
255
306
 
256
307
 
257
- def tail_file(path: Path, lines: int, block: int = 8192) -> str:
258
- """The last *lines* lines of *path*, read from the end in *block* steps."""
308
+ #: The most of a log's end :func:`tail_file` reads, in bytes.
309
+ TAIL_LIMIT = 256 * 1024
310
+
311
+
312
+ def tail_file(path: Path, lines: int, limit: int = TAIL_LIMIT) -> str:
313
+ """The last *lines* lines of *path*, read from at most its last *limit* bytes.
314
+
315
+ One read of a bounded span. The loop this replaced prepended each 8 KB
316
+ block to everything read so far until enough newlines turned up — on a
317
+ file with none (a truncated log a server kept writing at its old offset
318
+ fills with NULs) that is quadratic in the file's size, and an 11.7 MB log
319
+ hung the console, the agent and every reader with it (#23). A line longer
320
+ than *limit* is shown from its last *limit* bytes.
321
+ """
259
322
  if lines <= 0 or not path.is_file():
260
323
  return ""
261
324
  with path.open("rb") as fh:
262
325
  fh.seek(0, os.SEEK_END)
263
326
  end = fh.tell()
264
- data = b""
265
- while end > 0 and data.count(b"\n") <= lines:
266
- step = min(block, end)
267
- end -= step
268
- fh.seek(end)
269
- data = fh.read(step) + data
270
- text = data.decode("utf-8", errors="replace")
327
+ fh.seek(max(0, end - limit))
328
+ data = fh.read(limit)
329
+ text = data.decode("utf-8", errors="replace").replace("\x00", "")
271
330
  return "\n".join(text.splitlines()[-lines:])
272
331
 
273
332
 
333
+ def _from_line_start(start: int, data: bytes) -> tuple[int, bytes]:
334
+ """Drop the partial line a read from the middle of a file begins with.
335
+
336
+ A read from byte 0 starts on a line already. One whose span holds no line
337
+ break at all is kept whole: a line longer than the span still shows.
338
+ """
339
+ if start == 0:
340
+ return start, data
341
+ cut = data.find(b"\n") + 1
342
+ if cut == 0:
343
+ return start, data
344
+ return start + cut, data[cut:]
345
+
346
+
274
347
  def _now() -> str:
275
348
  return datetime.now(UTC).isoformat(timespec="seconds")
@@ -46,6 +46,9 @@ class ProcessRecord:
46
46
  env: The spec's environment — likewise.
47
47
  package: Distribution the server runs, for listings.
48
48
  version: Its installed version, for listings.
49
+ relayed: Its waiter copies the output into the log and can switch it
50
+ to a new file while it runs. ``False`` for a server an older
51
+ bench started, whose log is the server's own handle (#23).
49
52
  """
50
53
 
51
54
  name: str
@@ -61,6 +64,7 @@ class ProcessRecord:
61
64
  env: dict[str, str] = field(default_factory=dict)
62
65
  package: str = ""
63
66
  version: str = ""
67
+ relayed: bool = False
64
68
 
65
69
  def to_dict(self) -> dict[str, Any]:
66
70
  """JSON form."""
@@ -18,6 +18,7 @@ from __future__ import annotations
18
18
 
19
19
  import json
20
20
  import os
21
+ import re
21
22
  import shutil
22
23
  import sys
23
24
  import threading
@@ -195,14 +196,46 @@ def _planned_config(run_dir: Path) -> dict[str, Any] | None:
195
196
  return planned if isinstance(planned, dict) else None
196
197
 
197
198
 
198
- def _waiting_runs_last(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
199
- """Order runs newest first, with the ones not yet started at the end.
199
+ #: The Source of a run that has started but recorded no versions: one written
200
+ #: before runs journaled their provenance. Said as unknown, never filled in
201
+ #: with today's install, which is the one answer certain to be wrong.
202
+ UNKNOWN_SOURCE = "unknown — the run recorded no version"
203
+
204
+
205
+ def _run_source(status: dict[str, Any], package: str, installed: str) -> str:
206
+ """What a run's Source cell says: the version that measured it.
207
+
208
+ Args:
209
+ status: The run's status, with the ``provenance`` its start journaled.
210
+ package: The experiment's distribution.
211
+ installed: The current install's line (:meth:`Bench.experiment_source`).
212
+
213
+ Returns:
214
+ ``<package>==<version>`` from the run's own provenance once it has
215
+ started; the current install marked as what it will run on while it
216
+ waits; :data:`UNKNOWN_SOURCE` for a started run that recorded none.
217
+ """
218
+ if status.get("state") in ("created", "not_started"):
219
+ return f"{installed} (will run on)" if installed else ""
220
+ versions = (status.get("provenance") or {}).get("versions") or {}
221
+ wanted = _canonical(package)
222
+ version = next((v for n, v in versions.items() if _canonical(str(n)) == wanted), None)
223
+ return f"{package}=={version}" if version else UNKNOWN_SOURCE
224
+
225
+
226
+ def _canonical(name: str) -> str:
227
+ """A distribution name as PyPI compares them: case and ``_``/``.``/``-`` alike."""
228
+ return re.sub(r"[-_.]+", "-", name).lower()
229
+
230
+
231
+ def _waiting_runs_first(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
232
+ """Order runs with the ones not yet started on top, the newest created first.
200
233
 
201
234
  A created run has no start time, so by name alone it lands wherever its
202
- name happens to sort — which for a duplicate made today is the top, above
203
- the measurements. What has been measured is what an operator reads; what
204
- is waiting to be measured is a short list at the bottom, in the order it
205
- was created.
235
+ name happens to sort. Put at the bottom, under every measurement, the run
236
+ just created was out of sight, and creating it read as having failed — an
237
+ operator duplicated a run three times and saw none of them (#31). The run
238
+ just created is the one the next *Run* is meant for, so it leads.
206
239
 
207
240
  Args:
208
241
  rows: The runs, newest first by name.
@@ -212,8 +245,11 @@ def _waiting_runs_last(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:
212
245
  """
213
246
  started = [row for row in rows if row["status"].get("state") != "created"]
214
247
  waiting = [row for row in rows if row["status"].get("state") == "created"]
215
- waiting.sort(key=lambda row: str((row["status"].get("created") or {}).get("created_at") or ""))
216
- return started + waiting
248
+ waiting.sort(
249
+ key=lambda row: str((row["status"].get("created") or {}).get("created_at") or ""),
250
+ reverse=True,
251
+ )
252
+ return waiting + started
217
253
 
218
254
 
219
255
  def control_blocked(status: ProcessStatus) -> str | None:
@@ -432,8 +468,14 @@ class Bench:
432
468
  """
433
469
  resolved_home = Home.resolve(home)
434
470
  path = find_fleet(fleet_path, resolved_home.root)
435
- if path is None and resolved_home.last_fleet.is_file():
436
- # what the console used last — so a double-click opens where it left off
471
+ default = resolved_home.root / FLEET_FILENAME
472
+ unchosen = fleet_path is None and (path is None or path == default)
473
+ if unchosen and resolved_home.last_fleet.is_file():
474
+ # What the console used last outranks the home's default fleet.yaml,
475
+ # so a reopened app — after a crash too — comes back to the fleet
476
+ # whose servers are still running rather than to a stale default
477
+ # that orphans them (#24). An explicit -f and a fleet.yaml in the
478
+ # working directory still win: both were put there on purpose.
437
479
  remembered = Path(resolved_home.last_fleet.read_text(encoding="utf-8").strip())
438
480
  if remembered.is_file():
439
481
  path = remembered
@@ -568,6 +610,8 @@ class Bench:
568
610
  if source.exists():
569
611
  target.parent.mkdir(parents=True, exist_ok=True)
570
612
  source.rename(target)
613
+ # the old name's pointer named the file just moved (#23)
614
+ (self.home.logs / f"{old}.current").unlink(missing_ok=True)
571
615
  self.fleet = fleet
572
616
  self._save_fleet()
573
617
  return self.fleet[new]
@@ -629,6 +673,11 @@ class Bench:
629
673
  """
630
674
  self.fleet = load_fleet(path)
631
675
  self.remember_fleet()
676
+ if self._agent is not None:
677
+ # The agent passes its fleet to every command it runs; left on the
678
+ # one it started with, a remote declare or start acted on another
679
+ # fleet than the operator sees (#24).
680
+ self._agent.service.fleet_path = self.fleet.path
632
681
  return self.fleet
633
682
 
634
683
  def open_fleet(self, path: str | os.PathLike[str], create: bool = False) -> Fleet:
@@ -1023,7 +1072,10 @@ class Bench:
1023
1072
  found = sorted(
1024
1073
  f"config/{item.name}"
1025
1074
  for item in directory.iterdir()
1026
- if item.is_file() and item.suffix in (".yaml", ".yml")
1075
+ if item.is_file()
1076
+ and item.suffix in (".yaml", ".yml")
1077
+ # macOS's AppleDouble companion on a share, not a config
1078
+ and not item.name.startswith("._")
1027
1079
  )
1028
1080
  except (BenchError, FleetError, OSError):
1029
1081
  # Not installed yet, or a checkout this bench cannot reach: the
@@ -1675,17 +1727,21 @@ class Bench:
1675
1727
  Returns:
1676
1728
  One ``{"name", "status"}`` per run, or a single not-started row.
1677
1729
  """
1678
- # Read once per experiment rather than per run: every run of it comes
1679
- # from the same install, and it costs a directory listing.
1730
+ # The install is read once per experiment, and it is only what a run
1731
+ # *will* execute: a run measured before an update was made by another
1732
+ # version, which its own provenance records (#32).
1680
1733
  source = self.experiment_source(name)
1681
1734
  runs = self.experiment_runs(name)
1682
1735
  if not runs:
1683
1736
  return [{"name": name, "status": self.experiment_status(name), "source": source}]
1684
- rows = [
1685
- {"name": name, "status": self.experiment_status(name, run.name), "source": source}
1686
- for run in runs
1687
- ]
1688
- return _waiting_runs_last(rows)
1737
+ package = self.experiment(name).package
1738
+ rows = []
1739
+ for run in runs:
1740
+ status = self.experiment_status(name, run.name)
1741
+ rows.append(
1742
+ {"name": name, "status": status, "source": _run_source(status, package, source)}
1743
+ )
1744
+ return _waiting_runs_first(rows)
1689
1745
 
1690
1746
  def read_run(self, run_dir: str | Path) -> dict[str, Any]:
1691
1747
  """Read a run directory that is not one of this bench's own.
@@ -2925,6 +2981,18 @@ class Bench:
2925
2981
  """
2926
2982
  return [n for n in self.stoppable() if self.supervisor.alive(n)]
2927
2983
 
2984
+ def orphans(self) -> list[str]:
2985
+ """Servers alive on this bench that the current fleet does not declare.
2986
+
2987
+ A fleet opened in place of the one that started them — a stale default
2988
+ after a crash — drops them out of the table while they keep their
2989
+ ports and the instruments' sessions. Named, they can be stopped (#24).
2990
+ """
2991
+ declared = set(self.fleet.devices)
2992
+ return [
2993
+ n for n in self.supervisor.known() if n not in declared and self.supervisor.alive(n)
2994
+ ]
2995
+
2928
2996
  def run(
2929
2997
  self,
2930
2998
  stop: threading.Event,
@@ -3109,7 +3177,18 @@ class Bench:
3109
3177
  if blocked is not None:
3110
3178
  raise BenchError(f"{name}: {blocked}")
3111
3179
  port = self.status(name).port
3112
- return DeviceLink(name, f"tcp://{PROBE_HOST}:{port}")
3180
+
3181
+ def where() -> str | None:
3182
+ # The process record only: this runs before every call the panel
3183
+ # makes, and a probe per call would double the traffic. A server
3184
+ # that is gone has no address: a call there would wait out the
3185
+ # whole timeout for a reply nobody sends (#22).
3186
+ now = self.status(name, probe=False)
3187
+ if now.state not in (RUNNING, STARTING) or not now.port:
3188
+ return None
3189
+ return f"tcp://{PROBE_HOST}:{now.port}"
3190
+
3191
+ return DeviceLink(name, f"tcp://{PROBE_HOST}:{port}", resolve=where)
3113
3192
 
3114
3193
  def field_test_availability(self, name: str, tier: str = "host") -> str | None:
3115
3194
  """Why the field test of *name* cannot run, or ``None`` when it can.
@@ -3215,6 +3294,14 @@ class Bench:
3215
3294
  """Job log from byte *offset*; returns text and the next offset."""
3216
3295
  return self.jobs.store.log(job_id, offset)
3217
3296
 
3297
+ def job_log_end(self, job_id: str) -> tuple[str, int, int]:
3298
+ """The end of a job's log: text, the byte it starts at, the byte it ends at."""
3299
+ return self.jobs.store.log_end(job_id)
3300
+
3301
+ def job_log_before(self, job_id: str, end: int) -> tuple[str, int]:
3302
+ """The part of a job's log before byte *end*: text and the byte it starts at."""
3303
+ return self.jobs.store.log_before(job_id, end)
3304
+
3218
3305
  def cancel(self, job_id: str) -> bool:
3219
3306
  """Cancel a running job."""
3220
3307
  return self.jobs.cancel(job_id)
@@ -26,6 +26,7 @@ instead, behind *Take control*.
26
26
 
27
27
  from __future__ import annotations
28
28
 
29
+ from collections.abc import Callable
29
30
  from typing import Any
30
31
 
31
32
  #: How long one device call may take before the link gives up, in
@@ -37,25 +38,79 @@ DEVICE_TIMEOUT_MS = 15000
37
38
  class DeviceLink:
38
39
  """A lazy :class:`~plesty.lib.ui.device_link.DeviceHandle` over one device server."""
39
40
 
40
- def __init__(self, name: str, address: str, timeout_ms: int = DEVICE_TIMEOUT_MS) -> None:
41
+ def __init__(
42
+ self,
43
+ name: str,
44
+ address: str,
45
+ timeout_ms: int = DEVICE_TIMEOUT_MS,
46
+ resolve: Callable[[], str | None] | None = None,
47
+ ) -> None:
41
48
  """Prepare a link; nothing is connected yet.
42
49
 
43
50
  Args:
44
51
  name: The device's fleet name, for messages.
45
52
  address: ZMQ endpoint of its server, ``tcp://host:port``.
46
53
  timeout_ms: Reply timeout for one call.
54
+ resolve: Returns where the server is now, or ``None`` when it is
55
+ not running; raises when that is not known. A restarted server
56
+ can come back on another port; a link that kept the first
57
+ address drove a port nobody listened on, and the panel's
58
+ controls silently did nothing (#22).
47
59
  """
48
60
  self.name = name
49
61
  self.address = address
50
62
  self.timeout_ms = timeout_ms
63
+ self._resolve = resolve
51
64
  self._client: Any = None
65
+ self._held = False
66
+
67
+ def hold(self) -> None:
68
+ """Refuse every call until :meth:`release`: the server is being stopped.
69
+
70
+ A call sent to a server that is shutting down, or to the port it left,
71
+ gets no reply and waits out the whole timeout — and every call after it
72
+ waits behind it on the panel's one worker. Restarting on a new port
73
+ left the panel dead for up to fifteen seconds that way (#22). Held, a
74
+ call fails at once and sends nothing.
75
+ """
76
+ self._held = True
77
+
78
+ def release(self) -> None:
79
+ """Let calls through again after :meth:`hold`."""
80
+ self._held = False
81
+
82
+ def _follow(self) -> None:
83
+ """Move to the server's current address, dropping a client on the old one.
84
+
85
+ Raises:
86
+ ConnectionError: When the server is not running, rather than send
87
+ a call to an address nothing answers on.
88
+ """
89
+ if self._resolve is None:
90
+ return
91
+ try:
92
+ current = self._resolve()
93
+ except Exception: # noqa: BLE001 — not knowing where it is now is not a new address
94
+ return
95
+ if current is None:
96
+ self.close()
97
+ raise ConnectionError(f"{self.name} is not running")
98
+ if current != self.address:
99
+ self.close()
100
+ self.address = current
52
101
 
53
102
  def client(self) -> Any:
54
103
  """Return the connected client, building it on first use.
55
104
 
56
105
  Returns:
57
106
  The ``DeviceTCPIPClient``.
107
+
108
+ Raises:
109
+ ConnectionError: When the link is held or the server is not running.
58
110
  """
111
+ if self._held:
112
+ raise ConnectionError(f"{self.name} is restarting")
113
+ self._follow()
59
114
  if self._client is None:
60
115
  from plesty.lib.service import build_client
61
116