]> Sergey Matveev's repositories - public-inbox.git/commitdiff
lei: simplify common LeiInput users with ->wq1_start
authorEric Wong <e@80x24.org>
Tue, 2 Nov 2021 18:14:44 +0000 (18:14 +0000)
committerEric Wong <e@80x24.org>
Tue, 2 Nov 2021 19:06:51 +0000 (19:06 +0000)
This method replaces a common pattern of starting workers,
preparing internal auth ops, and asynchronous waiting of
command completion.

It also adds missing LeiAuth support to rediff and rm
which rarely need auth.

12 files changed:
lib/PublicInbox/LEI.pm
lib/PublicInbox/LeiExportKw.pm
lib/PublicInbox/LeiForgetSearch.pm
lib/PublicInbox/LeiImport.pm
lib/PublicInbox/LeiLsMailSource.pm
lib/PublicInbox/LeiMailDiff.pm
lib/PublicInbox/LeiP2q.pm
lib/PublicInbox/LeiRediff.pm
lib/PublicInbox/LeiRefreshMailSync.pm
lib/PublicInbox/LeiRm.pm
lib/PublicInbox/LeiTag.pm
lib/PublicInbox/LeiUp.pm

index 3e1706a0dd20258508666db347f85e75aee8d344..887025de5dedb608576b2263b4f20c0596fce8e5 100644 (file)
@@ -631,7 +631,10 @@ sub pkt_ops {
 
 sub workers_start {
        my ($lei, $wq, $jobs, $ops, $flds) = @_;
-       $ops = pkt_ops($lei, { ($ops ? %$ops : ()) });
+       $ops //= {};
+       ($wq->can('net_merge_all_done') && $lei->{auth}) and
+               $lei->{auth}->op_merge($ops, $wq, $lei);
+       pkt_ops($lei, $ops);
        $ops->{''} //= [ $wq->can('_lei_wq_eof') || \&wq_eof, $lei ];
        my $end = $lei->pkt_op_pair;
        my $ident = $wq->{-wq_ident} // "lei-$lei->{cmd} worker";
@@ -648,12 +651,22 @@ sub workers_start {
 # call this when we're ready to wait on events and yield to other clients
 sub wait_wq_events {
        my ($lei, $op_c, $ops) = @_;
+       my $wq1 = $lei->{wq1};
+       ($wq1 && $wq1->can('net_merge_all_done') && !$lei->{auth}) and
+               $wq1->net_merge_all_done;
        for my $wq (grep(defined, @$lei{qw(ikw pmd)})) { # auxiliary WQs
                $wq->wq_close;
        }
        $op_c->{ops} = $ops;
 }
 
+sub wq1_start {
+       my ($lei, $wq, $jobs) = @_;
+       my ($op_c, $ops) = workers_start($lei, $wq, $jobs // 1);
+       $lei->{wq1} = $wq;
+       wait_wq_events($lei, $op_c, $ops); # net_merge_all_done if !{auth}
+}
+
 sub _help {
        require PublicInbox::LeiHelp;
        PublicInbox::LeiHelp::call($_[0], $_[1], \%CMD, \%OPTDESC);
index 0ecfb7829772b68875c579c6c9901ba1d34b2610..d2396fa7d697bb7b8b9ddf092ff27de65230f350 100644 (file)
@@ -124,13 +124,8 @@ EOM
                                        'imap_add_kw' : 'imap_set_kw');
                $self->{nwr}->{-skip_creat} = 1;
        }
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
+       $lei->wq1_start($self);
 }
 
 sub _complete_export_kw {
index dfeb029322abd4fbd0e6d32b8f6f04c5620529b3..dd358ae16a3a086c90b8b0dae8a6b00670149247 100644 (file)
@@ -45,12 +45,7 @@ sub lei_forget_search {
        if ($self->{o_remote}) { # setup lei->{auth}
                $self->prepare_inputs($lei, $self->{o_remote}) or return;
        }
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 sub do_prune {
index d8f39fdf171bfcd99ac507b02acc0887fec12b96..bbc0634e820e653f21dd35bacdca19817000d24f 100644 (file)
@@ -102,14 +102,9 @@ sub do_import_index ($$@) {
        }
        ($lei->{opt}->{'new-only'} && (!$net || !$net->{imap_order})) and
                warn "# --new-only is only for IMAP\n";
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
        $lei->{-eml_noisy} = 1;
-       (my $op_c, $ops) = $lei->workers_start($self, $j, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self, $j);
 }
 
 sub lei_import { # the main "lei import" method
index 5eb7032d78b7252e47dd861c76f1e48ad14b7dad..50799270a8de20959381544e3d01495cb5fd3707 100644 (file)
@@ -95,13 +95,8 @@ sub lei_ls_mail_source {
                $json->pretty(1)->indent(2) if $isatty || $lei->{opt}->{pretty};
        }
        $lei->start_pager if $isatty;
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei);
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
+       $lei->wq1_start($self);
 }
 
 sub _complete_ls_mail_source {
index 48ba74cfe886f1811aa078670491ebce8f44eb09..2b4cfd9e102c9682b1093dc5ae9b25d2e1a9dfbb 100644 (file)
@@ -81,13 +81,8 @@ sub lei_mail_diff {
        my $isatty = -t $lei->{1};
        $lei->{opt}->{color} //= $isatty;
        $lei->start_pager if $isatty;
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 no warnings 'once';
index 09ec0a079bb904a972319b3f6a8592e4b79341c6..610adb78cb2a0b16edd4be71fd0fcb09a2f3a491 100644 (file)
@@ -183,12 +183,7 @@ sub lei_p2q { # the "lei patch-to-query" entry point
        $lei->{opt}->{'in-format'} //= 'eml' if $lei->{opt}->{stdin};
        my $self = bless { missing_ok => 1 }, __PACKAGE__;
        $self->prepare_inputs($lei, \@inputs) or return;
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 sub ipc_atfork_child {
index 56c457fc46933d83f95457d0329b9456037d6435..f0521bcc6b623ae54963809754a9e0ae9f67aeca 100644 (file)
@@ -279,10 +279,7 @@ sub lei_rediff {
        my $isatty = -t $lei->{1};
        $lei->{opt}->{color} //= $isatty;
        $lei->start_pager if $isatty;
-       my ($op_c, $ops) = $lei->workers_start($self, 1);
-       $lei->{wq1} = $self;
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 sub ipc_atfork_child {
index f516f5725f6eb17eeb7f888661b17b831633dd97..7821008fa38c5e80f6dae7d13b87cc0c552744e0 100644 (file)
@@ -81,13 +81,8 @@ EOM
        my $self = bless { missing_ok => 1, lms => $lms }, __PACKAGE__;
        $lei->{opt}->{'mail-sync'} = 1; # for prepare_inputs
        $self->prepare_inputs($lei, \@folders) or return;
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops); # net_merge_all_done if !{auth}
+       $lei->wq1_start($self);
 }
 
 sub ipc_atfork_child { # needed for PublicInbox::LeiPmdir
index cc1abbff66cb1e47a9fd0cd111d155059c10dffe..62423ac9828e7c2ec851f36de0161c08cd2fbaf3 100644 (file)
@@ -19,11 +19,8 @@ sub lei_rm {
        $lei->{opt}->{'in-format'} //= 'eml';
        my $self = bless {}, __PACKAGE__;
        $self->prepare_inputs($lei, \@inputs) or return;
-       my ($op_c, $ops) = $lei->workers_start($self, 1);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 no warnings 'once';
index d64a9f86e05ac69cc440874a39c88b1bd88465e7..2dc59f7023cac20d158a90880b8aa7d22c6620c8 100644 (file)
@@ -37,13 +37,8 @@ sub lei_tag { # the "lei tag" method
        $self->prepare_inputs($lei, \@argv) or return;
        grep(defined, @$vmd_mod{qw(+kw +L -L -kw)}) or
                return $lei->fail('no keywords or labels specified');
-       my $ops = {};
-       $lei->{auth}->op_merge($ops, $self, $lei) if $lei->{auth};
-       (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-       $lei->{wq1} = $self;
        $lei->{-err_type} = 'non-fatal';
-       net_merge_all_done($self) unless $lei->{auth};
-       $lei->wait_wq_events($op_c, $ops);
+       $lei->wq1_start($self);
 }
 
 sub note_unimported {
index 79639d5e62a49fe8f2eb43bfd3f454483e619e4f..66d950b2ac7b2a57a3e6c9c5afebef356b9d5471 100644 (file)
@@ -136,10 +136,7 @@ EOM
        if ($lei->{auth}) { # start auth worker
                require PublicInbox::NetWriter;
                bless $lei->{net}, 'PublicInbox::NetWriter';
-               $lei->{auth}->op_merge(my $ops = {}, $self, $lei);
-               (my $op_c, $ops) = $lei->workers_start($self, 1, $ops);
-               $lei->{wq1} = $self;
-               $lei->wait_wq_events($op_c, $ops);
+               $lei->wq1_start($self);
                # net_merge_all_done will fire when auth is done
        } else {
                redispatch_all($self, $lei); # see below