Skip to content

Commit

Permalink
Allow configuring the update refresh window
Browse files Browse the repository at this point in the history
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 c4fa76f

Signed-off-by: Kamil Domański <[email protected]>
  • Loading branch information
kdomanski committed Feb 16, 2024
1 parent 67c98f6 commit e8e5803
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
8 changes: 6 additions & 2 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1746,11 +1746,15 @@ 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.
# It can go over midnight, e.g. 22-6.
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,
Expand Down
28 changes: 23 additions & 5 deletions moonraker/components/update_manager/update_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,7 +67,20 @@ def __init__(self, config: ConfigHelper) -> None:
self.kconn: KlippyConnection
self.kconn = self.server.lookup_component("klippy_connection")
self.app_config = get_base_configuration(config)

auto_refresh_enabled = config.getboolean('enable_auto_refresh', False)
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.cmd_helper = CommandHelper(config, self.get_updaters)
self.updaters: Dict[str, BaseDeploy] = {}
if config.getboolean('enable_system_updates', True):
Expand Down Expand Up @@ -224,13 +235,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
Expand Down

0 comments on commit e8e5803

Please sign in to comment.