]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Watch.pm
watch: comments and tiny cleanups
[public-inbox.git] / lib / PublicInbox / Watch.pm
index 0bb92d0a9055703233b7af5cafe53bdfa0232e2f..db8d03964cddd1c4055ea6682ba70b5138c37031 100644 (file)
@@ -268,7 +268,7 @@ sub watch_fs_init ($) {
                delete $self->{done_timer};
                _done_for_now($self);
        };
-       my $cb = sub {
+       my $cb = sub { # called by PublicInbox::DirIdle::event_step
                _try_path($self, $_[0]->fullname);
                $self->{done_timer} //= PublicInbox::DS::requeue($done);
        };
@@ -411,7 +411,7 @@ sub imap_import_msg ($$$$$) {
        if (ref($inboxes)) {
                for my $ibx (@$inboxes) {
                        my $eml = PublicInbox::Eml->new($$raw);
-                       my $x = import_eml($self, $ibx, $eml);
+                       import_eml($self, $ibx, $eml);
                }
        } elsif ($inboxes eq 'watchspam') {
                # we don't remove unseen messages
@@ -566,7 +566,7 @@ sub watch_imap_idle_1 ($$$) {
                        $err = imap_fetch_all($self, $mic, $url);
                        $err //= imap_idle_once($self, $mic, $intvl, $url);
                } else {
-                       $err = "not connected: $!";
+                       $err = "E: not connected: $!";
                }
                if ($err && !$self->{quit}) {
                        warn $err, "\n";
@@ -589,6 +589,7 @@ sub watch_atfork_child ($) {
 sub watch_atfork_parent ($) {
        my ($self) = @_;
        _done_for_now($self);
+       PublicInbox::Sigfd::block_signals();
 }
 
 sub imap_idle_requeue ($) { # DS::add_timer callback
@@ -628,10 +629,14 @@ sub event_step {
        return if $self->{quit};
        my $idle_todo = $self->{idle_todo};
        if ($idle_todo && @$idle_todo) {
-               watch_atfork_parent($self);
-               while (my $url_intvl = shift(@$idle_todo)) {
-                       imap_idle_fork($self, $url_intvl);
-               }
+               my $oldset = watch_atfork_parent($self);
+               eval {
+                       while (my $url_intvl = shift(@$idle_todo)) {
+                               imap_idle_fork($self, $url_intvl);
+                       }
+               };
+               PublicInbox::Sigfd::sig_setmask($oldset);
+               die $@ if $@;
        }
        goto(&fs_scan_step) if $self->{mdre};
 }
@@ -684,9 +689,9 @@ sub watch_nntp_fetch_all ($$) {
 sub poll_fetch_fork ($) { # DS::add_timer callback
        my ($self, $intvl, $urls) = @{$_[0]};
        return if $self->{quit};
-       watch_atfork_parent($self);
-       defined(my $pid = fork) or die "fork: $!";
-       if ($pid == 0) {
+       my $oldset = watch_atfork_parent($self);
+       my $pid = fork;
+       if (defined($pid) && $pid == 0) {
                watch_atfork_child($self);
                if ($urls->[0] =~ m!\Aimaps?://!i) {
                        watch_imap_fetch_all($self, $urls);
@@ -695,6 +700,8 @@ sub poll_fetch_fork ($) { # DS::add_timer callback
                }
                _exit(0);
        }
+       PublicInbox::Sigfd::sig_setmask($oldset);
+       die "fork: $!"  unless defined $pid;
        $self->{poll_pids}->{$pid} = [ $intvl, $urls ];
        PublicInbox::DS::dwaitpid($pid, \&poll_fetch_reap, $self);
 }
@@ -977,7 +984,7 @@ sub watch_nntp_init ($$) {
        }
 }
 
-sub watch {
+sub watch { # main entry point
        my ($self, $sig, $oldset) = @_;
        $self->{oldset} = $oldset;
        $self->{sig} = $sig;
@@ -991,7 +998,7 @@ sub watch {
        }
        watch_fs_init($self) if $self->{mdre};
        PublicInbox::DS->SetPostLoopCallback(sub { !$self->quit_done });
-       PublicInbox::DS->EventLoop;
+       PublicInbox::DS->EventLoop; # calls ->event_step
        _done_for_now($self);
 }
 
@@ -1076,7 +1083,7 @@ sub content_exists ($$) {
 
 sub _spamcheck_cb {
        my ($sc) = @_;
-       sub {
+       sub { # this gets called by (V2Writable||Import)->add
                my ($mime, $ibx) = @_;
                return if content_exists($ibx, $mime);
                my $tmp = '';