- $ibx->cleanup;
- process_queue(\@q, $cb, $max, $opt);
- $im->lock_acquire if !$opt->{-coarse_lock};
- commit_changes($ibx, $im, $tmp, $opt);
- });
+sub _run {
+ my ($ibx, $cb, $opt, $reindex) = @_;
+ my $im = $ibx->importer(0);
+ $im->lock_acquire;
+ my ($tmp, $queue) = prepare_run($ibx, $opt);
+
+ # fine-grained locking if we prepare for reindex
+ if (!$opt->{-coarse_lock}) {
+ prepare_reindex($ibx, $im, $reindex);
+ $im->lock_release;
+ }
+
+ $ibx->cleanup;
+ process_queue($queue, $cb, $opt);
+ $im->lock_acquire if !$opt->{-coarse_lock};
+ commit_changes($ibx, $im, $tmp, $opt);
+}
+
+sub run {
+ my ($ibx, $task, $opt) = @_; # task = 'cpdb' or 'compact'
+ my $cb = \&${\"PublicInbox::Xapcmd::$task"};
+ PublicInbox::Admin::progress_prepare($opt ||= {});
+ defined(my $dir = $ibx->{inboxdir}) or die "no inboxdir defined\n";
+ -d $dir or die "inboxdir=$dir does not exist\n";
+ check_compact() if $opt->{compact} && $ibx->search;
+ my $reindex; # v1:{ from => $x40 }, v2:{ from => [ $x40, $x40, .. ] } }
+
+ if (!$opt->{-coarse_lock}) {
+ $reindex = $opt->{reindex} = { # per-epoch ranges for v2
+ from => $ibx->version == 1 ? '' : [],
+ };
+ require PublicInbox::SearchIdx;
+ PublicInbox::SearchIdx::load_xapian_writable();
+ }
+
+ local %SIG = %SIG;
+ setup_signals();
+ $ibx->umask_prepare;
+ $ibx->with_umask(\&_run, $ibx, $cb, $opt, $reindex);