Skip to content

Commit

Permalink
Comments addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
SRIKKANTH committed Jan 3, 2025
1 parent 30a8249 commit 90f21f6
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions lisa/base_tools/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class WindowsServiceStatus(int, Enum):
START_PENDING = 2
STOP_PENDING = 3
STOPPED = 1
NOT_FOUND = 0


class WindowsService(Tool):
Expand Down Expand Up @@ -258,29 +259,30 @@ def wait_for_service_stop(self, name: str) -> None:
def _check_exists(self) -> bool:
return True

def _check_service_exists(self, name: str) -> bool:
try:
self._get_status(name)
return True
except LisaException:
def check_service_exists(self, name: str) -> bool:
if (
self._get_status(name, fail_on_error=False)
== WindowsServiceStatus.NOT_FOUND
):
return False
return True

def _check_service_running(self, name: str) -> bool:
return self._get_status(name) == WindowsServiceStatus.RUNNING

def _get_status(self, name: str = "") -> WindowsServiceStatus:
service_status = self.node.tools[PowerShell].run_cmdlet(
f"Get-Service {name}",
force_run=True,
output_json=True,
fail_on_error=False,
)
if not service_status:
raise LisaException(f"service '{name}' does not exist")
return WindowsServiceStatus(int(service_status["Status"]))

def _is_service_inactive(self, name: str) -> bool:
return self._get_status(name) == WindowsServiceStatus.STOPPED
def _get_status(
self, name: str = "", fail_on_error: bool = True
) -> WindowsServiceStatus:
try:
service_status = self.node.tools[PowerShell].run_cmdlet(
f"Get-Service {name}",
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

def _wait_for_service(self, name: str, status: WindowsServiceStatus) -> None:
timeout = 60
Expand All @@ -294,5 +296,6 @@ def _wait_for_service(self, name: str, status: WindowsServiceStatus) -> None:

if timeout < timer.elapsed():
raise LisaException(
f"service '{name}' still in '{current_service_status}' state after '{timeout}' seconds" # noqa: E501
f"service '{name}' still in '{current_service_status}' state"
f"after '{timeout}' seconds"
)

0 comments on commit 90f21f6

Please sign in to comment.