]> Sergey Matveev's repositories - public-inbox.git/blob - script/public-inbox-watch
lei: give workers their own process group
[public-inbox.git] / script / public-inbox-watch
1 #!/usr/bin/perl -w
2 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 my $help = <<EOF;
5 usage: public-inbox-watch
6
7 See public-inbox-watch(1) man page for full documentation.
8 EOF
9
10 use strict;
11 use Getopt::Long qw(:config gnu_getopt no_ignore_case auto_abbrev);
12 use IO::Handle; # ->autoflush
13 use PublicInbox::Watch;
14 use PublicInbox::Config;
15 use PublicInbox::DS;
16 my $do_scan = 1;
17 GetOptions('scan!' => \$do_scan, # undocumented, testing only
18         'help|h' => \(my $show_help)) or do { print STDERR $help; exit 1 };
19 if ($show_help) { print $help; exit 0 };
20 my $oldset = PublicInbox::DS::block_signals();
21 STDOUT->autoflush(1);
22 STDERR->autoflush(1);
23 local $0 = $0; # local since this script may be eval-ed
24 my $watch = PublicInbox::Watch->new(PublicInbox::Config->new);
25 my $reload = sub {
26         my $prev = $watch or return; # SIGQUIT issued
27         $watch->quit;
28         $watch = PublicInbox::Watch->new(PublicInbox::Config->new);
29         if ($watch) {
30                 warn("I: reloaded\n");
31         } else {
32                 warn("E: reloading failed\n");
33                 $watch = $prev;
34         }
35 };
36
37 if ($watch) {
38         my $scan = sub {
39                 return if !$watch;
40                 warn "I: scanning\n";
41                 $watch->trigger_scan('full');
42         };
43         my $quit = sub {
44                 $watch->quit if $watch;
45                 $watch = undef;
46                 $0 .= ' quitting';
47         };
48         my $sig = {
49                 HUP => $reload,
50                 USR1 => $scan,
51                 CHLD => \&PublicInbox::DS::enqueue_reap,
52         };
53         $sig->{QUIT} = $sig->{TERM} = $sig->{INT} = $quit;
54
55         # --no-scan is only intended for testing atm, undocumented.
56         PublicInbox::DS::requeue($scan) if $do_scan;
57         $watch->watch($sig, $oldset) while ($watch);
58 }