33
44"""The dispatch actor."""
55
6+ import asyncio
67import logging
78from dataclasses import dataclass , field
89from datetime import datetime , timedelta , timezone
1314from frequenz .channels .timer import SkipMissedAndResync , Timer
1415from frequenz .client .dispatch import Client
1516from frequenz .client .dispatch .types import Event
16- from frequenz .sdk .actor import Actor
17+ from frequenz .sdk .actor import BackgroundService
1718
1819from ._dispatch import Dispatch
1920from ._event import Created , Deleted , DispatchEvent , Updated
2324
2425
2526# pylint: disable=too-many-instance-attributes
26- class DispatchingActor ( Actor ):
27- """Dispatch actor .
27+ class DispatchBackgroundService ( BackgroundService ):
28+ """Dispatch background service .
2829
29- This actor is responsible for handling dispatches for a microgrid.
30-
31- This means staying in sync with the API and scheduling
32- dispatches as necessary.
30+ This service is responsible for managing dispatches and scheduling them
31+ based on their start and stop times.
3332 """
3433
3534 @dataclass (order = True )
@@ -52,7 +51,7 @@ def __init__(
5251 microgrid_id : int ,
5352 client : Client ,
5453 ) -> None :
55- """Initialize the actor .
54+ """Initialize the background service .
5655
5756 Args:
5857 microgrid_id: The microgrid ID to handle dispatches for.
@@ -81,7 +80,7 @@ def __init__(
8180 Interval is chosen arbitrarily, as it will be reset on the first event.
8281 """
8382
84- self ._scheduled_events : list ["DispatchingActor .QueueItem" ] = []
83+ self ._scheduled_events : list ["DispatchBackgroundService .QueueItem" ] = []
8584 """The scheduled events, sorted by time.
8685
8786 Each event is a tuple of the scheduled time and the dispatch.
@@ -130,9 +129,16 @@ async def new_running_state_event_receiver(self, type: str) -> Receiver[Dispatch
130129
131130 # pylint: enable=redefined-builtin
132131
132+ def start (self ) -> None :
133+ """Start the background service."""
134+ self ._tasks .add (asyncio .create_task (self ._run ()))
135+
133136 async def _run (self ) -> None :
134- """Run the actor."""
135- _logger .info ("Starting dispatch actor for microgrid %s" , self ._microgrid_id )
137+ """Run the background service."""
138+ _logger .info (
139+ "Starting dispatching background service for microgrid %s" ,
140+ self ._microgrid_id ,
141+ )
136142
137143 # Initial fetch
138144 await self ._fetch ()
0 commit comments