+# Long-running "git-cat-file --batch" processes won't notice
+# unlinked packs, so we need to restart those processes occasionally.
+# Xapian and SQLite file handles are mostly stable, but sometimes an
+# admin will attempt to replace them atomically after compact/vacuum
+# and we need to be prepared for that.
+my $cleanup_timer;
+my $cleanup_avail = -1; # 0, or 1
+my $have_devel_peek;
+my $CLEANUP = {}; # string(inbox) -> inbox
+sub cleanup_task () {
+ $cleanup_timer = undef;
+ my $next = {};
+ for my $ibx (values %$CLEANUP) {
+ my $again;
+ if ($have_devel_peek) {
+ foreach my $f (qw(mm search over)) {
+ # we bump refcnt by assigning tmp, here:
+ my $tmp = $ibx->{$f} or next;
+ next if Devel::Peek::SvREFCNT($tmp) > 2;
+ delete $ibx->{$f};
+ # refcnt is zero when tmp is out-of-scope
+ }
+ }
+ if (my $git = $ibx->{git}) {
+ $again = $git->cleanup;
+ }
+ if (my $gits = $ibx->{-repo_objs}) {
+ foreach my $git (@$gits) {
+ $again = 1 if $git->cleanup;
+ }
+ }
+ if ($have_devel_peek) {
+ $again ||= !!($ibx->{over} || $ibx->{mm} ||
+ $ibx->{search});
+ }
+ $next->{"$ibx"} = $ibx if $again;
+ }
+ $CLEANUP = $next;
+}
+
+sub cleanup_possible () {
+ # no need to require EvCleanup, here, if it were enabled another
+ # module would've require'd it, already
+ eval { PublicInbox::EvCleanup::enabled() } or return 0;