From aa8a0a03e8552c004c5d34c640e8e5e282e236e3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 1 Sep 2023 19:43:25 -0500 Subject: [PATCH] refactor: reduce duplicate code in engine.py --- src/zeroconf/_engine.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/zeroconf/_engine.py b/src/zeroconf/_engine.py index 44435750e..a74c091c7 100644 --- a/src/zeroconf/_engine.py +++ b/src/zeroconf/_engine.py @@ -80,8 +80,7 @@ def setup(self, loop: asyncio.AbstractEventLoop, loop_thread_ready: Optional[thr async def _async_setup(self, loop_thread_ready: Optional[threading.Event]) -> None: """Set up the instance.""" - assert self.loop is not None - self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup) + self._async_schedule_next_cache_cleanup() await self._async_create_endpoints() assert self.running_event is not None self.running_event.set() @@ -118,8 +117,13 @@ def _async_cache_cleanup(self) -> None: now, [RecordUpdate(record, record) for record in self.zc.cache.async_expire(now)] ) self.zc.record_manager.async_updates_complete(False) - assert self.loop is not None - self._cleanup_timer = self.loop.call_later(_CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup) + self._async_schedule_next_cache_cleanup() + + def _async_schedule_next_cache_cleanup(self) -> None: + """Schedule the next cache cleanup.""" + loop = self.loop + assert loop is not None + self._cleanup_timer = loop.call_at(loop.time() + _CACHE_CLEANUP_INTERVAL, self._async_cache_cleanup) async def _async_close(self) -> None: """Cancel and wait for the cleanup task to finish."""