Compare commits
18 Commits
19548466ad
...
v1.6.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 9d4ea822fd | |||
| aa341c7342 | |||
| fb1edfbad4 | |||
| e6e4c94f19 | |||
| 92ebbd6537 | |||
| 4adaa09d75 | |||
| 730f215e23 | |||
| a457619158 | |||
| 48fba65f20 | |||
| 9b3ae6d548 | |||
| 65b3af90ae | |||
| e6c25ab941 | |||
| c81f4ee61f | |||
| 1abdc13645 | |||
| f790b21808 | |||
| 0bea6ca4ea | |||
| 2456fd0ee8 | |||
| c133932b29 |
+37
-1
@@ -6,13 +6,49 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## [Unreleased] — v1.5.0
|
## [6-25-26] — v1.6.1
|
||||||
|
|
||||||
|
Reliability fixes for the tray app's auto-updater and scan logging.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- **Auto-updater no longer dies when Settings are saved.** Saving the Settings dialog restarts the watcher thread via `_restart_watcher`, which sets and then replaces the shared `stop_event`. Because the updater loop keyed off that *same* event, saving settings would terminate the auto-updater thread (a race — it could survive one save and die on the next). The watcher kept running and reporting heartbeats, but the agent silently stopped checking for / applying updates until the tray app was relaunched. The updater now keys off its own app-lifetime event (`_app_stop`) that watcher restarts never touch; it stops only on Exit or when applying an update. (Same fix as thor-watcher 0.4.1.)
|
||||||
|
- **Unsniffable `.MLG` files are logged once, not every scan.** A `.MLG` file the watcher couldn't extract a unit ID from was never cached, so every scan (~every 5 min) re-sniffed *and* re-emitted its `[unsniffable-recent]` log line. With a few hundred such files in a long-lived Blastware autocall folder that meant tens of thousands of duplicate lines per day (a 10-day-old log had grown to ~600K lines). The scan now caches the unsniffable result, so each file is sniffed and logged once per session — and the redundant per-scan header reads are gone.
|
||||||
|
- Added `test_scan_latest.py` (unsniffable cached / logged-once + happy-path sniff) and `test_series3_tray.py` (updater survives a watcher restart; stops on exit).
|
||||||
|
|
||||||
|
## [6-25-26] — v1.6.0
|
||||||
|
|
||||||
|
Optional dual-send mirror so a standby server stays a live replica.
|
||||||
|
|
||||||
|
### Added — Mirror (dual-send)
|
||||||
|
- **Post every heartbeat and event to a second ("mirror") server in addition to the primary.** When `MIRROR_API_URL` and/or `MIRROR_SFM_URL` are set, each heartbeat POST and each forwarded Blastware event is *also* sent to the mirror destination — letting a standby box (e.g. the office NAS) stay a continuous replica of prod during a migration so the eventual cutover is a non-event. **Default off** — blank URLs mean existing 1.5.x installs don't change behaviour after an auto-update.
|
||||||
|
- **Heartbeat mirror (best-effort).** After the primary `send_api_payload`, the same payload is fired at `MIRROR_API_URL` when set. It is fully wrapped and bounded by the existing API timeout — the mirror can **never** delay or fail the primary heartbeat; its outcome is ignored except for a debug log line.
|
||||||
|
- **Event mirror (reliable, isolated).** Events are re-forwarded to `MIRROR_SFM_URL` through a *separate* sha256 state file (`MIRROR_SFM_STATE_FILE`, blank → `<log dir>/sfm_forwarded_mirror.json`) so the mirror tracks its own delivery independently of the primary forwarder. Before each mirror pass a quick reachability probe (~3 s) checks the mirror server; if it's down the pass is skipped and retried next tick rather than blocking the loop on per-event timeouts. Skipped events stay pending in the mirror state and deliver once the mirror returns — **no data loss**.
|
||||||
|
- **Isolation invariant.** Nothing on the mirror path can delay or fail the primary: its own state file, its own try/except, the reachability guard + bounded timeouts, and all exceptions swallowed-and-logged. The primary path stays exactly as reliable as before. The mirror rides along with the primary — there is no separate enable flag (heartbeat-mirror active iff the primary heartbeat is on *and* `MIRROR_API_URL` is set; event-mirror active iff primary forwarding is on *and* `MIRROR_SFM_URL` is set).
|
||||||
|
- **Mirror fields in the Settings dialog**: `Mirror API URL` + `Mirror SFM URL` (blank = off).
|
||||||
|
- New unit tests in `test_event_forwarder.py` covering mirror reachability, the down-mirror skip, separate-state idempotency, and the isolation invariant (a mirror failure leaves the primary's result + state untouched).
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
New `[agent]` keys (all default-off — existing 1.5.x deployments don't change behaviour on auto-update):
|
||||||
|
|
||||||
|
| Key | Default | Notes |
|
||||||
|
|---|---|---|
|
||||||
|
| `MIRROR_API_URL` | empty | Second heartbeat base URL, e.g. `http://10.0.0.x:8001` (blank = off) |
|
||||||
|
| `MIRROR_SFM_URL` | empty | Second SFM base URL, e.g. `http://10.0.0.x:8200` (blank = off) |
|
||||||
|
| `MIRROR_SFM_STATE_FILE` | `<log dir>/sfm_forwarded_mirror.json` | Override location of the mirror's forwarded-sha256 state file |
|
||||||
|
|
||||||
|
### Operational (migration seeding)
|
||||||
|
|
||||||
|
To make the mirror re-deliver only the gap since a one-time replica snapshot, copy the primary state file to the mirror state file and drop entries whose `forwarded_at` is after the snapshot. SFM-side dedup covers any overlap. See `docs/mirror-dual-send-design.md`.
|
||||||
|
|
||||||
|
## [5-11-26] — v1.5.0
|
||||||
|
|
||||||
First release of the SFM event forwarder.
|
First release of the SFM event forwarder.
|
||||||
|
|
||||||
### Added — SFM event forwarder
|
### Added — SFM event forwarder
|
||||||
- **Forward Blastware event binaries (+ paired BW ACH ASCII reports) to an SFM server.** When `SFM_FORWARD_ENABLED=true` and `SFM_URL` is set, every event binary in the BW ACH watch folder is POSTed as multipart to `/db/import/blastware_file` along with its `<stem>_<ext>_ASCII.TXT` partner report (BW ACH convention; manual-export `<binary>.TXT` is also supported as a fallback). SFM parses the report and indexes the full per-channel stats (PPV, ZC Freq, Time of Peak, Peak Acceleration / Displacement, Peak Vector Sum + time, sensor self-check Pass/Fail, monitor-log timestamps) into a searchable database — no codec decoding required.
|
- **Forward Blastware event binaries (+ paired BW ACH ASCII reports) to an SFM server.** When `SFM_FORWARD_ENABLED=true` and `SFM_URL` is set, every event binary in the BW ACH watch folder is POSTed as multipart to `/db/import/blastware_file` along with its `<stem>_<ext>_ASCII.TXT` partner report (BW ACH convention; manual-export `<binary>.TXT` is also supported as a fallback). SFM parses the report and indexes the full per-channel stats (PPV, ZC Freq, Time of Peak, Peak Acceleration / Displacement, Peak Vector Sum + time, sensor self-check Pass/Fail, monitor-log timestamps) into a searchable database — no codec decoding required.
|
||||||
- **Idempotent forwarding.** Forwarded files are tracked by sha256 in a JSON state file (default `<log dir>/sfm_forwarded.json`, override via `SFM_STATE_FILE`). Re-scans don't re-POST and the state survives restarts / auto-updates.
|
- **Idempotent forwarding.** Forwarded files are tracked by sha256 in a JSON state file (default `<log dir>/sfm_forwarded.json`, override via `SFM_STATE_FILE`). Re-scans don't re-POST and the state survives restarts / auto-updates.
|
||||||
|
- **Re-pair on late-arriving TXT.** When the watcher forwards a binary alone (`_ASCII.TXT` partner didn't appear within `SFM_MISSING_REPORT_GRACE_SECONDS`), the state file records `had_report: false`. On subsequent scans, the watcher re-checks whether the TXT has since arrived. If yes, the event is re-forwarded with the TXT attached — the SFM server's upsert path refreshes the DB row with the report's device-authoritative peak / project values. Without this, slow-disk or AV-interrupted TXT writes would permanently leave that event with broken-codec peaks in the SFM database. Legacy state-file entries (without the `had_report` field) default to `had_report: true` so an upgrade doesn't unexpectedly re-forward existing entries.
|
||||||
- **Quiescence + grace-period guards.** Files modified within `SFM_QUIESCENCE_SECONDS` (default 5s) are skipped to avoid forwarding mid-write. If a binary's report partner hasn't appeared after `SFM_MISSING_REPORT_GRACE_SECONDS` (default 60s), the binary is forwarded alone rather than blocking forever.
|
- **Quiescence + grace-period guards.** Files modified within `SFM_QUIESCENCE_SECONDS` (default 5s) are skipped to avoid forwarding mid-write. If a binary's report partner hasn't appeared after `SFM_MISSING_REPORT_GRACE_SECONDS` (default 60s), the binary is forwarded alone rather than blocking forever.
|
||||||
- **Per-pass rate cap.** `SFM_MAX_FORWARDS_PER_PASS` (default 500) drips first-deploy backfill instead of hammering the SFM server in one burst. At 60-second `SFM_FORWARD_INTERVAL_SECONDS` cadence that's ~30K events/hour throughput. Set to `0` for unlimited. Scan walks oldest-first so backfill advances chronologically and successive scans reliably progress.
|
- **Per-pass rate cap.** `SFM_MAX_FORWARDS_PER_PASS` (default 500) drips first-deploy backfill instead of hammering the SFM server in one burst. At 60-second `SFM_FORWARD_INTERVAL_SECONDS` cadence that's ~30K events/hour throughput. Set to `0` for unlimited. Scan walks oldest-first so backfill advances chronologically and successive scans reliably progress.
|
||||||
- **`event_forwarder.py --seed-state` CLI mode.** Walks the watch folder once, sha256s every in-window event, and marks them all as already-forwarded *without* POSTing anything. Recommended pre-deploy workflow on machines with a large historical archive — flip `SFM_FORWARD_ENABLED=true` after seeding and only events that appear from then on get forwarded.
|
- **`event_forwarder.py --seed-state` CLI mode.** Walks the watch folder once, sha256s every in-window event, and marks them all as already-forwarded *without* POSTing anything. Recommended pre-deploy workflow on machines with a large historical archive — flip `SFM_FORWARD_ENABLED=true` after seeding and only events that appear from then on get forwarded.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# Series 3 Watcher v1.5.0
|
# Series 3 Watcher v1.6.1
|
||||||
|
|
||||||
Monitors Instantel **Series 3 (Minimate)** call-in activity on a Blastware server. Runs as a **system tray app** that starts automatically on login, reports heartbeats to terra-view, and self-updates from Gitea.
|
Monitors Instantel **Series 3 (Minimate)** call-in activity on a Blastware server. Runs as a **system tray app** that starts automatically on login, reports heartbeats to terra-view, and self-updates from Gitea.
|
||||||
|
|
||||||
@@ -124,6 +124,27 @@ Combine both for a fully controlled rollout: seed-state to skip the deep archive
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Mirror (Dual-Send) — v1.6.0+
|
||||||
|
|
||||||
|
The watcher can post every heartbeat and event to a **second ("mirror") server** in addition to the primary — useful for keeping a standby box (e.g. the office NAS) as a continuous replica during a server migration, so the eventual cutover is a non-event. **Default off.**
|
||||||
|
|
||||||
|
The mirror rides along with the primary — there is no separate enable flag:
|
||||||
|
|
||||||
|
- **Heartbeat mirror** is active when `API_ENABLED = true` **and** `MIRROR_API_URL` is set. After the primary heartbeat POST, the same payload is fired at the mirror.
|
||||||
|
- **Event mirror** is active when `SFM_FORWARD_ENABLED = true` **and** `MIRROR_SFM_URL` is set. Events are re-forwarded to the mirror using a **separate** sha256 state file (`MIRROR_SFM_STATE_FILE`, blank → `<log dir>/sfm_forwarded_mirror.json`).
|
||||||
|
|
||||||
|
**The mirror can never delay or fail the primary.** The heartbeat mirror is fully wrapped and bounded by the API timeout. The event mirror runs a quick reachability probe (~3 s) first and skips the pass if the mirror server is down — skipped events stay pending in the mirror state and deliver when it returns, so nothing is lost. All mirror exceptions are swallowed-and-logged; the primary path stays exactly as reliable as before.
|
||||||
|
|
||||||
|
| Key | Description |
|
||||||
|
|-----|-------------|
|
||||||
|
| `MIRROR_API_URL` | Second heartbeat base URL, e.g. `http://10.0.0.x:8001` (blank = off) |
|
||||||
|
| `MIRROR_SFM_URL` | Second SFM base URL, e.g. `http://10.0.0.x:8200` (blank = off) |
|
||||||
|
| `MIRROR_SFM_STATE_FILE` | Path to the mirror's sha256 state file. Blank → `<log dir>/sfm_forwarded_mirror.json` |
|
||||||
|
|
||||||
|
**Seeding for a migration.** To re-deliver only the gap since a one-time replica snapshot, copy the primary state file to the mirror state file and drop entries whose `forwarded_at` is after the snapshot. SFM-side dedup covers any overlap. See [`docs/mirror-dual-send-design.md`](docs/mirror-dual-send-design.md).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Tray Icon
|
## Tray Icon
|
||||||
|
|
||||||
| Colour | Meaning |
|
| Colour | Meaning |
|
||||||
@@ -153,9 +174,21 @@ To view connected watchers: **Settings → Developer → Watcher Manager**.
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
## Roadmap (Future)
|
||||||
|
|
||||||
|
Deferred work — parked but worth tracking. Pairs with seismo-relay's
|
||||||
|
[Roadmap (Future)](https://gitea.serversdown.net/serversdown/seismo-relay#roadmap-future)
|
||||||
|
where the corresponding server-side work lives.
|
||||||
|
|
||||||
|
- [ ] **File archive manager.** Move BW autocall-home events older than ~90 days into `<watch_folder>_archive/<year>/<month>/` subfolders so the active watch directory doesn't accumulate hundreds of thousands of entries (filesystem dir lookups slow at 100K+, BW UI hangs opening the folder, watcher's own scandir gets expensive). Plan drafted in the codec-RE branch's plan-mode session, including a critical pre-coding test (does Blastware UI walk subfolders or only see the flat watch folder?) that determines the archive layout (in-place subfolders vs sibling archive). Default-off, dry-run mode, opt-in per machine.
|
||||||
|
- [ ] **MLG forwarding.** Currently the watcher's `is_event_binary()` filter explicitly excludes `.MLG` per-unit monitor log files — only event binaries (`.AB0` / `.PG0H` / etc.) and their paired `_ASCII.TXT` reports get forwarded. Adding an `POST /db/import/mlg_file` SFM endpoint + a parallel `.MLG` scan path on the watcher would populate `monitor_log` rows for non-ACH-routed units (coverage queries, "was this unit monitoring on date X" lookups). MLG files are append-only / mutable so the watcher needs a different dedup strategy than the per-event sha256 state file — better to forward whole file every scan and let the server dedup by `(serial, start_time)` on insert.
|
||||||
|
- [ ] **Pre-deploy seed-state UX in the Settings dialog.** Currently `event_forwarder.py --seed-state` is a CLI-only operation. A "Skip backfill" button next to the SFM Forward checkbox would let operators opt-out of re-forwarding the historical archive without dropping to a command line.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## Versioning
|
## Versioning
|
||||||
|
|
||||||
Follows **Semantic Versioning**. Current release: **v1.5.0**.
|
Follows **Semantic Versioning**. Current release: **v1.6.1**.
|
||||||
See `CHANGELOG.md` for full history.
|
See `CHANGELOG.md` for full history.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -72,3 +72,14 @@ SFM_STATE_FILE =
|
|||||||
# `--seed-state` workflow that skips the historical backfill entirely.
|
# `--seed-state` workflow that skips the historical backfill entirely.
|
||||||
SFM_MAX_FORWARDS_PER_PASS = 500
|
SFM_MAX_FORWARDS_PER_PASS = 500
|
||||||
|
|
||||||
|
# --- Mirror (dual-send) ---
|
||||||
|
# Optional best-effort second destination: post each heartbeat and event
|
||||||
|
# to a mirror server (e.g. the office NAS) IN ADDITION to the primary
|
||||||
|
# above. Default off (blank URLs). The mirror can never delay or fail
|
||||||
|
# the primary; the event mirror keeps its own state file so nothing is
|
||||||
|
# lost while it's down. See docs/mirror-dual-send-design.md.
|
||||||
|
MIRROR_API_URL = ; base URL e.g. http://10.0.0.x:8001 (blank = off)
|
||||||
|
MIRROR_SFM_URL = ; base URL e.g. http://10.0.0.x:8200 (blank = off)
|
||||||
|
# Blank → defaults to <log dir>/sfm_forwarded_mirror.json
|
||||||
|
MIRROR_SFM_STATE_FILE =
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
# Watcher Mirror (Dual-Send) — Design
|
||||||
|
|
||||||
|
**Date:** 2026-06-24 · **Branch:** `feat/mirror-dual-send` (both `thor-watcher` + `series3-watcher`) · **Status:** approved, implementing
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
Let each watcher post every heartbeat and event to a **second ("mirror") server** in addition to the primary, so the office NAS stays a continuous replica of prod during the migration. The eventual cutover becomes a non-event. **Default off** → existing installs unchanged.
|
||||||
|
|
||||||
|
## Pattern
|
||||||
|
Classic dual-write migration. The primary path stays **exactly as reliable as today**; the mirror is pure best-effort bonus that can **never delay or fail** the primary.
|
||||||
|
|
||||||
|
## The two watchers (parallel changes)
|
||||||
|
| | thor-watcher (series4) | series3-watcher (s3) |
|
||||||
|
|---|---|---|
|
||||||
|
| Config | `config.json` (JSON) | `config.ini` (`[agent]`, INI) |
|
||||||
|
| Heartbeat fn | `send_api_payload(payload, api_url, timeout)` | `send_api_payload(payload, api_url)` |
|
||||||
|
| Event fn | `event_forwarder.forward_pending(root, sfm_url, state, …)` | same (`event_forwarder` is a sibling port) |
|
||||||
|
| Loop | `series4_ingest.run_watcher` | `series3_watcher.run_watcher` |
|
||||||
|
| Settings GUI | `thor_settings_dialog.py` | `settings_dialog.py` |
|
||||||
|
|
||||||
|
## Design (Approach A — reliable event mirror, best-effort heartbeat)
|
||||||
|
|
||||||
|
### Config additions (default empty = off)
|
||||||
|
- thor (`config.json`): `mirror_api_url`, `mirror_sfm_url`, `mirror_sfm_state_file` (blank → `<log_dir>/thor_forwarded_mirror.json`).
|
||||||
|
- s3 (`config.ini`): `MIRROR_API_URL`, `MIRROR_SFM_URL`, `MIRROR_SFM_STATE_FILE` (blank → `sfm_forwarded_mirror.json`).
|
||||||
|
|
||||||
|
The mirror **rides along** with the primary: heartbeat-mirror active iff primary heartbeat on **and** `mirror_api_url` set; event-mirror active iff primary forwarding on **and** `mirror_sfm_url` set. No separate enable flag.
|
||||||
|
|
||||||
|
### Heartbeat mirror (best-effort)
|
||||||
|
After the primary `send_api_payload`, if `mirror_api_url` is set, fire one more `send_api_payload(payload, mirror_api_url, …)`. Wrapped so it can never raise into the loop; result ignored except a debug log. Bounded by the existing short API timeout (~5 s).
|
||||||
|
|
||||||
|
### Event mirror (reliable, isolated)
|
||||||
|
- Init a **second** `ForwardState(mirror_state_path)` alongside the primary state.
|
||||||
|
- Each forward tick, **after** the primary `forward_pending`, run a second `forward_pending(root, mirror_sfm_url, mirror_state, …)` in its **own** try/except.
|
||||||
|
- **Reachability guard:** before the mirror forward, a quick TCP/HTTP probe of `mirror_sfm_url` (~3 s). If unreachable → skip this pass, log, retry next tick. Prevents a down NAS from stalling the loop on per-event timeouts. The mirror state file means skipped events stay pending → delivered when the NAS returns. **No data loss.**
|
||||||
|
- Reliable + idempotent via its own sha256 state, fully independent of the primary's state.
|
||||||
|
|
||||||
|
### Isolation invariant (the one rule)
|
||||||
|
Nothing on the mirror path can delay or fail the primary: separate state file, its own try/except, the reachability guard + bounded timeouts, all exceptions swallowed-and-logged.
|
||||||
|
|
||||||
|
### Settings dialog
|
||||||
|
Add `Mirror API URL` + `Mirror SFM URL` fields (blank = off) to each watcher's settings dialog.
|
||||||
|
|
||||||
|
## Seeding (operational, for this migration)
|
||||||
|
The mirror state file should reflect what the NAS already has (everything ≤ the migration snapshot, ~16:23 on 2026-06-24). Recipe: copy the primary state file → mirror state file, then drop entries whose `forwarded_at` is after the snapshot, so the mirror re-delivers exactly the gap. SFM-side dedup covers any overlap. (If a watcher had no events in the gap, a straight copy suffices.)
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
- thor `test_event_forwarder.py`: mirror uses a separate state file (idempotent, independent); a mirror failure leaves the primary's result + state untouched; reachability guard skips cleanly when the mirror is down.
|
||||||
|
- s3: add a minimal test for the same isolation/idempotency.
|
||||||
|
|
||||||
|
## Out of scope / handoff
|
||||||
|
Rebuilding the Windows installers + redeploying (s3 on the Win7 box, thor on the Win10 box) is the operator's job — this change is **code + tests only**.
|
||||||
+144
-6
@@ -218,11 +218,53 @@ class ForwardState:
|
|||||||
def is_forwarded(self, sha256: str) -> bool:
|
def is_forwarded(self, sha256: str) -> bool:
|
||||||
return sha256 in self._data["forwarded"]
|
return sha256 in self._data["forwarded"]
|
||||||
|
|
||||||
def mark_forwarded(self, sha256: str, filename: str, size: int) -> None:
|
def status(self, sha256: str) -> Optional[bool]:
|
||||||
|
"""Return forwarding status for *sha256*.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
None — never forwarded. Eligible for a fresh forward.
|
||||||
|
True — forwarded successfully with its paired report
|
||||||
|
(or in a legacy entry that pre-dates the
|
||||||
|
had_report field — assumed complete for safety).
|
||||||
|
NOT a candidate for re-forward.
|
||||||
|
False — forwarded WITHOUT its paired ``_ASCII.TXT``
|
||||||
|
(BW's TXT-write lagged past the grace period).
|
||||||
|
Eligible for re-forward IF the TXT now exists,
|
||||||
|
so the SFM server's upsert path can refresh the
|
||||||
|
DB row with the report's authoritative values.
|
||||||
|
|
||||||
|
Legacy state-file entries without a ``had_report`` key default
|
||||||
|
to ``True`` so an upgrade doesn't unexpectedly re-forward
|
||||||
|
every entry the operator has accumulated.
|
||||||
|
"""
|
||||||
|
entry = self._data["forwarded"].get(sha256)
|
||||||
|
if entry is None:
|
||||||
|
return None
|
||||||
|
return bool(entry.get("had_report", True))
|
||||||
|
|
||||||
|
def mark_forwarded(
|
||||||
|
self,
|
||||||
|
sha256: str,
|
||||||
|
filename: str,
|
||||||
|
size: int,
|
||||||
|
had_report: bool = True,
|
||||||
|
) -> None:
|
||||||
|
"""Record a successful forward.
|
||||||
|
|
||||||
|
Set ``had_report=False`` when the forward shipped the binary
|
||||||
|
without its paired ASCII report. Such entries are re-checked
|
||||||
|
on subsequent scans and re-forwarded once the TXT appears, so
|
||||||
|
SFM's upsert refreshes the DB row with the device-authoritative
|
||||||
|
peak/project values.
|
||||||
|
|
||||||
|
Idempotent: re-marking an existing sha256 with ``had_report=True``
|
||||||
|
is the explicit promotion path used when a re-pair succeeds.
|
||||||
|
"""
|
||||||
self._data["forwarded"][sha256] = {
|
self._data["forwarded"][sha256] = {
|
||||||
"filename": filename,
|
"filename": filename,
|
||||||
"size": size,
|
"size": size,
|
||||||
"forwarded_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
|
"forwarded_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
|
||||||
|
"had_report": had_report,
|
||||||
}
|
}
|
||||||
self._save()
|
self._save()
|
||||||
|
|
||||||
@@ -340,12 +382,19 @@ def find_pending_events(
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
# Idempotency: skip if we already forwarded this content
|
# Idempotency: skip if we already forwarded this content
|
||||||
|
# successfully. Three cases via state.status(digest):
|
||||||
|
# True — forwarded WITH report → permanently done, skip.
|
||||||
|
# False — forwarded WITHOUT report → re-pair candidate.
|
||||||
|
# Forward again only if a paired TXT is now present
|
||||||
|
# so SFM's upsert refreshes the DB row.
|
||||||
|
# None — never forwarded → normal first-forward path.
|
||||||
try:
|
try:
|
||||||
digest = sha256_of_file(e.path)
|
digest = sha256_of_file(e.path)
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
log.warning("forward scan: sha256 failed for %s: %s", e.path, exc)
|
log.warning("forward scan: sha256 failed for %s: %s", e.path, exc)
|
||||||
continue
|
continue
|
||||||
if state.is_forwarded(digest):
|
fwd_status = state.status(digest)
|
||||||
|
if fwd_status is True:
|
||||||
skipped_already_forwarded += 1
|
skipped_already_forwarded += 1
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -380,9 +429,19 @@ def find_pending_events(
|
|||||||
txt_path = candidate
|
txt_path = candidate
|
||||||
# else: TXT is mid-write; treat as not-yet-paired and defer.
|
# else: TXT is mid-write; treat as not-yet-paired and defer.
|
||||||
|
|
||||||
if txt_path is None:
|
if fwd_status is False:
|
||||||
# No TXT (or not yet quiescent). Wait for the grace
|
# Previously forwarded WITHOUT report. We're here looking
|
||||||
# period before forwarding alone.
|
# for a re-pair opportunity. If the TXT is now present
|
||||||
|
# and quiescent, include in pending for re-forward (the
|
||||||
|
# SFM server's upsert will refresh the DB row with the
|
||||||
|
# report's authoritative values). Otherwise skip — no
|
||||||
|
# point re-forwarding the same binary alone again.
|
||||||
|
if txt_path is None:
|
||||||
|
skipped_already_forwarded += 1
|
||||||
|
continue
|
||||||
|
elif txt_path is None:
|
||||||
|
# First-time forward and TXT not yet present. Wait for the
|
||||||
|
# grace period before forwarding alone.
|
||||||
if (now_ts - mtime) < missing_report_grace_seconds:
|
if (now_ts - mtime) < missing_report_grace_seconds:
|
||||||
skipped_inflight += 1
|
skipped_inflight += 1
|
||||||
continue
|
continue
|
||||||
@@ -575,7 +634,18 @@ def forward_pending(
|
|||||||
try:
|
try:
|
||||||
digest = sha256_of_file(binary_path)
|
digest = sha256_of_file(binary_path)
|
||||||
size = os.path.getsize(binary_path)
|
size = os.path.getsize(binary_path)
|
||||||
state.mark_forwarded(digest, os.path.basename(binary_path), size)
|
# Record whether this forward shipped a paired TXT.
|
||||||
|
# Forwards without a TXT are flagged had_report=False so
|
||||||
|
# subsequent scans re-check whether the TXT has since
|
||||||
|
# appeared and trigger a re-forward (the SFM server's
|
||||||
|
# upsert path refreshes the DB row with the report's
|
||||||
|
# authoritative values).
|
||||||
|
state.mark_forwarded(
|
||||||
|
digest,
|
||||||
|
os.path.basename(binary_path),
|
||||||
|
size,
|
||||||
|
had_report=(txt_path is not None),
|
||||||
|
)
|
||||||
except OSError as exc:
|
except OSError as exc:
|
||||||
_log(f"[forward] post-success state save failed for "
|
_log(f"[forward] post-success state save failed for "
|
||||||
f"{os.path.basename(binary_path)}: {exc}")
|
f"{os.path.basename(binary_path)}: {exc}")
|
||||||
@@ -614,6 +684,74 @@ def forward_pending(
|
|||||||
return counts
|
return counts
|
||||||
|
|
||||||
|
|
||||||
|
# ── Mirror (dual-send) ────────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def mirror_reachable(base_url: str, timeout: float = 3.0) -> bool:
|
||||||
|
"""Quick liveness probe of a mirror SFM base URL.
|
||||||
|
|
||||||
|
Run before a mirror forward pass so an unreachable mirror can't stall
|
||||||
|
the watcher loop on a per-event HTTP timeout for every pending file.
|
||||||
|
Probes ``<base>/health`` (the SFM server exposes it). ANY HTTP
|
||||||
|
response — even 404/500 — means the server is up, so proceed; only a
|
||||||
|
connection-level failure (refused / timed out / DNS) counts as down.
|
||||||
|
Best-effort: never raises.
|
||||||
|
"""
|
||||||
|
if not base_url:
|
||||||
|
return False
|
||||||
|
url = base_url.rstrip("/") + "/health"
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(
|
||||||
|
urllib.request.Request(url, method="GET"), timeout=timeout
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
except urllib.error.HTTPError:
|
||||||
|
return True # got a status back → server is alive
|
||||||
|
except Exception:
|
||||||
|
return False # refused / timeout / DNS / socket → down
|
||||||
|
|
||||||
|
|
||||||
|
def mirror_forward_pass(
|
||||||
|
watch_dir: str,
|
||||||
|
mirror_url: str,
|
||||||
|
mirror_state: ForwardState,
|
||||||
|
*,
|
||||||
|
reachable_fn=mirror_reachable,
|
||||||
|
reachable_timeout: float = 3.0,
|
||||||
|
**forward_kwargs: Any,
|
||||||
|
) -> Optional[Dict[str, int]]:
|
||||||
|
"""One best-effort forwarding pass against a *mirror* SFM server.
|
||||||
|
|
||||||
|
The dual-send entry point. Wraps :func:`forward_pending` with two
|
||||||
|
properties that keep the mirror from ever harming the primary path:
|
||||||
|
|
||||||
|
1. **Fast-fail reachability guard** — probe ``mirror_url`` first; if
|
||||||
|
it's down, return ``None`` immediately rather than let
|
||||||
|
:func:`forward_pending` block on a per-event timeout for every
|
||||||
|
pending file.
|
||||||
|
2. **Total exception isolation** — any error (probe, forward, state
|
||||||
|
I/O) is swallowed and reported as ``None``. This function NEVER
|
||||||
|
raises, so the caller's already-completed primary forward is
|
||||||
|
untouched.
|
||||||
|
|
||||||
|
The mirror keeps its OWN ``ForwardState`` file, so its idempotency
|
||||||
|
and retry are independent of the primary's. Nothing is lost while
|
||||||
|
the mirror is down — skipped events stay pending and are delivered
|
||||||
|
on a later pass once it returns.
|
||||||
|
|
||||||
|
Returns the :func:`forward_pending` counts dict on a completed pass,
|
||||||
|
or ``None`` if the mirror was unreachable or the pass raised.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
if not mirror_url:
|
||||||
|
return None
|
||||||
|
if not reachable_fn(mirror_url, reachable_timeout):
|
||||||
|
return None
|
||||||
|
return forward_pending(watch_dir, mirror_url, mirror_state, **forward_kwargs)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
# ── Seed-state mode (skip historical backfill on first deploy) ────────────────
|
# ── Seed-state mode (skip historical backfill on first deploy) ────────────────
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
[Setup]
|
[Setup]
|
||||||
AppName=Series 3 Watcher
|
AppName=Series 3 Watcher
|
||||||
AppVersion=1.5.0
|
AppVersion=1.6.1
|
||||||
AppPublisher=Terra-Mechanics Inc.
|
AppPublisher=Terra-Mechanics Inc.
|
||||||
DefaultDirName={pf}\Series3Watcher
|
DefaultDirName={pf}\Series3Watcher
|
||||||
DefaultGroupName=Series 3 Watcher
|
DefaultGroupName=Series 3 Watcher
|
||||||
|
|||||||
+7
-4
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Series 3 Watcher — System Tray Launcher v1.5.0
|
Series 3 Watcher — System Tray Launcher v1.6.1
|
||||||
Requires: pystray, Pillow, tkinter (stdlib)
|
Requires: pystray, Pillow, tkinter (stdlib)
|
||||||
|
|
||||||
Run with: pythonw series3_tray.py (no console window)
|
Run with: pythonw series3_tray.py (no console window)
|
||||||
@@ -335,7 +335,8 @@ def _show_cancel_message():
|
|||||||
class WatcherTray:
|
class WatcherTray:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.state = {}
|
self.state = {}
|
||||||
self.stop_event = threading.Event()
|
self.stop_event = threading.Event() # watcher lifecycle; replaced on restart
|
||||||
|
self._app_stop = threading.Event() # app lifetime; only set on exit/update
|
||||||
self._watcher_thread = None
|
self._watcher_thread = None
|
||||||
self._icon = None
|
self._icon = None
|
||||||
# Lock guards _rebuild_menu calls from the updater thread
|
# Lock guards _rebuild_menu calls from the updater thread
|
||||||
@@ -394,6 +395,7 @@ class WatcherTray:
|
|||||||
subprocess.Popen(["explorer", HERE])
|
subprocess.Popen(["explorer", HERE])
|
||||||
|
|
||||||
def _exit(self, icon, item):
|
def _exit(self, icon, item):
|
||||||
|
self._app_stop.set()
|
||||||
self.stop_event.set()
|
self.stop_event.set()
|
||||||
icon.stop()
|
icon.stop()
|
||||||
|
|
||||||
@@ -459,7 +461,7 @@ class WatcherTray:
|
|||||||
last_status = None
|
last_status = None
|
||||||
update_check_counter = 0 # check for updates every ~5 min (30 * 10s ticks)
|
update_check_counter = 0 # check for updates every ~5 min (30 * 10s ticks)
|
||||||
|
|
||||||
while not self.stop_event.is_set():
|
while not self._app_stop.is_set():
|
||||||
icon_status = self._tray_status()
|
icon_status = self._tray_status()
|
||||||
|
|
||||||
if self._icon is not None:
|
if self._icon is not None:
|
||||||
@@ -487,7 +489,7 @@ class WatcherTray:
|
|||||||
self._do_update(url)
|
self._do_update(url)
|
||||||
return # exit loop; swap bat will relaunch
|
return # exit loop; swap bat will relaunch
|
||||||
|
|
||||||
self.stop_event.wait(timeout=10)
|
self._app_stop.wait(timeout=10)
|
||||||
|
|
||||||
def _do_update(self, download_url=None):
|
def _do_update(self, download_url=None):
|
||||||
"""Notify tray icon then apply update. If url is None, fetch it first."""
|
"""Notify tray icon then apply update. If url is None, fetch it first."""
|
||||||
@@ -502,6 +504,7 @@ class WatcherTray:
|
|||||||
|
|
||||||
success = apply_update(download_url)
|
success = apply_update(download_url)
|
||||||
if success:
|
if success:
|
||||||
|
self._app_stop.set()
|
||||||
self.stop_event.set()
|
self.stop_event.set()
|
||||||
if self._icon is not None:
|
if self._icon is not None:
|
||||||
self._icon.stop()
|
self._icon.stop()
|
||||||
|
|||||||
+70
-7
@@ -110,6 +110,12 @@ def load_config(path: str) -> Dict[str, Any]:
|
|||||||
# first deploy in a folder that's been accumulating for years.
|
# first deploy in a folder that's been accumulating for years.
|
||||||
# See README "First-time deployment" section.
|
# See README "First-time deployment" section.
|
||||||
"SFM_MAX_FORWARDS_PER_PASS": get_int("SFM_MAX_FORWARDS_PER_PASS", 500),
|
"SFM_MAX_FORWARDS_PER_PASS": get_int("SFM_MAX_FORWARDS_PER_PASS", 500),
|
||||||
|
|
||||||
|
# Mirror (dual-send) — best-effort second destination, default OFF.
|
||||||
|
# See docs/mirror-dual-send-design.md.
|
||||||
|
"MIRROR_API_URL": get_str("MIRROR_API_URL", ""),
|
||||||
|
"MIRROR_SFM_URL": get_str("MIRROR_SFM_URL", ""),
|
||||||
|
"MIRROR_SFM_STATE_FILE": get_str("MIRROR_SFM_STATE_FILE", ""),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -231,13 +237,16 @@ def scan_latest(
|
|||||||
uid = cached[1]
|
uid = cached[1]
|
||||||
else:
|
else:
|
||||||
uid = sniff_unit_from_mlg(fpath, header_bytes)
|
uid = sniff_unit_from_mlg(fpath, header_bytes)
|
||||||
if not uid:
|
# Cache the result either way — including unsniffable (uid=None) —
|
||||||
# If unsniffable but very recent, log for later inspection
|
# so the same file isn't re-sniffed and re-logged on every scan.
|
||||||
if (recent_cutoff is not None) and (mtime >= recent_cutoff):
|
|
||||||
if logger:
|
|
||||||
logger("[unsniffable-recent] {}".format(fpath))
|
|
||||||
continue # skip file if no unit ID found in header
|
|
||||||
cache[fpath] = (mtime, uid)
|
cache[fpath] = (mtime, uid)
|
||||||
|
if (not uid) and (recent_cutoff is not None) and (mtime >= recent_cutoff):
|
||||||
|
# Log once, on first sight, for later inspection.
|
||||||
|
if logger:
|
||||||
|
logger("[unsniffable-recent] {}".format(fpath))
|
||||||
|
|
||||||
|
if not uid:
|
||||||
|
continue # no unit ID in header — skip (cached above, won't re-log)
|
||||||
|
|
||||||
if (uid not in latest) or (mtime > latest[uid]["mtime"]):
|
if (uid not in latest) or (mtime > latest[uid]["mtime"]):
|
||||||
latest[uid] = {"mtime": mtime, "fname": e.name, "path": fpath}
|
latest[uid] = {"mtime": mtime, "fname": e.name, "path": fpath}
|
||||||
@@ -247,7 +256,7 @@ def scan_latest(
|
|||||||
|
|
||||||
|
|
||||||
# --- API heartbeat / SFM telemetry helpers ---
|
# --- API heartbeat / SFM telemetry helpers ---
|
||||||
VERSION = "1.5.0"
|
VERSION = "1.6.1"
|
||||||
|
|
||||||
|
|
||||||
def _read_log_tail(log_file: str, n: int = 25) -> Optional[list]:
|
def _read_log_tail(log_file: str, n: int = 25) -> Optional[list]:
|
||||||
@@ -427,6 +436,33 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
else:
|
else:
|
||||||
print("[CFG] SFM_FORWARD_ENABLED=false (event forwarding disabled)")
|
print("[CFG] SFM_FORWARD_ENABLED=false (event forwarding disabled)")
|
||||||
|
|
||||||
|
# ---- Mirror (dual-send) setup ----
|
||||||
|
# Best-effort second destination. The event mirror gets its OWN state
|
||||||
|
# file so its idempotency is independent of the primary's.
|
||||||
|
MIRROR_API_URL = cfg.get("MIRROR_API_URL", "")
|
||||||
|
MIRROR_SFM_URL = cfg.get("MIRROR_SFM_URL", "")
|
||||||
|
mirror_state = None
|
||||||
|
if sfm_state is not None and MIRROR_SFM_URL:
|
||||||
|
try:
|
||||||
|
from event_forwarder import ForwardState
|
||||||
|
mirror_state_file = cfg.get("MIRROR_SFM_STATE_FILE") or os.path.join(
|
||||||
|
os.path.dirname(LOG_FILE) or here, "sfm_forwarded_mirror.json"
|
||||||
|
)
|
||||||
|
mirror_state = ForwardState(mirror_state_file)
|
||||||
|
print("[CFG] mirror event forwarding → {} state={} ({} known)".format(
|
||||||
|
MIRROR_SFM_URL, mirror_state_file, mirror_state.count()))
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[mirror] event mirror enabled url={} state={} known={}".format(
|
||||||
|
MIRROR_SFM_URL, mirror_state_file, mirror_state.count()))
|
||||||
|
except Exception as e:
|
||||||
|
print("[WARN] mirror forwarder init failed: {}".format(e))
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[warn] mirror forwarder init failed: {}".format(e))
|
||||||
|
mirror_state = None
|
||||||
|
if MIRROR_API_URL:
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[mirror] heartbeat mirror enabled url={}".format(MIRROR_API_URL))
|
||||||
|
|
||||||
while not stop_event.is_set():
|
while not stop_event.is_set():
|
||||||
try:
|
try:
|
||||||
now_local = datetime.now().isoformat()
|
now_local = datetime.now().isoformat()
|
||||||
@@ -497,6 +533,13 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
hb_payload["log_tail"] = _read_log_tail(cfg.get("LOG_FILE", ""), 25)
|
hb_payload["log_tail"] = _read_log_tail(cfg.get("LOG_FILE", ""), 25)
|
||||||
response = send_api_payload(hb_payload, cfg.get("API_URL", ""))
|
response = send_api_payload(hb_payload, cfg.get("API_URL", ""))
|
||||||
last_api_ts = now_ts
|
last_api_ts = now_ts
|
||||||
|
# Best-effort heartbeat mirror — never affects primary.
|
||||||
|
if MIRROR_API_URL:
|
||||||
|
try:
|
||||||
|
send_api_payload(hb_payload, MIRROR_API_URL)
|
||||||
|
except Exception as e:
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[mirror] heartbeat post failed: {}".format(e))
|
||||||
if response is not None:
|
if response is not None:
|
||||||
state["api_status"] = "ok"
|
state["api_status"] = "ok"
|
||||||
state["last_api"] = datetime.now()
|
state["last_api"] = datetime.now()
|
||||||
@@ -550,6 +593,26 @@ def run_watcher(state: Dict[str, Any], stop_event: threading.Event) -> None:
|
|||||||
print(err)
|
print(err)
|
||||||
log_message(LOG_FILE, ENABLE_LOGGING, err)
|
log_message(LOG_FILE, ENABLE_LOGGING, err)
|
||||||
state["sfm_status"] = "fail"
|
state["sfm_status"] = "fail"
|
||||||
|
|
||||||
|
# Best-effort event mirror — own state, fast-fail guard,
|
||||||
|
# fully isolated. Runs after the primary each tick.
|
||||||
|
if mirror_state is not None:
|
||||||
|
from event_forwarder import mirror_forward_pass
|
||||||
|
m_counts = mirror_forward_pass(
|
||||||
|
WATCH_PATH, MIRROR_SFM_URL, mirror_state,
|
||||||
|
max_age_days=MAX_EVENT_AGE_DAYS,
|
||||||
|
quiescence_seconds=int(cfg.get("SFM_QUIESCENCE_SECONDS", 5)),
|
||||||
|
missing_report_grace_seconds=int(cfg.get("SFM_MISSING_REPORT_GRACE_SECONDS", 60)),
|
||||||
|
timeout=int(cfg.get("SFM_HTTP_TIMEOUT", 60)),
|
||||||
|
max_per_pass=int(cfg.get("SFM_MAX_FORWARDS_PER_PASS", 500)),
|
||||||
|
logger=lambda m: log_message(LOG_FILE, ENABLE_LOGGING, "[mirror] " + m),
|
||||||
|
)
|
||||||
|
if m_counts is None:
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[mirror] forward skipped (unreachable/error): {}".format(MIRROR_SFM_URL))
|
||||||
|
else:
|
||||||
|
log_message(LOG_FILE, ENABLE_LOGGING,
|
||||||
|
"[mirror] pass forwarded={forwarded} errors={errors}".format(**m_counts))
|
||||||
else:
|
else:
|
||||||
state["sfm_status"] = "disabled"
|
state["sfm_status"] = "disabled"
|
||||||
|
|
||||||
|
|||||||
+77
-1
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Series 3 Watcher — Settings Dialog v1.5.0
|
Series 3 Watcher — Settings Dialog v1.6.1
|
||||||
|
|
||||||
Provides a Tkinter settings dialog that doubles as a first-run wizard.
|
Provides a Tkinter settings dialog that doubles as a first-run wizard.
|
||||||
|
|
||||||
@@ -53,6 +53,11 @@ DEFAULTS = {
|
|||||||
"SFM_HTTP_TIMEOUT": "60",
|
"SFM_HTTP_TIMEOUT": "60",
|
||||||
"SFM_STATE_FILE": "",
|
"SFM_STATE_FILE": "",
|
||||||
"SFM_MAX_FORWARDS_PER_PASS": "500",
|
"SFM_MAX_FORWARDS_PER_PASS": "500",
|
||||||
|
|
||||||
|
# Mirror (dual-send) — best-effort second destination, default OFF.
|
||||||
|
"MIRROR_API_URL": "",
|
||||||
|
"MIRROR_SFM_URL": "",
|
||||||
|
"MIRROR_SFM_STATE_FILE": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -260,6 +265,14 @@ class SettingsDialog:
|
|||||||
self.var_sfm_state_file = tk.StringVar(value=v["SFM_STATE_FILE"])
|
self.var_sfm_state_file = tk.StringVar(value=v["SFM_STATE_FILE"])
|
||||||
self.var_sfm_max_per_pass = tk.StringVar(value=v["SFM_MAX_FORWARDS_PER_PASS"])
|
self.var_sfm_max_per_pass = tk.StringVar(value=v["SFM_MAX_FORWARDS_PER_PASS"])
|
||||||
|
|
||||||
|
# Mirror (dual-send) — best-effort second destination
|
||||||
|
_raw_mirror = v["MIRROR_API_URL"]
|
||||||
|
if _raw_mirror.endswith(_suffix):
|
||||||
|
_raw_mirror = _raw_mirror[:-len(_suffix)]
|
||||||
|
self.var_mirror_api_url = tk.StringVar(value=_raw_mirror)
|
||||||
|
self.var_mirror_sfm_url = tk.StringVar(value=v["MIRROR_SFM_URL"])
|
||||||
|
self.var_mirror_sfm_state_file = tk.StringVar(value=v["MIRROR_SFM_STATE_FILE"])
|
||||||
|
|
||||||
# --- UI construction ---
|
# --- UI construction ---
|
||||||
|
|
||||||
def _build_ui(self):
|
def _build_ui(self):
|
||||||
@@ -288,6 +301,7 @@ class SettingsDialog:
|
|||||||
self._build_tab_logging(nb)
|
self._build_tab_logging(nb)
|
||||||
self._build_tab_updates(nb)
|
self._build_tab_updates(nb)
|
||||||
self._build_tab_sfm(nb)
|
self._build_tab_sfm(nb)
|
||||||
|
self._build_tab_mirror(nb)
|
||||||
|
|
||||||
# Buttons
|
# Buttons
|
||||||
btn_frame = tk.Frame(outer)
|
btn_frame = tk.Frame(outer)
|
||||||
@@ -566,6 +580,58 @@ class SettingsDialog:
|
|||||||
row=8, column=0, columnspan=2, sticky="w", padx=(8, 8), pady=(8, 4)
|
row=8, column=0, columnspan=2, sticky="w", padx=(8, 8), pady=(8, 4)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _build_tab_mirror(self, nb):
|
||||||
|
"""Optional dual-send mirror: a second heartbeat + event destination."""
|
||||||
|
f = self._tab_frame(nb, "Mirror")
|
||||||
|
|
||||||
|
intro = (
|
||||||
|
"Optional dual-send: post each heartbeat and event to a SECOND\n"
|
||||||
|
"(\"mirror\") server in addition to the primary. Best-effort — the\n"
|
||||||
|
"mirror can never delay or fail the primary. Leave blank to disable."
|
||||||
|
)
|
||||||
|
tk.Label(f, text=intro, justify="left", fg="#1a5276", wraplength=420).grid(
|
||||||
|
row=0, column=0, columnspan=2, sticky="w", padx=(8, 8), pady=(6, 8)
|
||||||
|
)
|
||||||
|
|
||||||
|
_add_label_entry(f, 1, "Mirror Terra-View URL", self.var_mirror_api_url)
|
||||||
|
_add_label_entry(f, 2, "Mirror SFM URL", self.var_mirror_sfm_url)
|
||||||
|
|
||||||
|
tk.Label(f, text="Mirror State File", anchor="w").grid(
|
||||||
|
row=3, column=0, sticky="w", padx=(8, 4), pady=4
|
||||||
|
)
|
||||||
|
state_frame = tk.Frame(f)
|
||||||
|
state_frame.grid(row=3, column=1, sticky="ew", padx=(0, 8), pady=4)
|
||||||
|
state_frame.columnconfigure(0, weight=1)
|
||||||
|
ttk.Entry(state_frame, textvariable=self.var_mirror_sfm_state_file, width=32).grid(
|
||||||
|
row=0, column=0, sticky="ew"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _browse_mirror_state():
|
||||||
|
path = filedialog.asksaveasfilename(
|
||||||
|
title="Mirror forward-state file",
|
||||||
|
defaultextension=".json",
|
||||||
|
filetypes=[("JSON", "*.json"), ("All Files", "*.*")],
|
||||||
|
initialfile="sfm_forwarded_mirror.json",
|
||||||
|
)
|
||||||
|
if path:
|
||||||
|
self.var_mirror_sfm_state_file.set(path)
|
||||||
|
|
||||||
|
ttk.Button(state_frame, text="Browse...", width=10,
|
||||||
|
command=_browse_mirror_state).grid(row=0, column=1, padx=(4, 0))
|
||||||
|
|
||||||
|
hint_text = (
|
||||||
|
"Mirror Terra-View URL → base like http://10.0.0.x:8001 (heartbeats).\n"
|
||||||
|
"Mirror SFM URL → base like http://10.0.0.x:8200 (events).\n\n"
|
||||||
|
"The event mirror keeps its OWN state file, so nothing is lost while\n"
|
||||||
|
"the mirror is down — pending events are delivered once it returns.\n"
|
||||||
|
"A quick reachability check skips the mirror cleanly when unreachable,\n"
|
||||||
|
"so the primary is never slowed.\n"
|
||||||
|
"State File blank → defaults to <log dir>/sfm_forwarded_mirror.json."
|
||||||
|
)
|
||||||
|
tk.Label(f, text=hint_text, justify="left", fg="#555555", wraplength=420).grid(
|
||||||
|
row=4, column=0, columnspan=2, sticky="w", padx=(8, 8), pady=(8, 4)
|
||||||
|
)
|
||||||
|
|
||||||
def _test_sfm_connection(self):
|
def _test_sfm_connection(self):
|
||||||
"""GET <sfm_url>/health and show the result."""
|
"""GET <sfm_url>/health and show the result."""
|
||||||
import urllib.request
|
import urllib.request
|
||||||
@@ -673,6 +739,12 @@ class SettingsDialog:
|
|||||||
else:
|
else:
|
||||||
api_url = api_url.rstrip("/") + "/api/series3/heartbeat"
|
api_url = api_url.rstrip("/") + "/api/series3/heartbeat"
|
||||||
|
|
||||||
|
# Mirror (dual-send) — best-effort second destination.
|
||||||
|
mirror_api_url = self.var_mirror_api_url.get().strip()
|
||||||
|
if mirror_api_url:
|
||||||
|
mirror_api_url = mirror_api_url.rstrip("/") + "/api/series3/heartbeat"
|
||||||
|
mirror_sfm_url = self.var_mirror_sfm_url.get().strip().rstrip("/")
|
||||||
|
|
||||||
values = {
|
values = {
|
||||||
"API_ENABLED": "true" if self.var_api_enabled.get() else "false",
|
"API_ENABLED": "true" if self.var_api_enabled.get() else "false",
|
||||||
"API_URL": api_url,
|
"API_URL": api_url,
|
||||||
@@ -698,6 +770,10 @@ class SettingsDialog:
|
|||||||
"SFM_HTTP_TIMEOUT": str(int_values["SFM HTTP Timeout"]),
|
"SFM_HTTP_TIMEOUT": str(int_values["SFM HTTP Timeout"]),
|
||||||
"SFM_STATE_FILE": self.var_sfm_state_file.get().strip(),
|
"SFM_STATE_FILE": self.var_sfm_state_file.get().strip(),
|
||||||
"SFM_MAX_FORWARDS_PER_PASS": str(int_values["SFM Max Events Per Pass"]),
|
"SFM_MAX_FORWARDS_PER_PASS": str(int_values["SFM Max Events Per Pass"]),
|
||||||
|
|
||||||
|
"MIRROR_API_URL": mirror_api_url,
|
||||||
|
"MIRROR_SFM_URL": mirror_sfm_url,
|
||||||
|
"MIRROR_SFM_STATE_FILE": self.var_mirror_sfm_state_file.get().strip(),
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -285,6 +285,95 @@ class TestFindPendingEvents(unittest.TestCase):
|
|||||||
self.assertEqual(os.path.basename(bin_path), "M529LK44.AB0")
|
self.assertEqual(os.path.basename(bin_path), "M529LK44.AB0")
|
||||||
self.assertIsNone(txt_path)
|
self.assertIsNone(txt_path)
|
||||||
|
|
||||||
|
def test_re_pair_after_late_arriving_txt(self):
|
||||||
|
"""If we forwarded the binary alone (TXT was late) and the TXT
|
||||||
|
later appears, the binary becomes eligible for re-forward."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
tmp_p = Path(tmp)
|
||||||
|
bin_p = self._make(tmp_p, "M529LK44.AB0",
|
||||||
|
age_seconds=200, content=b"binary")
|
||||||
|
# Mark as already-forwarded WITHOUT a paired report (the
|
||||||
|
# state we'd be in after a TXT-too-late forward).
|
||||||
|
state = ef.ForwardState(str(tmp_p / "fwd.json"))
|
||||||
|
digest = ef.sha256_of_file(str(bin_p))
|
||||||
|
state.mark_forwarded(digest, "M529LK44.AB0", len(b"binary"),
|
||||||
|
had_report=False)
|
||||||
|
|
||||||
|
# First scan: TXT not present yet → still skipped.
|
||||||
|
pending = ef.find_pending_events(
|
||||||
|
str(tmp_p), state, max_age_days=30,
|
||||||
|
quiescence_seconds=5, missing_report_grace_seconds=60,
|
||||||
|
)
|
||||||
|
self.assertEqual(pending, [],
|
||||||
|
"no TXT present → no re-pair attempt")
|
||||||
|
|
||||||
|
# Now BW finally writes the TXT.
|
||||||
|
self._make(tmp_p, "M529LK44.AB0.TXT",
|
||||||
|
age_seconds=100, content=b"report")
|
||||||
|
pending = ef.find_pending_events(
|
||||||
|
str(tmp_p), state, max_age_days=30,
|
||||||
|
quiescence_seconds=5, missing_report_grace_seconds=60,
|
||||||
|
)
|
||||||
|
self.assertEqual(len(pending), 1,
|
||||||
|
"TXT now present → re-pair attempt expected")
|
||||||
|
self.assertEqual(os.path.basename(pending[0][0]), "M529LK44.AB0")
|
||||||
|
self.assertEqual(os.path.basename(pending[0][1]), "M529LK44.AB0.TXT")
|
||||||
|
|
||||||
|
def test_re_pair_not_attempted_when_already_had_report(self):
|
||||||
|
"""Successful WITH-report forwards stay permanently skipped.
|
||||||
|
Adding more files later does NOT trigger a re-forward."""
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
tmp_p = Path(tmp)
|
||||||
|
bin_p = self._make(tmp_p, "M529LK44.AB0", age_seconds=200, content=b"x")
|
||||||
|
self._make(tmp_p, "M529LK44.AB0.TXT", age_seconds=100, content=b"r")
|
||||||
|
state = ef.ForwardState(str(tmp_p / "fwd.json"))
|
||||||
|
state.mark_forwarded(ef.sha256_of_file(str(bin_p)),
|
||||||
|
"M529LK44.AB0", 1, had_report=True)
|
||||||
|
pending = ef.find_pending_events(
|
||||||
|
str(tmp_p), state, max_age_days=30,
|
||||||
|
quiescence_seconds=5, missing_report_grace_seconds=60,
|
||||||
|
)
|
||||||
|
self.assertEqual(pending, [],
|
||||||
|
"had_report=True forwards stay skipped")
|
||||||
|
|
||||||
|
def test_legacy_state_entries_default_to_had_report_true(self):
|
||||||
|
"""Backward compat: state-file entries from before the
|
||||||
|
had_report field existed are treated as fully forwarded so
|
||||||
|
an upgrade doesn't re-forward every entry."""
|
||||||
|
import json
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
tmp_p = Path(tmp)
|
||||||
|
path = str(tmp_p / "fwd.json")
|
||||||
|
with open(path, "w") as f:
|
||||||
|
json.dump({
|
||||||
|
"version": 1,
|
||||||
|
"forwarded": {
|
||||||
|
"abc123": {
|
||||||
|
"filename": "M529LK01.AB0",
|
||||||
|
"size": 123,
|
||||||
|
"forwarded_at": "2025-01-01T00:00:00Z",
|
||||||
|
# No had_report field — legacy entry
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, f)
|
||||||
|
state = ef.ForwardState(path)
|
||||||
|
self.assertIs(state.status("abc123"), True,
|
||||||
|
"legacy entry must default to 'fully forwarded'")
|
||||||
|
|
||||||
|
def test_state_status_returns_none_for_unknown_sha(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
state = ef.ForwardState(str(Path(tmp) / "fwd.json"))
|
||||||
|
self.assertIs(state.status("never-seen"), None)
|
||||||
|
|
||||||
|
def test_state_mark_with_had_report_false(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
state = ef.ForwardState(str(Path(tmp) / "fwd.json"))
|
||||||
|
state.mark_forwarded("xyz", "f.AB0", 100, had_report=False)
|
||||||
|
self.assertIs(state.status("xyz"), False)
|
||||||
|
# Subsequent re-mark with had_report=True promotes to done.
|
||||||
|
state.mark_forwarded("xyz", "f.AB0", 100, had_report=True)
|
||||||
|
self.assertIs(state.status("xyz"), True)
|
||||||
|
|
||||||
def test_defers_when_txt_missing_and_within_grace(self):
|
def test_defers_when_txt_missing_and_within_grace(self):
|
||||||
with tempfile.TemporaryDirectory() as tmp:
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
tmp_p = Path(tmp)
|
tmp_p = Path(tmp)
|
||||||
@@ -625,5 +714,127 @@ class TestForwardEventPair(unittest.TestCase):
|
|||||||
self.assertIn("serial=BE11529", req["path"])
|
self.assertIn("serial=BE11529", req["path"])
|
||||||
|
|
||||||
|
|
||||||
|
# ── Mirror (dual-send) ───────────────────────────────────────────────────────
|
||||||
|
|
||||||
|
|
||||||
|
def _mk_mirror_event(watch_dir, name="M529LK44.AB0", age_seconds=200, content=b"binary"):
|
||||||
|
"""Create a Blastware-shaped event binary with a controlled mtime."""
|
||||||
|
p = Path(watch_dir) / name
|
||||||
|
p.write_bytes(content)
|
||||||
|
target = time.time() - age_seconds
|
||||||
|
os.utime(str(p), (target, target))
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
class TestMirrorReachable(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_empty_url_is_unreachable(self):
|
||||||
|
self.assertFalse(ef.mirror_reachable("", timeout=1.0))
|
||||||
|
|
||||||
|
def test_dead_port_is_unreachable_and_fast(self):
|
||||||
|
t0 = time.time()
|
||||||
|
self.assertFalse(ef.mirror_reachable("http://127.0.0.1:1", timeout=2.0))
|
||||||
|
self.assertLess(time.time() - t0, 2.5)
|
||||||
|
|
||||||
|
def test_any_http_response_counts_as_reachable(self):
|
||||||
|
server, base = _start_fake_server()
|
||||||
|
try:
|
||||||
|
self.assertTrue(ef.mirror_reachable(base, timeout=2.0))
|
||||||
|
finally:
|
||||||
|
server.shutdown()
|
||||||
|
server.server_close()
|
||||||
|
|
||||||
|
|
||||||
|
class TestMirrorForwardPass(unittest.TestCase):
|
||||||
|
"""The dual-send entry point: best-effort, isolated, own state."""
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
_FakeImportHandler.received = []
|
||||||
|
self.server, self.base_url = _start_fake_server()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.server.shutdown()
|
||||||
|
self.server.server_close()
|
||||||
|
|
||||||
|
def test_empty_mirror_url_is_noop(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
_mk_mirror_event(tmp)
|
||||||
|
mstate = ef.ForwardState(os.path.join(tmp, "mirror.json"))
|
||||||
|
self.assertIsNone(ef.mirror_forward_pass(tmp, "", mstate, max_age_days=30))
|
||||||
|
self.assertEqual(len(_FakeImportHandler.received), 0)
|
||||||
|
|
||||||
|
def test_skips_when_unreachable_without_posting(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
_mk_mirror_event(tmp)
|
||||||
|
mstate = ef.ForwardState(os.path.join(tmp, "mirror.json"))
|
||||||
|
result = ef.mirror_forward_pass(
|
||||||
|
tmp, self.base_url, mstate,
|
||||||
|
reachable_fn=lambda url, timeout=3.0: False,
|
||||||
|
max_age_days=30, quiescence_seconds=5,
|
||||||
|
missing_report_grace_seconds=60, timeout=5.0,
|
||||||
|
)
|
||||||
|
self.assertIsNone(result)
|
||||||
|
self.assertEqual(mstate.count(), 0)
|
||||||
|
self.assertEqual(len(_FakeImportHandler.received), 0)
|
||||||
|
|
||||||
|
def test_forwards_when_reachable_using_its_own_state(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
_mk_mirror_event(tmp)
|
||||||
|
mstate = ef.ForwardState(os.path.join(tmp, "mirror.json"))
|
||||||
|
counts = ef.mirror_forward_pass(
|
||||||
|
tmp, self.base_url, mstate,
|
||||||
|
reachable_fn=lambda url, timeout=3.0: True,
|
||||||
|
max_age_days=30, quiescence_seconds=5,
|
||||||
|
missing_report_grace_seconds=60, timeout=5.0,
|
||||||
|
)
|
||||||
|
self.assertIsNotNone(counts)
|
||||||
|
self.assertEqual(counts["forwarded"], 1)
|
||||||
|
self.assertEqual(mstate.count(), 1)
|
||||||
|
self.assertEqual(len(_FakeImportHandler.received), 1)
|
||||||
|
|
||||||
|
def test_never_raises_when_forward_blows_up(self):
|
||||||
|
def _boom(*a, **k):
|
||||||
|
raise RuntimeError("boom")
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
_mk_mirror_event(tmp)
|
||||||
|
mstate = ef.ForwardState(os.path.join(tmp, "mirror.json"))
|
||||||
|
orig = ef.forward_pending
|
||||||
|
ef.forward_pending = _boom
|
||||||
|
try:
|
||||||
|
result = ef.mirror_forward_pass(
|
||||||
|
tmp, self.base_url, mstate,
|
||||||
|
reachable_fn=lambda url, timeout=3.0: True,
|
||||||
|
max_age_days=30,
|
||||||
|
)
|
||||||
|
finally:
|
||||||
|
ef.forward_pending = orig
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
def test_down_mirror_leaves_primary_state_untouched(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
_mk_mirror_event(tmp)
|
||||||
|
|
||||||
|
primary_state = ef.ForwardState(os.path.join(tmp, "primary.json"))
|
||||||
|
pcounts = ef.forward_pending(
|
||||||
|
tmp, self.base_url, primary_state,
|
||||||
|
max_age_days=30, quiescence_seconds=5,
|
||||||
|
missing_report_grace_seconds=60, timeout=5.0,
|
||||||
|
)
|
||||||
|
self.assertEqual(pcounts["forwarded"], 1)
|
||||||
|
primary_snapshot = primary_state.count()
|
||||||
|
|
||||||
|
mstate = ef.ForwardState(os.path.join(tmp, "mirror.json"))
|
||||||
|
t0 = time.time()
|
||||||
|
result = ef.mirror_forward_pass(
|
||||||
|
tmp, "http://127.0.0.1:1", mstate, # nothing listens
|
||||||
|
max_age_days=30, quiescence_seconds=5,
|
||||||
|
missing_report_grace_seconds=60, timeout=5.0,
|
||||||
|
)
|
||||||
|
self.assertIsNone(result)
|
||||||
|
self.assertLess(time.time() - t0, 3.5)
|
||||||
|
self.assertEqual(mstate.count(), 0)
|
||||||
|
self.assertEqual(primary_state.count(), primary_snapshot)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
"""
|
||||||
|
Tests for scan_latest's handling of unsniffable .MLG files.
|
||||||
|
|
||||||
|
Regression guard: a .MLG file the watcher can't extract a unit ID from must
|
||||||
|
be cached (as "no id") so it isn't re-sniffed and re-logged on every scan.
|
||||||
|
Previously the unsniffable result was never cached, so the
|
||||||
|
`[unsniffable-recent]` warning was re-emitted every scan cycle — ~200 files
|
||||||
|
x 288 scans/day flooded the log with hundreds of thousands of duplicate lines.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
import series3_watcher
|
||||||
|
|
||||||
|
|
||||||
|
class UnsniffableCaching(unittest.TestCase):
|
||||||
|
def test_unsniffable_recent_file_logged_once_across_scans(self):
|
||||||
|
with tempfile.TemporaryDirectory() as d:
|
||||||
|
# A .MLG file with no BE####/BA#### pattern → sniff returns None.
|
||||||
|
path = os.path.join(d, "junk001.MLG")
|
||||||
|
with open(path, "wb") as f:
|
||||||
|
f.write(b"\x00" * 512)
|
||||||
|
|
||||||
|
calls = []
|
||||||
|
cache = {}
|
||||||
|
recent_cutoff = time.time() - 86400.0 # file (mtime≈now) counts as recent
|
||||||
|
|
||||||
|
for _ in range(3):
|
||||||
|
series3_watcher.scan_latest(
|
||||||
|
watch=d,
|
||||||
|
header_bytes=2048,
|
||||||
|
cache=cache,
|
||||||
|
recent_cutoff=recent_cutoff,
|
||||||
|
max_age_days=365,
|
||||||
|
logger=calls.append,
|
||||||
|
)
|
||||||
|
|
||||||
|
unsniff = [c for c in calls if "[unsniffable-recent]" in c]
|
||||||
|
self.assertEqual(
|
||||||
|
len(unsniff), 1,
|
||||||
|
"unsniffable file should log once across scans, got {}: {}".format(
|
||||||
|
len(unsniff), unsniff),
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_sniffable_file_still_returned(self):
|
||||||
|
# Sanity: a file WITH a unit id is still detected (happy path intact).
|
||||||
|
with tempfile.TemporaryDirectory() as d:
|
||||||
|
path = os.path.join(d, "evt001.MLG")
|
||||||
|
with open(path, "wb") as f:
|
||||||
|
f.write(b"hdr\x00BE12599\x00hdr") # contains a sniffable unit id
|
||||||
|
cache = {}
|
||||||
|
result = series3_watcher.scan_latest(
|
||||||
|
watch=d, header_bytes=2048, cache=cache,
|
||||||
|
recent_cutoff=time.time() - 86400.0, max_age_days=365, logger=None,
|
||||||
|
)
|
||||||
|
self.assertIn("BE12599", result)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
"""
|
||||||
|
Tests for series3_tray's thread lifecycle.
|
||||||
|
|
||||||
|
Regression guard: saving the Settings dialog restarts the watcher thread via
|
||||||
|
``_restart_watcher``, which sets and replaces the shared ``stop_event``. The
|
||||||
|
auto-updater must key off its own lifetime event (``_app_stop``) so a watcher
|
||||||
|
restart never kills it; it should only stop when the app actually exits.
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
import unittest
|
||||||
|
from unittest import mock
|
||||||
|
|
||||||
|
# Series3's tray pulls in Windows-only GUI deps at import time; stub them.
|
||||||
|
for _name in ("pystray", "PIL", "PIL.Image", "PIL.ImageDraw"):
|
||||||
|
sys.modules.setdefault(_name, mock.MagicMock())
|
||||||
|
|
||||||
|
import series3_tray # noqa: E402
|
||||||
|
|
||||||
|
|
||||||
|
class TrayThreadLifecycle(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
# No-op watcher loop so restarting the watcher doesn't spin up the real
|
||||||
|
# Blastware scanner / network heartbeat.
|
||||||
|
patcher = mock.patch.object(
|
||||||
|
series3_tray.watcher, "run_watcher", lambda state, stop_event: None
|
||||||
|
)
|
||||||
|
patcher.start()
|
||||||
|
self.addCleanup(patcher.stop)
|
||||||
|
|
||||||
|
def test_app_stop_is_separate_lifetime_event(self):
|
||||||
|
app = series3_tray.WatcherTray()
|
||||||
|
self.assertIsInstance(app._app_stop, threading.Event)
|
||||||
|
self.assertIsNot(app._app_stop, app.stop_event)
|
||||||
|
self.assertFalse(app._app_stop.is_set())
|
||||||
|
|
||||||
|
def test_exit_signals_app_stop(self):
|
||||||
|
app = series3_tray.WatcherTray()
|
||||||
|
icon = mock.MagicMock()
|
||||||
|
app._exit(icon, None)
|
||||||
|
self.assertTrue(app._app_stop.is_set())
|
||||||
|
icon.stop.assert_called_once()
|
||||||
|
|
||||||
|
def test_restart_watcher_leaves_watcher_stop_unset(self):
|
||||||
|
app = series3_tray.WatcherTray()
|
||||||
|
app._restart_watcher()
|
||||||
|
self.assertFalse(app.stop_event.is_set())
|
||||||
|
|
||||||
|
def test_updater_survives_watcher_restart_then_stops_on_exit(self):
|
||||||
|
app = series3_tray.WatcherTray()
|
||||||
|
app._icon = None
|
||||||
|
with mock.patch.object(series3_tray, "check_for_update", return_value=(None, None)), \
|
||||||
|
mock.patch.object(app, "_tray_status", return_value="ok"):
|
||||||
|
t = threading.Thread(
|
||||||
|
target=app._icon_updater, daemon=True, name="test-updater"
|
||||||
|
)
|
||||||
|
t.start()
|
||||||
|
time.sleep(0.1)
|
||||||
|
self.assertTrue(t.is_alive(), "updater failed to start")
|
||||||
|
|
||||||
|
# A settings save restarts the watcher — it must NOT kill the updater.
|
||||||
|
app._restart_watcher()
|
||||||
|
time.sleep(0.2)
|
||||||
|
self.assertTrue(
|
||||||
|
t.is_alive(), "updater thread died on watcher restart (the bug)"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Real app exit DOES stop the updater.
|
||||||
|
app._app_stop.set()
|
||||||
|
t.join(timeout=2)
|
||||||
|
self.assertFalse(
|
||||||
|
t.is_alive(), "updater thread did not stop on app exit"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user