update_manger: log remaining refresh time on init

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2023-07-07 06:06:07 -04:00
parent 1c16233da0
commit 3f1e9e397e
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
3 changed files with 25 additions and 6 deletions

View File

@ -7,6 +7,7 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
import time import time
from ...utils import pretty_print_time
from typing import TYPE_CHECKING, Dict, Any, Optional from typing import TYPE_CHECKING, Dict, Any, Optional
if TYPE_CHECKING: if TYPE_CHECKING:
@ -53,12 +54,14 @@ class BaseDeploy:
self.last_cfg_hash: str = storage.get('last_config_hash', "") self.last_cfg_hash: str = storage.get('last_config_hash', "")
return storage 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 next_refresh_time = self.last_refresh_time + self.refresh_interval
return ( remaining_time = int(next_refresh_time - time.time() + .5)
self.cfg_hash != self.last_cfg_hash or if self.cfg_hash != self.last_cfg_hash or remaining_time <= 0:
time.time() > next_refresh_time 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: def get_last_refresh_time(self) -> float:
return self.last_refresh_time return self.last_refresh_time

View File

@ -236,7 +236,9 @@ class UpdateManager:
async def _handle_auto_refresh(self, eventtime: float) -> float: async def _handle_auto_refresh(self, eventtime: float) -> float:
cur_hour = time.localtime(time.time()).tm_hour cur_hour = time.localtime(time.time()).tm_hour
log_remaining_time = True
if self.initial_refresh_complete: if self.initial_refresh_complete:
log_remaining_time = False
# Update when the local time is between 12AM and 5AM # Update when the local time is between 12AM and 5AM
if cur_hour >= MAX_UPDATE_HOUR: if cur_hour >= MAX_UPDATE_HOUR:
return eventtime + UPDATE_REFRESH_INTERVAL return eventtime + UPDATE_REFRESH_INTERVAL
@ -256,7 +258,7 @@ class UpdateManager:
async with self.cmd_request_lock: async with self.cmd_request_lock:
try: try:
for name, updater in list(self.updaters.items()): for name, updater in list(self.updaters.items()):
if updater.needs_refresh(): if updater.needs_refresh(log_remaining_time):
await updater.refresh() await updater.refresh()
need_notify = True need_notify = True
except Exception: except Exception:

View File

@ -250,3 +250,17 @@ def get_unix_peer_credentials(
"user_id": uid, "user_id": uid,
"group_id": gid "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)