service_runner: Fix starting a service twice
This commit is contained in:
@@ -301,14 +301,12 @@ def test_start_service_twice_transient(service_manager: ServiceManagerProtocol)
|
||||
status = service_manager.get_status("simple-service")
|
||||
assert status == "running"
|
||||
|
||||
# Try to start the same service again - this should fail
|
||||
# systemd won't allow starting a unit with the same name
|
||||
with pytest.raises(ClanError, match="Failed to start service"):
|
||||
service_manager.start_service(
|
||||
name="simple-service",
|
||||
command=["sleep", "300"],
|
||||
autostart=False,
|
||||
)
|
||||
# Try to start the same service again - this shouldn't fail
|
||||
service_manager.start_service(
|
||||
name="simple-service",
|
||||
command=["sleep", "300"],
|
||||
autostart=False,
|
||||
)
|
||||
|
||||
# Original service should still be running
|
||||
status = service_manager.get_status("simple-service")
|
||||
|
||||
@@ -184,6 +184,13 @@ class SystemdUserService:
|
||||
service_name = self._service_name(name)
|
||||
self._check_executable(command)
|
||||
|
||||
# Stop and reset any existing service (allows redefining failed/running services)
|
||||
self._systemctl("stop", service_name)
|
||||
run(
|
||||
["systemctl", "--user", "reset-failed", f"{service_name}.service"],
|
||||
RunOpts(check=False),
|
||||
)
|
||||
|
||||
if autostart:
|
||||
if group:
|
||||
self._create_target_file(group)
|
||||
@@ -223,10 +230,7 @@ class SystemdUserService:
|
||||
|
||||
cmd.extend(command)
|
||||
|
||||
result = run(cmd, RunOpts(check=False))
|
||||
if result.returncode != 0:
|
||||
msg = f"Failed to start service: {result.stderr}"
|
||||
raise ClanError(msg)
|
||||
result = run(cmd, RunOpts(error_msg="Failed to start service"))
|
||||
|
||||
return name
|
||||
|
||||
|
||||
Reference in New Issue
Block a user