-
-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy pathevents.py
More file actions
160 lines (103 loc) · 3.24 KB
/
events.py
File metadata and controls
160 lines (103 loc) · 3.24 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from __future__ import annotations
import asyncio
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Dict, Literal, TypedDict, Union
from api import ErrorValue, InputId, NodeId, OutputId
# General events
class BackendStatusData(TypedDict):
message: str
progress: float
statusProgress: float | None
class BackendStatusEvent(TypedDict):
event: Literal["backend-status", "package-install-status"]
data: BackendStatusData
class BackendStateEvent(TypedDict):
event: Literal["backend-started"]
data: None
BackendEvent = Union[BackendStatusEvent, BackendStateEvent]
# Execution events
InputsDict = Dict[InputId, ErrorValue]
class ExecutionErrorSource(TypedDict):
nodeId: NodeId
schemaId: str
inputs: InputsDict
class ExecutionErrorData(TypedDict):
message: str
exception: str
exceptionTrace: str
source: ExecutionErrorSource | None
class ExecutionErrorEvent(TypedDict):
event: Literal["execution-error"]
data: ExecutionErrorData
class ChainStartData(TypedDict):
nodes: list[str]
class ChainStartEvent(TypedDict):
event: Literal["chain-start"]
data: ChainStartData
class NodeStartData(TypedDict):
nodeId: NodeId
class NodeStartEvent(TypedDict):
event: Literal["node-start"]
data: NodeStartData
class NodeProgressData(TypedDict):
nodeId: NodeId
progress: float
"""A number between 0 and 1"""
index: int
total: int
eta: float
class NodeProgressUpdateEvent(TypedDict):
event: Literal["node-progress"]
data: NodeProgressData
class NodeBroadcastData(TypedDict):
nodeId: NodeId
data: dict[OutputId, object]
types: dict[OutputId, object]
class NodeBroadcastEvent(TypedDict):
event: Literal["node-broadcast"]
data: NodeBroadcastData
class NodeFinishData(TypedDict):
nodeId: NodeId
executionTime: float
class NodeFinishEvent(TypedDict):
event: Literal["node-finish"]
data: NodeFinishData
ExecutionEvent = Union[
ExecutionErrorEvent,
ChainStartEvent,
NodeStartEvent,
NodeProgressUpdateEvent,
NodeBroadcastEvent,
NodeFinishEvent,
]
Event = Union[ExecutionEvent, BackendEvent]
class EventConsumer(ABC):
@abstractmethod
def put(self, event: Event) -> None: ...
@staticmethod
def filter(queue: EventConsumer, allowed: set[str]) -> EventConsumer:
return _FilteredEventConsumer(queue, allowed)
@dataclass
class _FilteredEventConsumer(EventConsumer):
queue: EventConsumer
allowed: set[str]
def put(self, event: Event) -> None:
if event["event"] in self.allowed:
self.queue.put(event)
class EventQueue(EventConsumer):
def __init__(self):
self.queue = asyncio.Queue()
async def get(self) -> Event:
return await self.queue.get()
def put(self, event: Event) -> None:
self.queue.put_nowait(event)
async def wait_until_empty(self, timeout: float) -> None:
while timeout > 0:
if self.queue.empty():
return
await asyncio.sleep(0.01)
timeout -= 0.01
async def put_and_wait(self, event: Event, timeout: float = float("inf")) -> None:
await self.queue.put(event)
await self.wait_until_empty(timeout)