Skip to content

Commit

Permalink
only check for upgrades once every 12 hours
Browse files Browse the repository at this point in the history
  • Loading branch information
epwalsh committed Jun 13, 2024
1 parent 5878465 commit df27ac8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use patch releases for compatibility fixes instead.

## Unreleased

### Fixed

- Only check for upgrades once every 12 hours by default.

## [v1.29.1](https://github.com/allenai/beaker-py/releases/tag/v1.29.1) - 2024-06-13

### Fixed
Expand Down
18 changes: 16 additions & 2 deletions beaker/client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import time
from contextlib import contextmanager
from typing import Generator, Optional, Tuple, Union

Expand All @@ -8,7 +9,7 @@
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry

from .config import Config
from .config import Config, InternalConfig
from .data_model import *
from .exceptions import *
from .services import *
Expand Down Expand Up @@ -70,6 +71,7 @@ class Beaker:

API_VERSION = "v3"
CLIENT_VERSION = VERSION
VERSION_CHECK_INTERVAL = 12 * 3600 # 12 hours

logger = logging.getLogger("beaker")

Expand Down Expand Up @@ -138,6 +140,14 @@ def _check_for_upgrades(self):
if _LATEST_VERSION_CHECKED:
return

config = InternalConfig.load()
if (
config is not None
and config.version_checked is not None
and (time.time() - config.version_checked <= self.VERSION_CHECK_INTERVAL)
):
return

import warnings

import packaging.version
Expand All @@ -149,7 +159,6 @@ def _check_for_upgrades(self):
)
if response.ok:
latest_version = packaging.version.parse(response.json()["tag_name"])
_LATEST_VERSION_CHECKED = True
if latest_version > packaging.version.parse(self.CLIENT_VERSION):
warnings.warn(
f"You're using beaker-py v{self.CLIENT_VERSION}, "
Expand All @@ -159,6 +168,11 @@ def _check_for_upgrades(self):
f"https://github.com/allenai/beaker-py/releases/tag/v{latest_version}\n",
UserWarning,
)

_LATEST_VERSION_CHECKED = True
if config is not None:
config.version_checked = time.time()
config.save()
except Exception:
pass

Expand Down
28 changes: 28 additions & 0 deletions beaker/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import os
import warnings
Expand All @@ -10,8 +11,10 @@
from .exceptions import ConfigurationError

DEFAULT_CONFIG_LOCATION: Optional[Path] = None
DEFAULT_INTERNAL_CONFIG_LOCATION: Optional[Path] = None
try:
DEFAULT_CONFIG_LOCATION = Path.home() / ".beaker" / "config.yml"
DEFAULT_INTERNAL_CONFIG_LOCATION = Path.home() / ".beaker" / ".beaker-py.json"
except RuntimeError:
# Can't locate home directory.
pass
Expand Down Expand Up @@ -156,3 +159,28 @@ def find_config(cls) -> Optional[Path]:
return DEFAULT_CONFIG_LOCATION

return None


@dataclass
class InternalConfig:
version_checked: Optional[float] = None

@classmethod
def load(cls) -> Optional["InternalConfig"]:
path = DEFAULT_INTERNAL_CONFIG_LOCATION
if path is None:
return None
elif path.is_file():
with open(path, "r") as f:
return cls(**json.load(f))
else:
return cls()

def save(self):
path = DEFAULT_INTERNAL_CONFIG_LOCATION
if path is None:
return None
else:
path.parent.mkdir(exist_ok=True, parents=True)
with open(path, "w") as f:
json.dump(asdict(self), f)

0 comments on commit df27ac8

Please sign in to comment.