Skip to content

Commit 60b6cfe

Browse files
committed
Only fetch relevant dispatches, excluding past ones
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 519ae09 commit 60b6cfe

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* Two new parameters were added to the `Dispatcher` constructor:
1616
* `sign_secret`: A secret key used for signing messages.
1717
* `auth_key`: An authentication key for the Dispatch API.
18+
* `Dispatcher` now only fetches ongoing dispatches, excluding completed ones, to optimize performance and relevance.
1819

1920
## Bug Fixes
2021

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dependencies = [
4040
# plugins.mkdocstrings.handlers.python.import)
4141
"frequenz-sdk >= 1.0.0-rc2100, < 1.0.0-rc2200",
4242
"frequenz-channels >= 1.6.1, < 2.0.0",
43-
"frequenz-client-dispatch >= 0.11.2, < 0.12.0",
43+
"frequenz-client-dispatch >= 0.11.3, < 0.12.0",
4444
"frequenz-client-base >= 0.11.0, < 0.12.0",
4545
]
4646
dynamic = ["version"]

src/frequenz/dispatch/_bg_service.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,18 @@ async def _fetch(self, timer: Timer) -> None:
370370

371371
try:
372372
_logger.debug("Fetching dispatches for microgrid %s", self._microgrid_id)
373-
async for page in self._client.list(microgrid_id=self._microgrid_id):
373+
now = datetime.now(timezone.utc)
374+
async for page in self._client.list(
375+
microgrid_id=self._microgrid_id, end_from=now - timedelta(seconds=5)
376+
):
374377
for client_dispatch in page:
375378
deleted_timestamp = self._deleted_dispatches.get(client_dispatch.id)
376379
if (
377380
deleted_timestamp
378381
and client_dispatch.update_time < deleted_timestamp
379382
):
380383
continue
384+
381385
dispatch = Dispatch(client_dispatch)
382386

383387
new_dispatches[dispatch.id] = dispatch

tests/test_frequenz_dispatch.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,7 @@ async def test_dispatch_new_but_finished(
476476
)
477477
fake_time.shift(timedelta(seconds=1))
478478

479-
await asyncio.sleep(1)
480-
# Process the lifecycle event caused by the old dispatch at startup
481-
await test_env.lifecycle_events.receive()
479+
# Expecting no event for a past and finished dispatch as it should be filtered out
482480

483481
# Create another dispatch the normal way
484482
new_dispatch = generator.generate_dispatch()
@@ -514,7 +512,7 @@ async def test_notification_on_actor_start(
514512
running_dispatch,
515513
active=True,
516514
duration=timedelta(seconds=10),
517-
start_time=_now() - timedelta(seconds=5),
515+
start_time=_now() + timedelta(seconds=5),
518516
recurrence=RecurrenceRule(),
519517
type="TEST_TYPE",
520518
)
@@ -524,7 +522,7 @@ async def test_notification_on_actor_start(
524522
stopped_dispatch,
525523
active=False,
526524
duration=timedelta(seconds=5),
527-
start_time=_now() - timedelta(seconds=5),
525+
start_time=_now() + timedelta(seconds=5),
528526
recurrence=RecurrenceRule(),
529527
type="TEST_TYPE",
530528
)
@@ -536,8 +534,8 @@ async def test_notification_on_actor_start(
536534
)
537535
test_env.service.start()
538536

539-
fake_time.shift(timedelta(seconds=1))
540-
await asyncio.sleep(1)
537+
fake_time.shift(timedelta(seconds=7))
538+
await asyncio.sleep(6)
541539

542540
# Expect notification of the running dispatch being ready to run
543541
ready_dispatch = await test_env.running_state_change.receive()

0 commit comments

Comments
 (0)