From df9704f1db0bf736b78f6b8348bd88499621d95f Mon Sep 17 00:00:00 2001 From: Colin Rofls Date: Wed, 4 Sep 2024 13:42:27 -0400 Subject: [PATCH] Use a lock file to prevent concurrent runs We want to make sure the same machine is only ever executing one run of the ci script at a time, so we will write a sentinal file when we start execution, and delete it when we're done. --- .gitignore | 1 + run.sh | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 14180e0..eaf20cb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ venv/ .DS_Store fontc/ GITHUB_TOKEN +results/CRATER.lock diff --git a/run.sh b/run.sh index 21998bd..1554ebc 100755 --- a/run.sh +++ b/run.sh @@ -16,6 +16,7 @@ export FONTC_CRATER_RESULTS=$(realpath ./results) export FONTC_CRATER_INPUT=$(realpath "gf-repos-2024-08-12.json") GENERATED_HTML="$FONTC_CRATER_RESULTS/index.html" +LOCKFILE="$FONTC_CRATER_RESULTS/CRATER.lock" FONTC_REPO=https://github.com/googlefonts/fontc.git FONTC_DIR=./fontc FONTC_REQUIREMENTS="$FONTC_DIR/resources/scripts/requirements.txt" @@ -23,11 +24,27 @@ FONTC_REQUIREMENTS="$FONTC_DIR/resources/scripts/requirements.txt" SCRIPT_PATH=fontc_crater/resources/ci.sh GITHUB_TOKEN=$(<"GITHUB_TOKEN") +cleanup() { + echo "cleaning up" + rm -rf $FONTC_DIR + rm $LOCKFILE + deactivate +} + +# acquire lock, or bail: +if [ -f $LOCKFILE ]; then + echo "$LOCKFILE exists (CI is already running, or failed and requires manual cleanup)" + exit 1 +else + touch $LOCKFILE +fi + # make sure that the upstream repo is configured to authenticate with our token: git remote set-url origin "https://$GITHUB_TOKEN:x-oauth-basic@github.com/googlefonts/fontc_crater.git" if [ $? -ne 0 ]; then echo "failed to set upstream" + cleanup exit 1 fi @@ -35,6 +52,7 @@ fi git pull if [ $? -ne 0 ]; then echo "git pull failed, exiting" + cleanup exit 1 fi @@ -46,6 +64,7 @@ echo "setting up venv" python -m venv venv if [ $? -ne 0 ]; then echo could not setup venv, exiting + cleanup exit 1 fi @@ -63,9 +82,6 @@ if git clone $FONTC_REPO $FONTC_DIR ; then ( $SCRIPT_PATH ) cd .. fi -echo "cleaning up" -rm -rf $FONTC_DIR -deactivate # move index.html from results/ to repo root mv $GENERATED_HTML index.html @@ -76,3 +92,6 @@ git commit -m 'Automated commit' if [ $? -eq 0 ]; then git push fi + +# finally, cleanup if we got this far +cleanup