Skip to content

CLI for SidePulse

Python

Restart the background service so upgrades take effect

Peter Kuhar53f2cf87036d
diff --git a/README.md b/README.md
index ac58357..ebf3012 100644
--- a/README.md
+++ b/README.md
@@ -37,9 +37,11 @@ uses CLI-only mode for mounted SidePulse devices and iPhone linking/push. Run
the same command again to upgrade to the newest version.
Setup also installs a headless SidePulse service. It runs through a LaunchAgent
-on macOS and a user systemd service on Linux. If a Linux session has no systemd
-user manager, setup still installs the unit and prints that it could not start;
-`sidepulse service run` remains available as a foreground fallback.
+on macOS and a user systemd service on Linux. Setup restarts the service, so an
+upgrade takes effect right away instead of leaving the previous build running.
+If a Linux session has no systemd user manager, setup still installs the unit
+and prints that it could not start; `sidepulse service run` remains available as
+a foreground fallback.
### 2. Install into your own Python environment
diff --git a/scripts/setup.sh b/scripts/setup.sh
index 653a81f..319fdd0 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -21,6 +21,12 @@ if ! "$PYTHON_BIN" -m venv "$VENV"; then
printf 'On Debian or Ubuntu, install python3-venv and run this command again.\n' >&2
exit 1
fi
+# Stop a service still running from this venv before pip replaces the files
+# underneath it. `sidepulse setup` starts the new build again below.
+if [ -x "$VENV/bin/sidepulse" ]; then
+ "$VENV/bin/sidepulse" service stop >/dev/null 2>&1 || true
+fi
+
"$VENV/bin/python" -m pip install --upgrade \
"$INSTALL_SPEC"
diff --git a/src/sidepulse/service_launch.py b/src/sidepulse/service_launch.py
index 76d3ca3..5cec58b 100644
--- a/src/sidepulse/service_launch.py
+++ b/src/sidepulse/service_launch.py
@@ -121,18 +121,26 @@ def install_systemd_user_service(*, start: bool, dry_run: bool) -> ServiceInstal
False,
reload_result.stderr.strip() or "systemd user manager unavailable",
)
- result = subprocess.run(
- [systemctl, "--user", "enable", "--now", "sidepulse.service"],
+ enable_result = subprocess.run(
+ [systemctl, "--user", "enable", "sidepulse.service"],
check=False,
capture_output=True,
text=True,
)
- return ServiceInstallResult(
- path,
- changed,
- result.returncode == 0,
- result.stderr.strip() if result.returncode else "",
+ # Restart rather than `enable --now`. An upgrade replaces the files the
+ # running service loaded at start, and `--now` leaves an already-running
+ # unit alone, so the old code would keep driving the LEDs until the next
+ # reboot. Restart starts a stopped unit and replaces a running one.
+ result = subprocess.run(
+ [systemctl, "--user", "restart", "sidepulse.service"],
+ check=False,
+ capture_output=True,
+ text=True,
)
+ if result.returncode == 0:
+ return ServiceInstallResult(path, changed, True, "")
+ detail = result.stderr.strip() or enable_result.stderr.strip()
+ return ServiceInstallResult(path, changed, False, detail)
def stop_service() -> bool:
diff --git a/tests/test_relay.py b/tests/test_relay.py
index 1cb86e0..52a384f 100644
--- a/tests/test_relay.py
+++ b/tests/test_relay.py
@@ -247,6 +247,48 @@ class ServiceLaunchTests(unittest.TestCase):
self.assertFalse(result.started)
self.assertIn("unavailable", result.detail)
+ def test_linux_restarts_the_unit_so_upgrades_replace_the_running_service(self) -> None:
+ with tempfile.TemporaryDirectory() as tmp:
+ home = Path(tmp)
+ completed = SimpleNamespace(returncode=0, stderr="")
+ with (
+ patch.object(Path, "home", return_value=home),
+ patch("sidepulse.service_launch.shutil.which", return_value="/usr/bin/systemctl"),
+ patch(
+ "sidepulse.service_launch.subprocess.run",
+ return_value=completed,
+ ) as run,
+ ):
+ result = install_systemd_user_service(start=True, dry_run=False)
+
+ self.assertTrue(result.started)
+ commands = [call.args[0] for call in run.call_args_list]
+ verbs = [command[2] for command in commands]
+ self.assertEqual(verbs, ["daemon-reload", "enable", "restart"])
+ # `enable --now` leaves an already-running unit on the old code.
+ self.assertNotIn("--now", [part for command in commands for part in command])
+
+ def test_linux_restarts_the_unit_so_upgrades_replace_the_running_service(self) -> None:
+ with tempfile.TemporaryDirectory() as tmp:
+ home = Path(tmp)
+ completed = SimpleNamespace(returncode=0, stderr="")
+ with (
+ patch.object(Path, "home", return_value=home),
+ patch("sidepulse.service_launch.shutil.which", return_value="/usr/bin/systemctl"),
+ patch(
+ "sidepulse.service_launch.subprocess.run",
+ return_value=completed,
+ ) as run,
+ ):
+ result = install_systemd_user_service(start=True, dry_run=False)
+
+ self.assertTrue(result.started)
+ commands = [call.args[0] for call in run.call_args_list]
+ verbs = [command[2] for command in commands]
+ self.assertEqual(verbs, ["daemon-reload", "enable", "restart"])
+ # `enable --now` leaves an already-running unit on the old code.
+ self.assertNotIn("--now", [part for command in commands for part in command])
+
if __name__ == "__main__":
unittest.main()