diff options
Diffstat (limited to 'lib/get-test-job.c')
-rw-r--r-- | lib/get-test-job.c | 51 |
1 files changed, 40 insertions, 11 deletions
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 <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <sys/time.h> #include <sys/types.h> #include <unistd.h> @@ -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); |