]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WatchMaildir.pm
watch: commit changes to fast-import sooner
[public-inbox.git] / lib / PublicInbox / WatchMaildir.pm
index 0e2a6d2c3c5d6f6ea50580b5ad8e25eb9314278f..8e10e43f056c677d4deb6e116a71692a55eed704 100644 (file)
@@ -91,13 +91,18 @@ sub new {
 
 sub _done_for_now {
        my ($self) = @_;
+       my $importers = $self->{importers};
+       foreach my $im (values %$importers) {
+               $im->done if $im->{nchg};
+       }
+
        my $opendirs = $self->{opendirs};
 
        # spamdir scanning means every importer remains open
        my $spamdir = $self->{spamdir};
        return if defined($spamdir) && $opendirs->{$spamdir};
 
-       foreach my $im (values %{$self->{importers}}) {
+       foreach my $im (values %$importers) {
                # not done if we're scanning
                next if $opendirs->{$im->{git}->{git_dir}};
                $im->done;
@@ -201,7 +206,7 @@ sub _try_path {
        $im->add($mime, $self->{spamcheck});
 }
 
-sub quit { $_[0]->{quit} = 1 }
+sub quit { trigger_scan($_[0], 'quit') }
 
 sub watch {
        my ($self) = @_;
@@ -215,7 +220,7 @@ sub watch {
        # in the future...
        require Filesys::Notify::Simple;
        my $fsn = Filesys::Notify::Simple->new([@{$self->{mdir}}, $scandir]);
-       $fsn->wait($cb) until ($self->{quit});
+       $fsn->wait($cb) until $self->{quit};
 }
 
 sub trigger_scan {
@@ -227,6 +232,14 @@ sub trigger_scan {
 
 sub scan {
        my ($self, $path) = @_;
+       if ($path =~ /quit\z/) {
+               %{$self->{opendirs}} = ();
+               _done_for_now($self);
+               $self->{quit} = 1;
+               return;
+       }
+       # else: $path =~ /(cont|full)\z/
+       return if $self->{quit};
        my $max = 10;
        my $opendirs = $self->{opendirs};
        my @dirnames = keys %$opendirs;
@@ -255,11 +268,9 @@ sub scan {
                        $opendirs->{$dir} = $dh if $n < 0;
                }
        }
-       if (keys %$opendirs) { # do we have more work to do?
-               trigger_scan($self, 'cont');
-       } else {
-               _done_for_now($self);
-       }
+       _done_for_now($self);
+       # do we have more work to do?
+       trigger_scan($self, 'cont') if keys %$opendirs;
 }
 
 sub _path_to_mime {