]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-watch
ae7b70be355f056a0201f51a66bb089f2562371d
[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 = {
26                 HUP => $reload,
27                 USR1 => $scan,
28                 CHLD => \&PublicInbox::DS::enqueue_reap,
29         };
30         $sig->{QUIT} = $sig->{TERM} = $sig->{INT} = $quit;
31
32         # --no-scan is only intended for testing atm, undocumented.
33         unless (grep(/\A--no-scan\z/, @ARGV)) {
34                 PublicInbox::DS::requeue($scan);
35         }
36         my $sigfd = PublicInbox::Sigfd->new($sig, SFD_NONBLOCK);
37         local %SIG = (%SIG, %$sig) if !$sigfd;
38         if (!$sigfd) {
39                 PublicInbox::Sigfd::set_sigmask($oldset);
40                 PublicInbox::DS->SetLoopTimeout(1000);
41         }
42         $watch_md->watch($sig, $oldset) while ($watch_md);
43 }