]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-watch
watch: use signalfd for Maildir watching
[public-inbox.git] / script / public-inbox-watch
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016-2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 use strict;
5 use warnings;
6 use PublicInbox::WatchMaildir;
7 use PublicInbox::Config;
8 use PublicInbox::DS;
9 use PublicInbox::Sigfd;
10 use PublicInbox::Syscall qw(SFD_NONBLOCK);
11 my $oldset = PublicInbox::Sigfd::block_signals();
12 my ($config, $watch_md);
13 my $reload = sub {
14         $config = PublicInbox::Config->new;
15         $watch_md->quit if $watch_md;
16         $watch_md = PublicInbox::WatchMaildir->new($config);
17 };
18 $reload->();
19 if ($watch_md) {
20         my $scan = sub { $watch_md->trigger_scan('full') if $watch_md };
21         my $quit = sub {
22                 $watch_md->quit if $watch_md;
23                 $watch_md = undef;
24         };
25         my $sig = { HUP => $reload, USR1 => $scan };
26         $sig->{QUIT} = $sig->{TERM} = $sig->{INT} = $quit;
27
28         # --no-scan is only intended for testing atm, undocumented.
29         unless (grep(/\A--no-scan\z/, @ARGV)) {
30                 PublicInbox::DS::requeue($scan);
31         }
32         my $sigfd = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
33         local %SIG = (%SIG, %$sig) if !$sigfd;
34         if (!$sigfd) {
35                 PublicInbox::Sigfd::set_sigmask($oldset);
36                 PublicInbox::DS->SetLoopTimeout(1000);
37         }
38         $watch_md->watch($sig, $oldset) while ($watch_md);
39 }