From 3f1e9e397e9e67980314f858eab1e844724be078 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Fri, 7 Jul 2023 06:06:07 -0400 Subject: [PATCH] update_manger: log remaining refresh time on init Signed-off-by: Eric Callahan --- moonraker/components/update_manager/base_deploy.py | 13 ++++++++----- .../components/update_manager/update_manager.py | 4 +++- moonraker/utils/__init__.py | 14 ++++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/moonraker/components/update_manager/base_deploy.py b/moonraker/components/update_manager/base_deploy.py index 53a38be..9f620af 100644 --- a/moonraker/components/update_manager/base_deploy.py +++ b/moonraker/components/update_manager/base_deploy.py @@ -7,6 +7,7 @@ from __future__ import annotations import logging import time +from ...utils import pretty_print_time from typing import TYPE_CHECKING, Dict, Any, Optional if TYPE_CHECKING: @@ -53,12 +54,14 @@ class BaseDeploy: self.last_cfg_hash: str = storage.get('last_config_hash', "") return storage - def needs_refresh(self) -> bool: + def needs_refresh(self, log_remaining_time: bool = False) -> bool: next_refresh_time = self.last_refresh_time + self.refresh_interval - return ( - self.cfg_hash != self.last_cfg_hash or - time.time() > next_refresh_time - ) + remaining_time = int(next_refresh_time - time.time() + .5) + if self.cfg_hash != self.last_cfg_hash or remaining_time <= 0: + return True + if log_remaining_time: + self.log_info(f"Next refresh in: {pretty_print_time(remaining_time)}") + return False def get_last_refresh_time(self) -> float: return self.last_refresh_time diff --git a/moonraker/components/update_manager/update_manager.py b/moonraker/components/update_manager/update_manager.py index 70f7bff..d1da75a 100644 --- a/moonraker/components/update_manager/update_manager.py +++ b/moonraker/components/update_manager/update_manager.py @@ -236,7 +236,9 @@ class UpdateManager: async def _handle_auto_refresh(self, eventtime: float) -> float: cur_hour = time.localtime(time.time()).tm_hour + 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: return eventtime + UPDATE_REFRESH_INTERVAL @@ -256,7 +258,7 @@ class UpdateManager: async with self.cmd_request_lock: try: for name, updater in list(self.updaters.items()): - if updater.needs_refresh(): + if updater.needs_refresh(log_remaining_time): await updater.refresh() need_notify = True except Exception: diff --git a/moonraker/utils/__init__.py b/moonraker/utils/__init__.py index 25993c7..20e8813 100644 --- a/moonraker/utils/__init__.py +++ b/moonraker/utils/__init__.py @@ -250,3 +250,17 @@ def get_unix_peer_credentials( "user_id": uid, "group_id": gid } + +def pretty_print_time(seconds: int) -> str: + if seconds == 0: + return "0 Seconds" + fmt_list: List[str] = [] + times: Dict[str, int] = {} + times["Day"], seconds = divmod(seconds, 86400) + times["Hour"], seconds = divmod(seconds, 3600) + times["Minute"], times["Second"] = divmod(seconds, 60) + for ident, val in times.items(): + if val == 0: + continue + fmt_list.append(f"{val} {ident}" if val == 1 else f"{val} {ident}s") + return ", ".join(fmt_list)