]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LEI.pm
lei_mail_sync: rely on flock(2), avoid IPC
[public-inbox.git] / lib / PublicInbox / LEI.pm
index 0a30bc36d96baf5b367252779143952ea6b4dd75..053b6174d808514f04227b32f3bef982e72b3498 100644 (file)
@@ -24,6 +24,8 @@ use PublicInbox::DS qw(now dwaitpid);
 use PublicInbox::Spawn qw(spawn popen_rd);
 use PublicInbox::Lock;
 use PublicInbox::Eml;
+use PublicInbox::Import;
+use PublicInbox::ContentHash qw(git_sha);
 use Time::HiRes qw(stat); # ctime comparisons for config cache
 use File::Path qw(mkpath);
 use File::Spec;
@@ -256,12 +258,12 @@ our %CMD = ( # sorted in order of importance/use:
         @c_opt ],
 'import' => [ 'LOCATION...|--stdin',
        'one-time import/update from URL or filesystem',
-       qw(stdin| offset=i recursive|r exclude=s include|I=s jobs=s new-only
+       qw(stdin| offset=i recursive|r exclude=s include|I=s new-only
        lock=s@ in-format|F=s kw! verbose|v+ incremental! mail-sync!),
        @net_opt, @c_opt ],
 'forget-mail-sync' => [ 'LOCATION...',
        'forget sync information for a mail folder', @c_opt ],
-'prune-mail-sync' => [ 'LOCATION...|--all',
+'refresh-mail-sync' => [ 'LOCATION...|--all',
        'prune dangling sync data for a mail folder', 'all:s', @c_opt ],
 'export-kw' => [ 'LOCATION...|--all',
        'one-time export of keywords of sync sources',
@@ -614,6 +616,7 @@ sub pkt_ops {
        $ops->{x_it} = [ \&x_it, $lei ];
        $ops->{child_error} = [ \&child_error, $lei ];
        $ops->{incr} = [ \&incr, $lei ];
+       $ops->{sto_done_request} = [ \&sto_done_request, $lei, $lei->{sock} ];
        $ops;
 }
 
@@ -624,6 +627,7 @@ sub workers_start {
        my $end = $lei->pkt_op_pair;
        my $ident = $wq->{-wq_ident} // "lei-$lei->{cmd} worker";
        $flds->{lei} = $lei;
+       $wq->{-wq_nr_workers} //= $jobs; # lock, no incrementing
        $wq->wq_workers_start($ident, $jobs, $lei->oldset, $flds);
        delete $lei->{pkt_op_p};
        my $op_c = delete $lei->{pkt_op_c};
@@ -1438,20 +1442,16 @@ sub refresh_watches {
        }
 
        # add all known Maildir folders as implicit watches
-       my $sto = $lei->_lei_store;
-       my $renames = 0;
-       if (my $lms = $sto ? $sto->search->lms : undef) {
+       my $lms = $lei->lms;
+       if ($lms) {
+               $lms->lms_write_prepare;
                for my $d ($lms->folders('maildir:')) {
                        substr($d, 0, length('maildir:')) = '';
-                       my $cd = canonpath_harder($d);
-                       my $f = "maildir:$cd";
 
                        # fixup old bugs while we're iterating:
-                       if ($d ne $cd) {
-                               $sto->ipc_do('lms_rename_folder',
-                                               "maildir:$d", $f);
-                               ++$renames;
-                       }
+                       my $cd = canonpath_harder($d);
+                       my $f = "maildir:$cd";
+                       $lms->rename_folder("maildir:$d", $f) if $d ne $cd;
                        next if $watches->{$f}; # may be set to pause
                        require PublicInbox::LeiWatch;
                        $watches->{$f} = PublicInbox::LeiWatch->new($f);
@@ -1459,7 +1459,6 @@ sub refresh_watches {
                        add_maildir_watch($cd, $cfg_f);
                }
        }
-       $lei->sto_done_request if $renames;
        if ($old) { # cull old non-existent entries
                for my $url (keys %$old) {
                        next if exists $seen{$url};
@@ -1479,18 +1478,19 @@ sub refresh_watches {
        }
 }
 
-sub git_blob_id {
-       my ($lei, $eml) = @_;
-       ($lei->{sto} // _lei_store($lei, 1))->git_blob_id($eml);
+# TODO: support SHA-256
+sub git_oid {
+       my $eml = $_[-1];
+       $eml->header_set($_) for @PublicInbox::Import::UNWANTED_HEADERS;
+       git_sha(1, $eml);
 }
 
-sub lms { # read-only LeiMailSync
-       my ($lei) = @_;
-       my $lse = $lei->{lse} // do {
-               my $sto = $lei->{sto} // _lei_store($lei);
-               $sto ? $sto->search : undef
-       };
-       $lse ? $lse->lms : undef;
+sub lms {
+       my ($lei, $rw) = @_;
+       my $sto = $lei->{sto} // _lei_store($lei) // return;
+       require PublicInbox::LeiMailSync;
+       my $f = "$sto->{priv_eidx}->{topdir}/mail_sync.sqlite3";
+       (-f $f || $rw) ? PublicInbox::LeiMailSync->new($f) : undef;
 }
 
 sub sto_done_request { # only call this from lei-daemon process (not workers)