-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_manager.py
43 lines (32 loc) · 1.15 KB
/
service_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import logging
from .abstract_service import AbstractService
from .task_manager import TaskManager
log = logging.getLogger(__name__)
class ServiceManager:
def __init__(self):
self._services = {}
def register(self, name, service: AbstractService):
assert name not in self._services
self._services[name] = service
def get(self, key):
if key in self._services:
return self._services[key]
else:
raise AttributeError(f"Service '{key}' not found")
async def run(self):
"""
You don't need a `close()`: When a Task running this coroutine is cancelled, this cancels `await gather`. This cancels all Tasks due to the internal logic of `gather`.
"""
log.info("Run services")
# Use an instance so that we can reset it if necessary
# TODO: Maybe this is unnecessary and we should subclass from TaskManager instead
self._mgr = TaskManager()
for k in self._services:
log.info(f"Start '{k}' service")
service = self._services[k]
self._mgr.create_task(service.run())
await self._mgr.gather()
def cancel(self):
self._mgr.cancel()
async def close(self):
await self._mgr.close()