-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
service.py
52 lines (37 loc) · 1.46 KB
/
service.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
44
45
46
47
48
49
50
51
52
from __future__ import annotations
from typing import TYPE_CHECKING, Set
from launart import Launart, Service, any_completed
from .connection.base import ElizabethNetworking
from .connection.ws_client import ElizabethWsClientNetworking
if TYPE_CHECKING:
from .protocol import ElizabethProtocol
class ElizabethService(Service):
id = "elizabeth.service"
protocol: ElizabethProtocol
connections: list[ElizabethWsClientNetworking]
account_map: dict[int, ElizabethNetworking]
def __init__(self, protocol: ElizabethProtocol):
self.protocol = protocol
self.connections = []
self.account_map = {}
super().__init__()
def has_connection(self, account_id: str):
return int(account_id) in self.account_map
def get_connection(self, account_id: str) -> ElizabethNetworking:
return self.account_map[int(account_id)]
async def launch(self, manager: Launart):
async with self.stage("preparing"):
for i in self.connections:
manager.add_component(i)
async with self.stage("blocking"):
await any_completed(
manager.status.wait_for_sigexit(), *(i.status.wait_for("blocking-completed") for i in self.connections)
)
async with self.stage("cleanup"):
...
@property
def stages(self):
return {"preparing", "blocking", "cleanup"}
@property
def required(self) -> Set[str]:
return set()