]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-watch
20534bf2a3ec196e36cdd3c9d2bb0c6136fba849
[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 IO::Handle;
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 STDOUT->autoflush(1);
13 STDERR->autoflush(1);
14 my ($config, $watch_md);
15 my $reload = sub {
16         $config = PublicInbox::Config->new;
17         $watch_md->quit if $watch_md;
18         $watch_md = PublicInbox::WatchMaildir->new($config);
19 };
20 $reload->();
21 if ($watch_md) {
22         my $scan = sub { $watch_md->trigger_scan('full') if $watch_md };
23         my $quit = sub {
24                 $watch_md->quit if $watch_md;
25                 $watch_md = undef;
26         };
27         my $sig = {
28                 HUP => $reload,
29                 USR1 => $scan,
30                 CHLD => \&PublicInbox::DS::enqueue_reap,
31         };
32         $sig->{QUIT} = $sig->{TERM} = $sig->{INT} = $quit;
33
34         # --no-scan is only intended for testing atm, undocumented.
35         unless (grep(/\A--no-scan\z/, @ARGV)) {
36                 PublicInbox::DS::requeue($scan);
37         }
38         my $sigfd = PublicInbox::Sigfd->new($sig, $SFD_NONBLOCK);
39         local %SIG = (%SIG, %$sig) if !$sigfd;
40         if (!$sigfd) {
41                 PublicInbox::Sigfd::set_sigmask($oldset);
42                 PublicInbox::DS->SetLoopTimeout(1000);
43         }
44         $watch_md->watch($sig, $oldset) while ($watch_md);
45 }