From ebf7e042821dd79818fc4f7953e837eabc5c6400 Mon Sep 17 00:00:00 2001 From: BoYanZh Date: Sun, 3 Nov 2024 01:45:41 -0400 Subject: [PATCH] feat: clean git lock file for joj3 --- joint_teapot/app.py | 12 +++++++++--- joint_teapot/workers/git.py | 10 +++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/joint_teapot/app.py b/joint_teapot/app.py index 9c22cf0..07dae1e 100644 --- a/joint_teapot/app.py +++ b/joint_teapot/app.py @@ -264,7 +264,9 @@ def joj3_scoreboard( ) with FileLock(lock_file_path, timeout=settings.joj3_lock_file_timeout).acquire(): logger.info("file lock acquired") - repo_path = tea.pot.git.repo_clean_and_checkout(repo_name, "grading") + repo_path = tea.pot.git.repo_clean_and_checkout( + repo_name, "grading", clean_git_lock=True + ) repo: Repo = tea.pot.git.get_repo(repo_name) if "grading" not in repo.remote().refs: logger.error( @@ -344,7 +346,9 @@ def joj3_failed_table( ) with FileLock(lock_file_path, timeout=settings.joj3_lock_file_timeout).acquire(): logger.info("file lock acquired") - repo_path = tea.pot.git.repo_clean_and_checkout(repo_name, "grading") + repo_path = tea.pot.git.repo_clean_and_checkout( + repo_name, "grading", clean_git_lock=True + ) repo: Repo = tea.pot.git.get_repo(repo_name) if "grading" not in repo.remote().refs: logger.error( @@ -555,7 +559,9 @@ def joj3_all( retry_interval = 1 git_push_ok = False while not git_push_ok: - repo_path = tea.pot.git.repo_clean_and_checkout(repo_name, "grading") + repo_path = tea.pot.git.repo_clean_and_checkout( + repo_name, "grading", clean_git_lock=True + ) repo: Repo = tea.pot.git.get_repo(repo_name) if "grading" not in repo.remote().refs: logger.error( diff --git a/joint_teapot/workers/git.py b/joint_teapot/workers/git.py index 081f77c..8c5b278 100644 --- a/joint_teapot/workers/git.py +++ b/joint_teapot/workers/git.py @@ -72,7 +72,11 @@ def get_repo(self, repo_name: str) -> Optional[Repo]: return self.clone_repo(repo_name) def repo_clean_and_checkout( - self, repo_name: str, checkout_dest: str, auto_retry: bool = True + self, + repo_name: str, + checkout_dest: str, + auto_retry: bool = True, + clean_git_lock: bool = False, ) -> str: repo_dir = os.path.join(self.repos_dir, repo_name) repo = self.get_repo(repo_name) @@ -81,6 +85,10 @@ def repo_clean_and_checkout( retry_interval = 2 while retry_interval and auto_retry: try: + if clean_git_lock and os.path.exists( + os.path.join(repo_dir, ".git/index.lock") + ): + os.remove(os.path.join(repo_dir, ".git/index.lock")) repo.git.fetch("--tags", "--all", "-f") repo.git.reset("--hard", "origin/master") repo.git.clean("-d", "-f", "-x")