Skip to content

Commit

Permalink
Update service.py
Browse files Browse the repository at this point in the history
  • Loading branch information
SRIKKANTH committed Jan 7, 2025
1 parent 90f21f6 commit d48cd21
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lisa/base_tools/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,19 +235,29 @@ def enable_service(self, name: str) -> None:
pass

def restart_service(self, name: str, ignore_exit_code: int = 0) -> None:
self.node.tools[PowerShell].run_cmdlet(
f"Restart-service {name}",
force_run=True,
)
try:
self.node.tools[PowerShell].run_cmdlet(
f"Restart-service {name}",
force_run=True,
)
except LisaException as identifier:
if "Cannot find any service with service name" in str(identifier):
self._log.debug(f"service '{name}' does not exist")
return
raise identifier
self.wait_for_service_start(name)

def stop_service(self, name: str) -> None:
self.node.tools[PowerShell].run_cmdlet(
f"Stop-Service {name} -Force",
force_run=True,
output_json=True,
fail_on_error=False,
)
try:
self.node.tools[PowerShell].run_cmdlet(
f"Stop-Service {name} -Force",
force_run=True,
)
except LisaException as identifier:
if "Cannot find any service with service name" in str(identifier):
self._log.debug(f"service '{name}' does not exist")
return
raise identifier
self.wait_for_service_stop(name)

def wait_for_service_start(self, name: str) -> None:
Expand Down Expand Up @@ -276,16 +286,17 @@ def _get_status(
force_run=True,
output_json=True,
)
return WindowsServiceStatus(int(service_status["Status"]))
except LisaException as identifier:
if "Cannot find any service with service name" in str(identifier):
if fail_on_error:
raise LisaException(f"service '{name}' does not exist")
return WindowsServiceStatus.NOT_FOUND
raise identifier
return WindowsServiceStatus(int(service_status["Status"]))

def _wait_for_service(self, name: str, status: WindowsServiceStatus) -> None:
timeout = 60
def _wait_for_service(
self, name: str, status: WindowsServiceStatus, timeout: int = 30
) -> None:
timer = create_timer()
self._log.debug(f"waiting for service '{name}' to be in '{status}' state")
while timeout > timer.elapsed(False):
Expand Down

0 comments on commit d48cd21

Please sign in to comment.