From bac4c732d48e99f94b6099971b1333aef2bcaba5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Doma=C5=84ski?= Date: Tue, 13 Feb 2024 20:57:40 +0100 Subject: [PATCH] Allow configuring the update refresh window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This enables customizing the time of day within which periodic refresh occurs. Previously docs said it happens between 12am-4am which is misleading, because the code effectively allowed refresh until 4:59:59 AM. The default window is now '0-5', which is practically the same as before. The comment about checking every 24h hours has been fixed as well. This hasn't been true since commit c4fa76f21792943ea512d24326259f01d2fa7e89 Signed-off-by: Kamil DomaƄski --- docs/configuration.md | 7 +++-- .../update_manager/update_manager.py | 27 +++++++++++++++---- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index b867f6ce6..161ab446a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -1746,11 +1746,14 @@ disk or cloned from unofficial sources are not supported. [update_manager] enable_auto_refresh: False -# When set to True Moonraker will attempt to fetch status about -# available updates roughly every 24 hours, between 12am-4am. +# When set to True, Moonraker will check roughly every 1 hour (only within +# the update window) whether it's time to fetch status about available updates. # When set to False Moonraker will only fetch update state on startup # and clients will need to request that Moonraker updates state. The # default is False. +refresh_window: 0-5 +# The hours between which the periodic update check will be done. +# Default is 0-5, meaning the refresh can only occur from midnight until 5am. refresh_interval: 672 # The interval (in hours) after which the update manager will check # for new updates. This interval is applies to updates for Moonraker, diff --git a/moonraker/components/update_manager/update_manager.py b/moonraker/components/update_manager/update_manager.py index 86bd3448e..416d76fa0 100644 --- a/moonraker/components/update_manager/update_manager.py +++ b/moonraker/components/update_manager/update_manager.py @@ -48,8 +48,6 @@ # Check To see if Updates are necessary each hour UPDATE_REFRESH_INTERVAL = 3600. -# Perform auto refresh no later than 4am -MAX_UPDATE_HOUR = 4 def get_deploy_class( app_type: Union[AppType, str], default: _T @@ -126,6 +124,18 @@ def __init__(self, config: ConfigHelper) -> None: # Auto Status Refresh self.refresh_timer: Optional[FlexTimer] = None if auto_refresh_enabled: + self.refresh_window = config.getintlist('refresh_window', [0, 5], + separator='-', count=2) + if ( + not (0 <= self.refresh_window[0] <= 23) or + not (0 <= self.refresh_window[1] <= 23) + ): + raise config.error("The hours specified in 'refresh_window'" + " must be between 0 and 23.") + if self.refresh_window[0] == self.refresh_window[1]: + raise config.error("The start and end hours specified" + "in 'refresh_window' cannot be the same.") + self.refresh_timer = self.event_loop.register_timer( self._handle_auto_refresh) @@ -224,13 +234,20 @@ async def _update_klipper_repo(self, updater: BaseDeploy, notify: bool) -> None: if notify: self.cmd_helper.notify_update_refreshed() - async def _handle_auto_refresh(self, eventtime: float) -> float: + def _is_within_refresh_window(self) -> bool: cur_hour = time.localtime(time.time()).tm_hour + if self.refresh_window[0] < self.refresh_window[1]: + return self.refresh_window[0] <= cur_hour < self.refresh_window[1] + return cur_hour >= self.refresh_window[0] or cur_hour < self.refresh_window[1] + + async def _handle_auto_refresh(self, eventtime: float) -> float: log_remaining_time = True if self.initial_refresh_complete: log_remaining_time = False - # Update when the local time is between 12AM and 5AM - if cur_hour >= MAX_UPDATE_HOUR: + # Update only if within the refresh window + if not self._is_within_refresh_window(): + logging.debug("update_manager: current time is outside of" + " the refresh window, auto refresh rescheduled") return eventtime + UPDATE_REFRESH_INTERVAL if self.kconn.is_printing(): # Don't Refresh during a print