chiark / gitweb /
git-cache-proxy: Refactor to prepare for 2nd kind of housekeeping
authorIan Jackson <ian.jackson@eu.citrix.com>
Tue, 11 Feb 2020 15:01:49 +0000 (15:01 +0000)
committerIan Jackson <ian.jackson@eu.citrix.com>
Tue, 11 Feb 2020 15:58:45 +0000 (15:58 +0000)
Generalise the housekeeping action code.
Introduce new variables $mode_what, $mode_locknb, and $mode_action.
Move the actual cleanup functionality into the $mode_action value.

No overall functional change.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
scripts/git-cache-proxy

index 52e01d0203b0e210200119cb42b57eea76ff7dbb..d798f2bab30fbbdfc4a940bc5d7882db8c5963ab 100755 (executable)
@@ -400,39 +400,47 @@ sub housekeeping () {
            $! == ENOENT or hkfail "$lock: lstat: $!";
            next;
        }
+       my ($mode_what,$mode_locknb,$mode_action);
        if (-M _ <= $treeexpiredays) {
            logm 'debug', "housekeeping: subdirs $subdir: touched recently";
            next;
+       } else {
+           $mode_what = 'cleaning';
+           $mode_locknb = LOCK_NB;
+           $mode_action = sub {
+               eval {
+                   foreach my $suffix (qw(tmp git)) {
+                       my $dir = "${subdir}\\.$suffix";
+                       my $tdir = "${subdir}\\.tmp";
+                       if ($dir ne $tdir) {
+                           if (!rename $dir,$tdir) {
+                               next if $! == ENOENT;
+                               die "$dir: cannot rename to $tdir: $!\n";
+                           }
+                       }
+                       system qw(rm -rf --), $tdir;
+                       if (stat $tdir) {
+ die "$dir: problem deleting file(s), rm exited $?\n";
+                       } elsif ($! != ENOENT) {
+                           die "$tdir: cannot stat after deletion: $!\n";
+                       }
+                   }
+               };
+               if (length $@) {
+                   chomp $@;
+ logm 'warning', "housekeeping: $subdir: cleanup prevented: $@";
+               } else {
+                   unlink $lock or hkfail "remove $lock: $!";
+               }
+           };
        }
-       if (!lockfile \*LOCK, $lock, LOCK_EX|LOCK_NB) {
+       if (!lockfile \*LOCK, $lock, LOCK_EX|$mode_locknb) {
+            die $! unless $mode_locknb;
            logm 'info', "housekeeping: subdirs $subdir: lock busy, skipping";
            next;
        }
-       logm 'info', "housekeeping: subdirs $subdir: cleaning";
-       eval {
-           foreach my $suffix (qw(tmp git)) {
-               my $dir = "${subdir}\\.$suffix";
-               my $tdir = "${subdir}\\.tmp";
-               if ($dir ne $tdir) {
-                   if (!rename $dir,$tdir) {
-                       next if $! == ENOENT;
-                       die "$dir: cannot rename to $tdir: $!\n";
-                   }
-               }
-               system qw(rm -rf --), $tdir;
-               if (stat $tdir) {
-                   die "$dir: problem deleting file(s), rm exited $?\n";
-               } elsif ($! != ENOENT) {
-                   die "$tdir: cannot stat after deletion: $!\n";
-               }
-           }
-       };
-       if (length $@) {
-           chomp $@;
-           logm 'warning', "housekeeping: $subdir: cleanup prevented: $@";
-       } else {
-           unlink $lock or hkfail "remove $lock: $!";
-       }
+       logm 'info', "housekeeping: subdirs $subdir: $mode_what";
+       $mode_action->();
     }
     open HS, ">", "Housekeeping.stamp" or hkfail "touch Housekeeping.stamp: $!";
     close HS or hkfail "close Housekeeping.stamp: $!";