Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cache in tests-cacher #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions alma_tests_cacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async def main():
bs_api_url=config.bs_api_url,
bs_jwt_token=config.bs_jwt_token,
gerrit_username=config.gerrit_username,
workdir=config.workdir,
).run()


Expand Down
41 changes: 21 additions & 20 deletions alma_tests_cacher/cacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re
import urllib.parse
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import List, Optional, Union

import aiohttp
Expand All @@ -14,6 +13,7 @@
DEFAULT_LOGGING_LEVEL,
DEFAULT_REQUESTS_LIMIT,
DEFAULT_SLEEP_TIMEOUT,
DEFAULT_WORKDIR_PATH,
)
from alma_tests_cacher.models import PackageTestRepository, TestRepository
from alma_tests_cacher.utils import (
Expand All @@ -32,6 +32,7 @@ def __init__(
bs_api_url: str = DEFAULT_BS_API_URL,
logging_level: str = DEFAULT_LOGGING_LEVEL,
gerrit_username: str = '',
workdir: Path = DEFAULT_WORKDIR_PATH,
):
self.requests_limit = asyncio.Semaphore(requests_limit)
self.sleep_timeout = sleep_timeout
Expand All @@ -43,6 +44,7 @@ def __init__(
self.session_mapping = {}
self.logger = self.setup_logger(logging_level)
self.gerrit_username = gerrit_username
self.workdir = workdir

@staticmethod
def setup_logger(logging_level: str) -> logging.Logger:
Expand Down Expand Up @@ -163,7 +165,7 @@ async def bulk_create_test_folders(
async def process_repo(
self,
repo: TestRepository,
workdir: str,
workdir: Path,
):
async with self.requests_limit:
remote_test_folders = []
Expand Down Expand Up @@ -287,22 +289,21 @@ async def process_repo(
self.logger.info('Repo "%s" is processed', repo.name)

async def run(self, dry_run: bool = False):
with TemporaryDirectory(prefix='alma-cacher-') as workdir:
while True:
self.logger.info('Start processing test repositories')
try:
repos = await self.get_test_repositories()
await asyncio.gather(
*(self.process_repo(repo, workdir) for repo in repos)
)
except Exception:
self.logger.exception('Cannot process test repositories:')
finally:
await self.close_sessions()
self.logger.info(
'All repositories are processed, sleeping %d seconds',
self.sleep_timeout,
while True:
self.logger.info('Start processing test repositories')
try:
repos = await self.get_test_repositories()
await asyncio.gather(
*(self.process_repo(repo, self.workdir) for repo in repos)
)
await asyncio.sleep(self.sleep_timeout)
if dry_run:
break
except Exception:
self.logger.exception('Cannot process test repositories:')
finally:
await self.close_sessions()
self.logger.info(
'All repositories are processed, sleeping %d seconds',
self.sleep_timeout,
)
await asyncio.sleep(self.sleep_timeout)
if dry_run:
break
2 changes: 2 additions & 0 deletions alma_tests_cacher/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
from pathlib import Path

DEFAULT_REQUESTS_LIMIT = 5
DEFAULT_SLEEP_TIMEOUT = 600 # 10 minutes
DEFAULT_BS_API_URL = 'http://web_server:8000'
DEFAULT_LOGGING_LEVEL = 'DEBUG'
DEFAULT_CONFIG_PATH = os.getenv('CONFIG_PATH', 'vars.yaml')
DEFAULT_WORKDIR_PATH = Path.cwd().parent
3 changes: 3 additions & 0 deletions alma_tests_cacher/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pathlib import Path
from typing import List, Optional, Union

from pydantic import BaseModel, Field
Expand All @@ -8,6 +9,7 @@
DEFAULT_LOGGING_LEVEL,
DEFAULT_REQUESTS_LIMIT,
DEFAULT_SLEEP_TIMEOUT,
DEFAULT_WORKDIR_PATH,
)


Expand Down Expand Up @@ -45,3 +47,4 @@ class Config(BaseSettings):
cacher_sentry_dsn: str = ""
cacher_sentry_traces_sample_rate: float = 0.2
gerrit_username: str = ""
workdir: Path = DEFAULT_WORKDIR_PATH
2 changes: 1 addition & 1 deletion alma_tests_cacher/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def prepare_gerrit_repo_url(url: str, username: str) -> str:


def clone_git_repo(
workdir_path: str,
workdir_path: Path,
repo_url: str,
) -> Tuple[int, str, str]:
return (
Expand Down
1 change: 1 addition & 0 deletions tests/test_cacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def cacher(config: Config) -> AlmaTestsCacher:
bs_api_url=config.bs_api_url,
bs_jwt_token=config.bs_jwt_token,
gerrit_username=config.gerrit_username,
workdir=config.workdir,
)


Expand Down
Loading