Service: pick up app updates by self-restarting onto the new binary

Local checkpoint - NOT for public release. Ed: an update should tear down the service,
update it, and restart it. It didn't - the updater has no service awareness.

Current (unchanged, verified correct): the auto-updater renames the install files aside
and copies the new ones in, which a running service TOLERATES (no failed swap). The old
version keeps streaming; the new files sit in place.

New: the service now adopts the update itself. Because it runs as SYSTEM (which has the
rights the non-elevated updater lacks), a 45s timer notices when a strictly-newer
RemSound.exe has landed next to it and restarts itself onto the new binary (detached
PowerShell Stop-Service+Start-Service). Loop-safe: only fires on a strictly-newer on-disk
version; any uncertainty (file mid-swap, unparseable version) means don't restart, and
after the restart on-disk == running so it never re-triggers.

Test "Service registration args" now also covers the version-comparison logic (newer =>
restart; same/older/missing => no restart). Gate 27/27.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Ednunp
2026-07-13 09:38:52 +01:00
co-authored by Claude Opus 4.8
parent 941e0df99f
commit 56dbd0b80f
3 changed files with 88 additions and 1 deletions
+17
View File
@@ -15,6 +15,8 @@ public sealed class RemSoundService : ServiceBase
private Thread? worker;
private ServiceSendHost? host;
private RemSoundLog? log;
private System.Threading.Timer? updateWatch;
private volatile bool restartScheduled;
public RemSoundService()
{
@@ -47,11 +49,26 @@ public sealed class RemSoundService : ServiceBase
})
{ IsBackground = true, Name = "remsound-service" };
worker.Start();
// Self-update: the auto-updater swaps the files in place but can't restart us (no admin). We run
// as SYSTEM, so when a newer RemSound.exe lands next to us we restart onto it ourselves. Checked
// on a slow timer (an update is rare); loop-safe (only fires on a strictly-newer on-disk version).
updateWatch = new System.Threading.Timer(_ => CheckForUpdate(), null, TimeSpan.FromSeconds(45), TimeSpan.FromSeconds(45));
}
private void CheckForUpdate()
{
if (restartScheduled) return;
if (!ServiceUpdate.UpdateLanded()) return;
restartScheduled = true;
log?.Event("service: a newer RemSound version was installed — restarting to update");
ServiceUpdate.RestartSelf();
}
protected override void OnStop()
{
log?.Event("service: OnStop");
try { updateWatch?.Dispose(); } catch { }
try { cts.Cancel(); } catch { }
try { worker?.Join(5000); } catch { }
try { host?.Dispose(); } catch { }