diff --git a/run.sh b/run.sh index bf66245c7a..f499439714 100755 --- a/run.sh +++ b/run.sh @@ -108,9 +108,8 @@ fi mkdir -p "$OUT_DIR" # Delete all locks in cache -if [ -d "$CACHE_DIR" ]; then - find "$CACHE_DIR" -name "*.lock" -delete -fi +find "$CACHE_DIR" -name "*.lock" -delete +find "repos" -name "*.lock" -delete # Delete .workdir rm -rf .workdir diff --git a/src/python/repo.py b/src/python/repo.py index 28456c50a2..176e4b67a4 100755 --- a/src/python/repo.py +++ b/src/python/repo.py @@ -23,6 +23,7 @@ set_in_cache, lookup_in_cache, ) +import fasteners import git.repo from variables import ( REPOS_PATH, @@ -209,21 +210,16 @@ def __init__( # pylint: disable=too-many-arguments def clone_repo(self) -> None: """Clones the repository.""" - if self.repo_path.exists(): - return - print( - "Cloning", - self.repo_slug, - "to", - self.repo_path, - "because:", - self.repo_path.exists(), - ) - try: - clone_repo(self.repo_slug, self.repo_path) - except Exception as e: - logger.error("Exception during cloning:\n", e) - raise + lock_path = REPOS_PATH / "locks" / self.repo_slug + lock = fasteners.InterProcessLock(lock_path) + with lock: + if self.repo_path.exists(): + return + try: + clone_repo(self.repo_slug, self.repo_path) + except Exception as e: + logger.error("Exception during cloning:\n", e) + raise if not self.repo_path.exists(): logger.error( f"Repo {self.repo_slug} does not exist after cloning {self.repo_path}"