Skip to content

Commit c1012fc

Browse files
committed
Rework event and instance id naming pattern
Since Invocation is a Suspension, it's becoming clear that we can have lifecycle events for all suspensions. But "suspension_id" being the id just because everything is a suspension, even when the type is more specialized, seems suboptimal. Instead, since the event_id is much less likely to be accessed, it can have the name "event_id", leaving the "id" to be the instance id, or aggregate id in event sourcing. This has the benefit of avoiding the "aggregate" verbage, which IMO is not very approachable as a concept. I think it's a little unintuitive that event.id will be the id of the object of the event rather than the event, but I think it's outweighed by the benefits of doing it this way.
1 parent 9bfb9fb commit c1012fc

5 files changed

Lines changed: 32 additions & 54 deletions

File tree

qio/consumer.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __iter__(self) -> Iterator[Invocation]:
4242

4343
def start(self, invocation: Invocation):
4444
"""Signal that the invocation is starting."""
45-
self.__stream.publish(InvocationStarted(invocation_id=invocation.id))
45+
self.__stream.publish(InvocationStarted(id=invocation.id))
4646

4747
def suspend(
4848
self,
@@ -52,10 +52,10 @@ def suspend(
5252
):
5353
"""Signal that the invocation has suspended."""
5454
if suspension:
55-
self.__stream.publish(InvocationSuspended(invocation_id=invocation.id))
55+
self.__stream.publish(InvocationSuspended(id=invocation.id))
5656
self.__stream.publish_local(
5757
LocalInvocationSuspended(
58-
invocation_id=invocation.id,
58+
id=invocation.id,
5959
suspension=suspension,
6060
invocation=invocation,
6161
generator=generator,
@@ -65,42 +65,32 @@ def suspend(
6565

6666
def resolve(self, invocation: Invocation, generator: Generator, value: Any):
6767
"""Signal that a suspension has resolved to a value."""
68-
self.__stream.publish(
69-
InvocationContinued(invocation_id=invocation.id, value=value)
70-
)
68+
self.__stream.publish(InvocationContinued(id=invocation.id, value=value))
7169
self.__stream.publish_local(
72-
LocalInvocationContinued(
73-
invocation_id=invocation.id, generator=generator, value=value
74-
)
70+
LocalInvocationContinued(id=invocation.id, generator=generator, value=value)
7571
)
7672
self.__receiver.unpause(self.__invocations[invocation])
7773

7874
def throw(self, invocation: Invocation, generator: Generator, exception: Exception):
7975
"""Signal that a suspension has thrown an exception."""
80-
self.__stream.publish(
81-
InvocationThrew(invocation_id=invocation.id, exception=exception)
82-
)
76+
self.__stream.publish(InvocationThrew(id=invocation.id, exception=exception))
8377
self.__stream.publish_local(
8478
LocalInvocationThrew(
85-
invocation_id=invocation.id, generator=generator, exception=exception
79+
id=invocation.id, generator=generator, exception=exception
8680
)
8781
)
8882
self.__receiver.unpause(self.__invocations[invocation])
8983

9084
def resume(self, invocation: Invocation):
9185
"""Signal that the invocation is resuming."""
92-
self.__stream.publish(InvocationResumed(invocation_id=invocation.id))
86+
self.__stream.publish(InvocationResumed(id=invocation.id))
9387

9488
def succeed(self, invocation: Invocation, value: Any):
9589
"""Signal that the invocation has succeeded."""
96-
self.__stream.publish(
97-
InvocationSucceeded(invocation_id=invocation.id, value=value)
98-
)
90+
self.__stream.publish(InvocationSucceeded(id=invocation.id, value=value))
9991
self.__receiver.finish(self.__invocations.pop(invocation))
10092

10193
def error(self, invocation: Invocation, exception: Exception):
10294
"""Signal that the invocation has errored."""
103-
self.__stream.publish(
104-
InvocationErrored(invocation_id=invocation.id, exception=exception)
105-
)
95+
self.__stream.publish(InvocationErrored(id=invocation.id, exception=exception))
10696
self.__receiver.finish(self.__invocations.pop(invocation))

qio/invocation.py

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def deserialize(serialized: bytes, /) -> Invocation:
7474

7575
@dataclass(eq=False, kw_only=True)
7676
class InvocationEvent:
77-
id: str = field(default_factory=random_id)
77+
event_id: str = field(default_factory=random_id)
7878
timestamp: datetime = field(default_factory=lambda: datetime.now(tz=UTC))
79-
invocation_id: str
79+
id: str
8080

8181
def __repr__(self):
82-
return f"<{type(self).__name__} {self.invocation_id}>"
82+
return f"<{type(self).__name__} {self.id}>"
8383

8484

8585
@dataclass(eq=False, kw_only=True, repr=False)
@@ -92,10 +92,7 @@ def __repr__(self):
9292
params_repr = ", ".join(
9393
(*map(repr, self.args), *(f"{k}={v!r}" for k, v in self.kwargs.items())),
9494
)
95-
return (
96-
f"<{type(self).__name__} {self.invocation_id} "
97-
f"{self.routine}({params_repr})>"
98-
)
95+
return f"<{type(self).__name__} {self.id} {self.routine}({params_repr})>"
9996

10097

10198
@dataclass(eq=False, kw_only=True, repr=False)
@@ -117,18 +114,15 @@ class LocalInvocationSuspended(BaseInvocationSuspended):
117114
invocation: Invocation
118115

119116
def __repr__(self):
120-
return (
121-
f"<{type(self).__name__} {self.invocation_id}"
122-
f" suspension={self.suspension!r}>"
123-
)
117+
return f"<{type(self).__name__} {self.id} suspension={self.suspension!r}>"
124118

125119

126120
@dataclass(eq=False, kw_only=True)
127121
class BaseInvocationContinued(InvocationEvent):
128122
value: Any
129123

130124
def __repr__(self):
131-
return f"<{type(self).__name__} {self.invocation_id} value={self.value!r}>"
125+
return f"<{type(self).__name__} {self.id} value={self.value!r}>"
132126

133127

134128
@dataclass(eq=False, kw_only=True, repr=False)
@@ -145,9 +139,7 @@ class BaseInvocationThrew(InvocationEvent):
145139
exception: Exception
146140

147141
def __repr__(self):
148-
return (
149-
f"<{type(self).__name__} {self.invocation_id} exception={self.exception!r}>"
150-
)
142+
return f"<{type(self).__name__} {self.id} exception={self.exception!r}>"
151143

152144

153145
@dataclass(eq=False, kw_only=True, repr=False)
@@ -172,14 +164,12 @@ class InvocationSucceeded(InvocationCompleted):
172164
value: Any
173165

174166
def __repr__(self):
175-
return f"<{type(self).__name__} {self.invocation_id} value={self.value!r}>"
167+
return f"<{type(self).__name__} {self.id} value={self.value!r}>"
176168

177169

178170
@dataclass(eq=False, kw_only=True)
179171
class InvocationErrored(InvocationCompleted):
180172
exception: Exception
181173

182174
def __repr__(self):
183-
return (
184-
f"<{type(self).__name__} {self.invocation_id} exception={self.exception!r}>"
185-
)
175+
return f"<{type(self).__name__} {self.id} exception={self.exception!r}>"

qio/monitor.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,51 +63,51 @@ def handle_invocation_event(
6363
match event:
6464
case InvocationSubmitted():
6565
table.add_row(
66-
event.invocation_id,
66+
event.id,
6767
event.routine,
6868
"Submitted",
6969
self.__qio.routine(event.routine).queue,
70-
key=event.invocation_id,
70+
key=event.id,
7171
)
7272
case InvocationStarted():
7373
table.update_cell(
74-
event.invocation_id,
74+
event.id,
7575
self.__column_keys[2],
7676
"Started",
7777
)
7878
case InvocationSuspended():
7979
table.update_cell(
80-
event.invocation_id,
80+
event.id,
8181
self.__column_keys[2],
8282
"Suspended",
8383
)
8484
case InvocationContinued():
8585
table.update_cell(
86-
event.invocation_id,
86+
event.id,
8787
self.__column_keys[2],
8888
"Continued",
8989
)
9090
case InvocationThrew():
9191
table.update_cell(
92-
event.invocation_id,
92+
event.id,
9393
self.__column_keys[2],
9494
"Threw",
9595
)
9696
case InvocationResumed():
9797
table.update_cell(
98-
event.invocation_id,
98+
event.id,
9999
self.__column_keys[2],
100100
"Resumed",
101101
)
102102
case InvocationSucceeded():
103103
table.update_cell(
104-
event.invocation_id,
104+
event.id,
105105
self.__column_keys[2],
106106
"Succeeded",
107107
)
108108
case InvocationErrored():
109109
table.update_cell(
110-
event.invocation_id,
110+
event.id,
111111
self.__column_keys[2],
112112
"Errored",
113113
)

qio/qio.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,11 @@ def resolver():
129129
break
130130

131131
match event:
132-
case InvocationSucceeded(invocation_id=invocation_id, value=value):
132+
case InvocationSucceeded(id=invocation_id, value=value):
133133
if invocation_id in waiting:
134134
future = waiting.pop(invocation_id)
135135
future.set_result(value)
136-
case InvocationErrored(
137-
invocation_id=invocation_id, exception=exception
138-
):
136+
case InvocationErrored(id=invocation_id, exception=exception):
139137
if invocation_id in waiting:
140138
future = waiting.pop(invocation_id)
141139
future.set_exception(exception)
@@ -161,7 +159,7 @@ def submit(self, invocation: Invocation, /):
161159
routine = self.routine(invocation.routine)
162160
self.__stream.publish(
163161
InvocationSubmitted(
164-
invocation_id=invocation.id,
162+
id=invocation.id,
165163
routine=invocation.routine,
166164
args=invocation.args,
167165
kwargs=invocation.kwargs,

qio/sample_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def test_integration():
2727
)
2828
try:
2929
while event := events.get():
30-
if event.invocation_id == invocation.id:
30+
if event.id == invocation.id:
3131
break
3232
finally:
3333
if worker.poll() is None: # Process is still running

0 commit comments

Comments
 (0)