From 7983f052111d9551fc8341d71a15fb4b2ceaae8b Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 4 Sep 2022 13:30:02 -0400 Subject: ci: Move stale job cleanup to get-test-job.c It's more efficient to do it there, and now we emit a log message when we delete a stale test job. Signed-off-by: Kent Overstreet --- ci/get-test-job.sh | 17 +---------------- lib/get-test-job.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/ci/get-test-job.sh b/ci/get-test-job.sh index bf64c6b..8347b08 100755 --- a/ci/get-test-job.sh +++ b/ci/get-test-job.sh @@ -2,24 +2,9 @@ [[ -f ~/.ktestrc ]] && . ~/.ktestrc -# Clean stale test jobs: - -cd $JOBSERVER_OUTPUT_DIR - -if [[ ! -f stale-job-cleanup ]]; then - touch stale-job-cleanup -fi - -if [[ $(find stale-job-cleanup -mmin +5) ]]; then - echo -n "Cleaning stale jobs.. " >&2 - find -size 0 -cmin +180 |xargs rm -f > /dev/null - touch stale-job-cleanup - echo " done" >&2 -fi - cd $JOBSERVER_HOME/linux flock --nonblock .git_fetch.lock git fetch --all > /dev/null make -C ~/ktest/lib get-test-job 1>&2 -~/ktest/lib/get-test-job -b ~/BRANCHES-TO-TEST -o $JOBSERVER_OUTPUT_DIR +~/ktest/lib/get-test-job -b ~/BRANCHES-TO-TEST -o $JOBSERVER_OUTPUT_DIR/c diff --git a/lib/get-test-job.c b/lib/get-test-job.c index f45b947..aab6a2f 100644 --- a/lib/get-test-job.c +++ b/lib/get-test-job.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include @@ -158,6 +159,31 @@ static char *slashes_to_dots(const char *str) return ret; } +static bool __lockfile_exists(const char *commitdir, + const char *testdir, + const char *lockfile, + bool create) +{ + if (!create) { + return access(lockfile, F_OK) == 0; + } else { + bool exists; + + if (mkdir(commitdir, 0755) < 0 && errno != EEXIST) + die("error creating %s", commitdir); + + if (mkdir(testdir, 0755) < 0 && errno != EEXIST) + die("error creating %s", testdir); + + int fd = open(lockfile, O_RDWR|O_CREAT|O_EXCL, 0644); + exists = fd < 0; + if (!exists) + close(fd); + + return exists; + } +} + static bool lockfile_exists(const char *commit, const char *test_path, const char *subtest, @@ -168,21 +194,24 @@ static bool lockfile_exists(const char *commit, char *commitdir = mprintf("%s/%s", outdir, commit); char *testdir = mprintf("%s/%s.%s", commitdir, test_name, subtest_mangled); char *lockfile = mprintf("%s/status", testdir); + struct timeval now; + struct stat statbuf; bool exists; - if (!create) { - exists = access(lockfile, F_OK) == 0; - } else { - if (mkdir(commitdir, 0755) < 0 && errno != EEXIST) - die("error creating %s", commitdir); + gettimeofday(&now, NULL); - if (mkdir(testdir, 0755) < 0 && errno != EEXIST) - die("error creating %s", testdir); + exists = __lockfile_exists(commitdir, testdir, lockfile, create); - int fd = open(lockfile, O_RDWR|O_CREAT|O_EXCL, 0644); - exists = fd < 0; - if (!exists) - close(fd); + if (exists && + !stat(lockfile, &statbuf) && + !statbuf.st_size && + S_ISREG(statbuf.st_mode) && + statbuf.st_ctime + 20 * 60 < now.tv_sec && + !unlink(lockfile)) { + fprintf(stderr, "Deleting stale test job %s %s %s (%lu minutes old)\n", + commit, test_name, subtest, + (now.tv_sec - statbuf.st_ctime) / 60); + exists = false; } free(lockfile); -- cgit v1.2.3