Skip to content

Fetch dispatches in sync with periodic reconnect of client.stream #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: v0.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* This release now supports the sdk up to rc2000.
* Less logs are now on `INFO` level, and more on `DEBUG` level, making the output less verbose.
* Changed the type of `DispatchInfo.components` from `list[int] | list[ComponentCategory]` to `list[ComponentId] | list[ComponentCategory]`, where `ComponentId` is imported from `frequenz.client.microgrid`.
* While the dispatch stream restarts we refresh our dispatch cache as well, to ensure we didn't miss any updates.

## Bug Fixes

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ dependencies = [
# plugins.mkdocstrings.handlers.python.import)
"frequenz-sdk >= 1.0.0-rc2000, < 1.0.0-rc2100",
"frequenz-channels >= 1.6.1, < 2.0.0",
"frequenz-client-dispatch >= 0.10.1, < 0.11.0",
"frequenz-client-dispatch >= 0.10.2, < 0.11.0",
#"frequenz-client-base >= 0.11.0, < 0.13.0",
"frequenz-client-base @ git+https://github.com/frequenz-floss/frequenz-client-base-python@refs/pull/146/head",
]
dynamic = ["version"]

Expand Down
54 changes: 36 additions & 18 deletions src/frequenz/dispatch/_bg_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import grpc.aio
from frequenz.channels import Broadcast, Receiver, select, selected_from
from frequenz.channels.timer import SkipMissedAndResync, Timer
from frequenz.client.base.streaming import StreamStartedEvent
from frequenz.client.dispatch import DispatchApiClient
from frequenz.client.dispatch.types import DispatchEvent as ApiDispatchEvent
from frequenz.client.dispatch.types import Event
from frequenz.sdk.actor import BackgroundService

Expand Down Expand Up @@ -244,24 +246,40 @@ async def _run(self) -> None:
heappop(self._scheduled_events).dispatch
)
elif selected_from(selected, stream):
_logger.debug("Received dispatch event: %s", selected.message)
dispatch = Dispatch(selected.message.dispatch)
match selected.message.event:
case Event.CREATED:
self._dispatches[dispatch.id] = dispatch
await self._update_dispatch_schedule_and_notify(dispatch, None)
await self._lifecycle_events_tx.send(Created(dispatch=dispatch))
case Event.UPDATED:
await self._update_dispatch_schedule_and_notify(
dispatch, self._dispatches[dispatch.id]
)
self._dispatches[dispatch.id] = dispatch
await self._lifecycle_events_tx.send(Updated(dispatch=dispatch))
case Event.DELETED:
self._dispatches.pop(dispatch.id)
await self._update_dispatch_schedule_and_notify(None, dispatch)

await self._lifecycle_events_tx.send(Deleted(dispatch=dispatch))
match selected.message:
case StreamStartedEvent():
_logger.info("Dispatch stream restarted, refreshing dispatches")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"(re)started"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically we never will get the start notification because we create the new_receiver() only after it was sent

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless the channel's resend_latest is True and there are no messages in between. But point taken.

await self._fetch()

case ApiDispatchEvent():
_logger.debug("Received dispatch event: %s", selected.message)
dispatch = Dispatch(selected.message.dispatch)
match selected.message.event:
case Event.CREATED:
self._dispatches[dispatch.id] = dispatch
await self._update_dispatch_schedule_and_notify(
dispatch, None
)
await self._lifecycle_events_tx.send(
Created(dispatch=dispatch)
)
case Event.UPDATED:
await self._update_dispatch_schedule_and_notify(
dispatch, self._dispatches[dispatch.id]
)
self._dispatches[dispatch.id] = dispatch
await self._lifecycle_events_tx.send(
Updated(dispatch=dispatch)
)
case Event.DELETED:
self._dispatches.pop(dispatch.id)
await self._update_dispatch_schedule_and_notify(
None, dispatch
)

await self._lifecycle_events_tx.send(
Deleted(dispatch=dispatch)
)

async def _execute_scheduled_event(self, dispatch: Dispatch) -> None:
"""Execute a scheduled event.
Expand Down