Skip to content

Commit 2b05383

Browse files
committed
Simplified the logic for checking if a Dispatch is new or updated
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 184507a commit 2b05383

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/frequenz/dispatch/_bg_service.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,24 @@ async def _run(self) -> None:
264264
heappop(self._scheduled_events).dispatch, next_event_timer
265265
)
266266
elif selected_from(selected, stream):
267-
268-
def is_more_relevant(
269-
dispatch: Dispatch,
270-
) -> bool:
271-
existing_dispatch = self._dispatches.get(dispatch.id)
272-
273-
return (
274-
not existing_dispatch
275-
or dispatch.update_time > existing_dispatch.update_time
276-
)
277-
278267
match selected.message:
279268
case ApiDispatchEvent():
280269
_logger.debug(
281270
"Received dispatch event: %s", selected.message
282271
)
283272
dispatch = Dispatch(selected.message.dispatch)
273+
_existing_dispatch = self._dispatches.get(dispatch.id)
274+
is_new_or_newer = (
275+
_existing_dispatch is None
276+
or dispatch.update_time > _existing_dispatch.update_time
277+
)
278+
284279
match selected.message.event:
285280
case Event.CREATED:
286281
# Check if the dispatch already exists and
287282
# was updated. The CREATE event is late in
288283
# this case
289-
if is_more_relevant(dispatch):
284+
if is_new_or_newer:
290285
self._dispatches[dispatch.id] = dispatch
291286
await self._update_dispatch_schedule_and_notify(
292287
dispatch, None, next_event_timer
@@ -297,7 +292,7 @@ def is_more_relevant(
297292
case Event.UPDATED:
298293
# We might receive update before we fetched
299294
# the entry, so don't rely on it existing
300-
if is_more_relevant(dispatch):
295+
if is_new_or_newer:
301296
await self._update_dispatch_schedule_and_notify(
302297
dispatch,
303298
self._dispatches.get(dispatch.id),
@@ -311,7 +306,7 @@ def is_more_relevant(
311306
# The dispatch might already be deleted,
312307
# depending on the exact timing of fetch()
313308
# so we don't rely on it existing.
314-
if is_more_relevant(dispatch):
309+
if is_new_or_newer:
315310
self._dispatches.pop(dispatch.id, None)
316311

317312
self._deleted_dispatches[dispatch.id] = (

0 commit comments

Comments
 (0)