Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 91
Run update call on recurring schedule#1268
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import threading | ||
| import time | ||
| import structlog | ||
| import codegate | ||
| from codegate.updates.client import Origin, UpdateClient | ||
| logger = structlog.get_logger("codegate") | ||
| class ScheduledUpdateChecker(threading.Thread): | ||
| """ | ||
| ScheduledUpdateChecker calls the UpdateClient on a recurring interval. | ||
| This is implemented as a separate thread to avoid blocking the main thread. | ||
| A dedicated scheduling library could have been used, but the requirements | ||
| are trivial, and a simple hand-rolled solution is sufficient. | ||
| """ | ||
| def __init__(self, client: UpdateClient, interval_seconds: int = 14400): # 4 hours in seconds | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if we want this interval to be externally configurable? | ||
| super().__init__() | ||
| self.__client = client | ||
| self.__interval_seconds = interval_seconds | ||
| def run(self): | ||
| """ | ||
| Overrides the `run` method of threading.Thread. | ||
| """ | ||
| while True: | ||
| logger.info("Checking for CodeGate updates") | ||
| latest = self.__client.get_latest_version(Origin.BackEnd) | ||
| if latest != codegate.__version__: | ||
| logger.warning(f"A new version of CodeGate is available: {latest}") | ||
| time.sleep(self.__interval_seconds) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -75,18 +75,20 @@ def test_health_check(test_client: TestClient) -> None: | ||
| assert response.json() == {"status": "healthy"} | ||
| @patch("codegate.api.v1_processing.fetch_latest_version", return_value="foo") | ||
| def test_version_endpoint(mock_fetch_latest_version, test_client: TestClient) -> None: | ||
| @patch("codegate.api.v1._get_latest_version") | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this test would use a patched out UpdateClient instance, but it's difficult to patch out a singleton with the mocks. This sort of testing would be much easier if the V1 routes were defined in a class, and all dependencies were supplied via the constructor. | ||
| def test_version_endpoint(mock_get_latest_version, test_client: TestClient) -> None: | ||
| """Test the version endpoint.""" | ||
| # Mock the __get_latest_version function to return a specific version | ||
| mock_get_latest_version.return_value = "v1.2.3" | ||
| response = test_client.get("/api/v1/version") | ||
| assert response.status_code == 200 | ||
| response_data = response.json() | ||
| assert response_data["current_version"] == __version__.lstrip("v") | ||
| assert response_data["latest_version"] == "foo" | ||
| assert isinstance(response_data["is_latest"], bool) | ||
| assert response_data["current_version"] == "0.1.7" | ||
| assert response_data["latest_version"] == "1.2.3" | ||
| assert response_data["is_latest"] is False | ||
| assert response_data["error"] is None | ||
| @patch("codegate.pipeline.sensitive_data.manager.SensitiveDataManager") | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved the caching here since I do not want to cache the backend calls.