|
4 | 4 | from psutil import STATUS_STOPPED, NoSuchProcess, ZombieProcess, AccessDenied |
5 | 5 | from psutil._pswindows import WindowsService |
6 | 6 |
|
7 | | -from constants.log import LOG |
8 | 7 | from model.service import Service |
9 | 8 | from util.decorators import suppress_exception |
10 | 9 |
|
|
23 | 22 |
|
24 | 23 | class ServicesInfoService(ABC): |
25 | 24 | """ |
26 | | - The ServicesInfoService class provides methods for retrieving information about running services. |
27 | | - It is an abstract base class (ABC) to be subclassed by specific implementation classes. |
| 25 | + The ServicesInfoService class provides methods for retrieving information about Windows services. |
28 | 26 | """ |
29 | 27 |
|
30 | 28 | @staticmethod |
31 | | - def get_services() -> dict[int, Service]: |
32 | | - """ |
33 | | - Get a dictionary of running services and their information. |
34 | | -
|
35 | | - Returns: |
36 | | - dict[int, Service]: A dictionary where keys are process IDs (pids) and values are Service objects |
37 | | - representing the running services. |
38 | | - """ |
| 29 | + def get_running_services() -> dict[int, Service]: |
39 | 30 | result: dict[int, Service] = {} |
40 | 31 |
|
41 | 32 | for service in psutil.win_service_iter(): |
42 | 33 | try: |
43 | | - info = service.as_dict() |
| 34 | + # noinspection PyUnresolvedReferences |
| 35 | + info = service._query_status() |
| 36 | + status = info['status'] |
| 37 | + pid = info['pid'] |
44 | 38 |
|
45 | | - if info['status'] == STATUS_STOPPED: |
| 39 | + if pid == STATUS_STOPPED: |
46 | 40 | continue |
47 | 41 |
|
48 | | - result[info['pid']] = Service( |
49 | | - info['pid'], |
50 | | - info['name'], |
51 | | - info['display_name'], |
52 | | - info['description'], |
53 | | - info['status'], |
54 | | - info['binpath'] |
| 42 | + result[pid] = Service( |
| 43 | + pid, |
| 44 | + service.name(), |
| 45 | + status |
55 | 46 | ) |
56 | 47 | except NoSuchProcess: |
57 | | - LOG.warning(f"No such service: {service.name}") |
| 48 | + pass |
| 49 | + |
| 50 | + return result |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def get_services() -> list[Service]: |
| 54 | + result: list[Service] = [] |
| 55 | + |
| 56 | + for service in psutil.win_service_iter(): |
| 57 | + try: |
| 58 | + # noinspection PyUnresolvedReferences |
| 59 | + info = service._query_status() |
| 60 | + |
| 61 | + result.append(Service( |
| 62 | + info['pid'], |
| 63 | + service.name(), |
| 64 | + info['status'] |
| 65 | + )) |
| 66 | + except NoSuchProcess: |
| 67 | + pass |
58 | 68 |
|
59 | 69 | return result |
0 commit comments