]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-watch
watch: log signal activities to STDERR
[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::Watch;
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 local $0 = $0; # local since this script may be eval-ed
15 my $watch = PublicInbox::Watch->new(PublicInbox::Config->new);
16 my $reload = sub {
17         my $prev = $watch or return; # SIGQUIT issued
18         $watch->quit;
19         $watch = PublicInbox::Watch->new(PublicInbox::Config->new);
20         if ($watch) {
21                 warn("I: reloaded\n");
22         } else {
23                 warn("E: reloading failed\n");
24                 $watch = $prev;
25         }
26 };
27
28 if ($watch) {
29         my $scan = sub {
30                 return if !$watch;
31                 warn "I: scanning\n";
32                 $watch->trigger_scan('full');
33         };
34         my $quit = sub {
35                 $watch->quit if $watch;
36                 $watch = undef;
37                 $0 .= ' quitting';
38         };
39         my $sig = {
40                 HUP => $reload,
41                 USR1 => $scan,
42                 CHLD => \&PublicInbox::DS::enqueue_reap,
43         };
44         $sig->{QUIT} = $sig->{TERM} = $sig->{INT} = $quit;
45
46         # --no-scan is only intended for testing atm, undocumented.
47         unless (grep(/\A--no-scan\z/, @ARGV)) {
48                 PublicInbox::DS::requeue($scan);
49         }
50         my $sigfd = PublicInbox::Sigfd->new($sig, $SFD_NONBLOCK);
51         local %SIG = (%SIG, %$sig) if !$sigfd;
52         if (!$sigfd) {
53                 PublicInbox::Sigfd::set_sigmask($oldset);
54                 PublicInbox::DS->SetLoopTimeout(1000);
55         }
56         $watch->watch($sig, $oldset) while ($watch);
57 }