[Unit]
# The recovery half of parvagues-sc.service.
#
# parvagues-sc's MainPID is sclang; scsynth is its child. systemd therefore reports
# the unit `active (running)` while the audio server is dead — which is exactly what
# happened on 2026-08-01 (suspend killed scsynth at 10:48, sclang logged it, nothing
# acted, 63 Bridge warnings went to a journal nobody reads).
#
# This unit is deliberately SEPARATE from parvagues-sc rather than folded into its
# ExecStart: at J-7 from a gig, a bug in the boot path is fatal and a bug out here is
# merely noisy. Post-gig this should become an sclang-side ServerQuit handler (#115).
Description=ParVagues SuperDirt watchdog (restarts SC when scsynth dies)
After=parvagues-sc.service
#
# NO PartOf=/BindsTo= HERE, AND THAT IS LOAD-BEARING. The first version had
# `PartOf=parvagues-sc.service`, reasoning that stopping SuperDirt should stop the
# watchdog. But PartOf propagates RESTARTS too — so the instant the watchdog issued
# `systemctl restart parvagues-sc`, systemd killed the watchdog, Restart=always
# revived it, and its in-memory rate-limit counter reset to zero. Measured
# 2026-08-01, three lines 90ms apart:
#
#   11:35:46.265  sc-watchdog[1102323]: scsynth GONE — restarting
#   11:35:46.321  systemd: parvagues-sc.service: Consumed 1min CPU time
#   11:35:46.354  sc-watchdog[1123545]: watching scsynth        <- NEW PID
#
# MAX_RESTARTS was therefore unreachable and the loop was unbounded. A supervisor
# must OUTLIVE the thing it supervises. Respecting a deliberate stop is handled in
# the script instead, by checking `systemctl is-active` before ever acting.

[Service]
Type=simple
ExecStart=/home/pln/Work/Sound/Tidal/tools/sc-watchdog.sh
# The watchdog itself may restart freely — it makes no sound and holds no ports.
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=sc-watchdog

[Install]
WantedBy=default.target
