]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei up: support --all=local
authorEric Wong <e@80x24.org>
Mon, 19 Apr 2021 23:49:01 +0000 (14:49 -0900)
committerEric Wong <e@80x24.org>
Tue, 20 Apr 2021 19:02:46 +0000 (19:02 +0000)
Users may wish to update several saved searches at once.  We can
support parallel updates in lei-daemon so users won't have to do
it themselves via xargs or similar.

Supporting IMAP outputs would be significantly more involved
since we'd have to pre-authenticate for every single IMAP
output before entering the redispatch loop.

lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiQuery.pm
lib/PublicInbox/LeiUp.pm
t/lei-q-save.t

index 5ee02f643a11304eb2a118450e4b464bebd76ff7..c0385eb59320d7177b2f9c2284bc8dd7cd6d05f0 100644 (file)
@@ -138,8 +138,9 @@ our %CMD = ( # sorted in order of importance/use:
        sort|s=s reverse|r offset=i pretty jobs|j=s globoff|g augment|a
        import-before! lock=s@ rsyncable alert=s@ mua=s verbose|v+), @c_opt,
        opt_dash('limit|n=i', '[0-9]+') ],
-'up' => [ 'OUTPUT', 'update saved search',
-       qw(jobs|j=s lock=s@ alert=s@ mua=s verbose|v+ all), @c_opt ],
+
+'up' => [ 'OUTPUT|--all', 'update saved search',
+       qw(jobs|j=s lock=s@ alert=s@ mua=s verbose|v+ all:s), @c_opt ],
 
 'blob' => [ 'OID', 'show a git blob, reconstructing from mail if necessary',
        qw(git-dir=s@ cwd! verbose|v+ mail! oid-a|A=s path-a|a=s path-b|b=s),
@@ -331,6 +332,8 @@ my %OPTDESC = (
 'remote' => 'limit operations to those requiring network access',
 'remote!' => 'prevent operations requiring network access',
 
+'all:s up' => ['local', 'update all (local) saved searches' ],
+
 'mid=s' => 'specify the Message-ID of a message',
 'oid=s' => 'specify the git object ID of a message',
 
index 385ba0a9b4cad38af59a23913b703344eb05339f..4099b26c85ce87e4b0005f98008987d2e518d72b 100644 (file)
@@ -14,6 +14,7 @@ sub prep_ext { # externals_each callback
 
 sub _start_query { # used by "lei q" and "lei up"
        my ($self) = @_;
+       require PublicInbox::LeiOverview;
        PublicInbox::LeiOverview->new($self) or return;
        my $opt = $self->{opt};
        my ($xj, $mj) = split(/,/, $opt->{jobs} // '');
@@ -117,7 +118,6 @@ sub lxs_prepare {
 # the main "lei q SEARCH_TERMS" method
 sub lei_q {
        my ($self, @argv) = @_;
-       require PublicInbox::LeiOverview;
        PublicInbox::Config->json; # preload before forking
        my $lxs = lxs_prepare($self) or return;
        $self->ale->refresh_externals($lxs);
index e80ccf57f3cceecc12f35999460c7a78fdfc86ef..0fb9698b0394c3b535d353e638ef7eb1ed300983 100644 (file)
@@ -6,15 +6,14 @@ package PublicInbox::LeiUp;
 use strict;
 use v5.10.1;
 use PublicInbox::LeiSavedSearch;
-use PublicInbox::LeiOverview;
+use parent qw(PublicInbox::IPC);
 
-sub lei_up {
+sub up1 ($$) {
        my ($lei, $out) = @_;
-       $lei->{lse} = $lei->_lei_store(1)->search;
        my $lss = PublicInbox::LeiSavedSearch->up($lei, $out) or return;
+       my $f = $lss->{'-f'};
        my $mset_opt = $lei->{mset_opt} = { relevance => -2 };
        $mset_opt->{limit} = $lei->{opt}->{limit} // 10000;
-       my $f = $lss->{'-f'};
        my $q = $mset_opt->{q_raw} = $lss->{-cfg}->{'lei.q'} //
                                return $lei->fail("lei.q unset in $f");
        my $lse = $lei->{lse} // die 'BUG: {lse} missing';
@@ -40,10 +39,60 @@ sub lei_up {
        $lei->{lss} = $lss; # for LeiOverview->new
        my $lxs = $lei->lxs_prepare or return;
        $lei->ale->refresh_externals($lxs);
-       $lei->{opt}->{save} = -1;
        $lei->_start_query;
 }
 
+sub up1_redispatch {
+       my ($lei, $out, $op_p) = @_;
+       my $l = bless { %$lei }, ref($lei);
+       $l->{opt} = { %{$l->{opt}} };
+       delete $l->{sock};
+       $l->{''} = $op_p; # daemon only
+       eval {
+               $l->qerr("# updating $out");
+               up1($l, $out);
+               $l->qerr("# $out done");
+       };
+       $l->err($@) if $@;
+}
+
+sub lei_up {
+       my ($lei, $out) = @_;
+       $lei->{lse} = $lei->_lei_store(1)->search;
+       my $opt = $lei->{opt};
+       $opt->{save} = -1;
+       if (defined $opt->{all}) {
+               length($opt->{mua}//'') and return
+                       $lei->fail('--all and --mua= are incompatible');
+
+               # supporting IMAP outputs is more involved due to
+               # git-credential prompts.  TODO: add this in 1.8
+               $opt->{all} eq 'local' or return
+                       $lei->fail('only --all=local works at the moment');
+               my @all = PublicInbox::LeiSavedSearch::list($lei);
+               my @local = grep(!m!\Aimaps?://!i, @all);
+               $lei->_lei_store->write_prepare($lei); # share early
+               if ($lei->{oneshot}) { # synchronous
+                       up1_redispatch($lei, $_) for @local;
+               } else {
+                       # daemon mode, re-dispatch into our event loop w/o
+                       # creating an extra fork-level
+                       require PublicInbox::DS;
+                       require PublicInbox::PktOp;
+                       my ($op_c, $op_p) = PublicInbox::PktOp->pair;
+                       for my $o (@local) {
+                               PublicInbox::DS::requeue(sub {
+                                       up1_redispatch($lei, $o, $op_p);
+                               });
+                       }
+                       $lei->event_step_init;
+                       $op_c->{ops} = { '' => [$lei->can('dclose'), $lei] };
+               }
+       } else {
+               up1($lei, $out);
+       }
+}
+
 sub _complete_up {
        my ($lei, @argv) = @_;
        my ($cur, $re) = $lei->complete_url_common(\@argv);
index c0c7458164101a84d5321aed6cc32bb4839735fd..5834217113e5ed1c1291d61da0c5b8c7121c3050 100644 (file)
@@ -108,5 +108,7 @@ test_lei(sub {
        $mb = do { local $/; <$mb> };
        like($mb, qr/pre-existing/, 'pre-existing message preserved w/ -a');
        like($mb, qr/<qp\@example\.com>/, 'new result written w/ -a');
+
+       lei_ok(qw(up --all=local));
 });
 done_testing;