Skip to content

Commit

Permalink
Stop Select in BatteriesStatus and BatteriesStatusTracker
Browse files Browse the repository at this point in the history
Select that is not closed causes errors in unit tests.

Signed-off-by: ela-kotulska-frequenz <elzbieta.kotulska@frequenz.com>
  • Loading branch information
ela-kotulska-frequenz committed Jan 24, 2023
1 parent 210f3d3 commit 6bd3146
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
21 changes: 9 additions & 12 deletions src/frequenz/sdk/actor/power_distributing/_batteries_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def __init__(
channel_name=channel.name, tracker=tracker
)

self._task = asyncio.create_task(self._run(receivers))
self._select: Select = Select(
**receivers,
)

self._task = asyncio.create_task(self._run())

async def stop(self) -> None:
"""Stop tracking batteries status."""
Expand All @@ -141,21 +145,14 @@ async def stop(self) -> None:
for battery in self._batteries.values()
]
)
await self._select.stop()
await cancel_and_await(self._task)

async def _run(self, receivers: Dict[str, Receiver[BatteryStatus]]) -> None:
"""Start tracking batteries status.
Args:
receivers: Map between channel name and receiver for that channel.
"""
select: Select = Select(
**receivers,
)

async def _run(self) -> None:
"""Start tracking batteries status."""
while True:
try:
await self._update_status(select)
await self._update_status(self._select)
except Exception as err: # pylint: disable=broad-except
_logger.error("BatteriesStatus failed with error: %s. Restart.", err)

Expand Down
14 changes: 11 additions & 3 deletions src/frequenz/sdk/microgrid/_battery/_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ class BatteryStatusTracker:
InverterComponentState.COMPONENT_STATE_STANDBY,
}

# Instance attributes defined outside __init__
_select: Optional[Select]

def __init__( # pylint: disable=too-many-arguments
self,
battery_id: int,
Expand Down Expand Up @@ -182,6 +185,8 @@ def __init__( # pylint: disable=too-many-arguments

self._battery = _ComponentData[BatteryData](battery_id)
self._inverter = _ComponentData[InverterData](inverter_id)
# Select needs receivers that can be get in async way only.
self._select = None

self._task = asyncio.create_task(
self._run(status_sender, request_result_receiver)
Expand All @@ -198,6 +203,9 @@ def battery_id(self) -> int:

async def stop(self) -> None:
"""Stop tracking battery status."""
if self._select is not None:
await self._select.stop()

await cancel_and_await(self._task)

async def _run(
Expand All @@ -219,16 +227,16 @@ async def _run(
battery_receiver = await api_client.battery_data(self._battery.component_id)
inverter_receiver = await api_client.inverter_data(self._inverter.component_id)

select = Select(
self._select = Select(
battery=battery_receiver,
inverter=inverter_receiver,
request_result=request_result_receiver,
)

while True:
try:
while await select.ready():
new_status = self._update_status(select)
while await self._select.ready():
new_status = self._update_status(self._select)

if new_status is not None:
await status_sender.send(new_status)
Expand Down

0 comments on commit 6bd3146

Please sign in to comment.