From 97c5db97e30b7af527798310ab29fb99a45386f9 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 11 Feb 2020 15:01:49 +0000 Subject: [PATCH] git-cache-proxy: Refactor to prepare for 2nd kind of housekeeping 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 --- scripts/git-cache-proxy | 60 +++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/scripts/git-cache-proxy b/scripts/git-cache-proxy index 52e01d0..d798f2b 100755 --- a/scripts/git-cache-proxy +++ b/scripts/git-cache-proxy @@ -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: $!"; -- 2.30.2