]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxIdle.pm
97e9d53250eb43285ae7eba56c3f4ae79d898d8e
[public-inbox.git] / lib / PublicInbox / InboxIdle.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 package PublicInbox::InboxIdle;
5 use strict;
6 use base qw(PublicInbox::DS);
7 use fields qw(pi_config inot pathmap);
8 use Cwd qw(abs_path);
9 use Symbol qw(gensym);
10 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
11 my $IN_MODIFY = 0x02; # match Linux inotify
12 my $ino_cls;
13 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
14         $IN_MODIFY = Linux::Inotify2::IN_MODIFY();
15         $ino_cls = 'Linux::Inotify2';
16 } elsif (eval { require PublicInbox::KQNotify }) {
17         $IN_MODIFY = PublicInbox::KQNotify::NOTE_WRITE();
18         $ino_cls = 'PublicInbox::KQNotify';
19 }
20 require PublicInbox::In2Tie if $ino_cls;
21
22 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
23         my ($ibx, $self) = @_;
24         my $dir = abs_path($ibx->{inboxdir});
25         if (!defined($dir)) {
26                 warn "W: $ibx->{inboxdir} not watched: $!\n";
27                 return;
28         }
29         my $inot = $self->{inot};
30         my $cur = $self->{pathmap}->{$dir} //= [];
31
32         # transfer old subscriptions to the current inbox, cancel the old watch
33         if (my $old_ibx = $cur->[0]) {
34                 $ibx->{unlock_subs} and
35                         die "BUG: $dir->{unlock_subs} should not exist";
36                 $ibx->{unlock_subs} = $old_ibx->{unlock_subs};
37                 $cur->[1]->cancel;
38         }
39         $cur->[0] = $ibx;
40
41         my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
42         $cur->[1] = $inot->watch($lock, $IN_MODIFY, sub { $ibx->on_unlock });
43
44         # TODO: detect deleted packs (and possibly other files)
45 }
46
47 sub refresh {
48         my ($self, $pi_config) = @_;
49         $pi_config->each_inbox(\&in2_arm, $self);
50 }
51
52 sub new {
53         my ($class, $pi_config) = @_;
54         my $self = fields::new($class);
55         my $inot;
56         if ($ino_cls) {
57                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
58                 my $sock = gensym;
59                 tie *$sock, 'PublicInbox::In2Tie', $inot;
60                 $inot->blocking(0);
61                 if ($inot->can('on_overflow')) {
62                          # broadcasts everything on overflow
63                         $inot->on_overflow(undef);
64                 }
65                 $self->SUPER::new($sock, EPOLLIN | EPOLLET);
66         } else {
67                 require PublicInbox::FakeInotify;
68                 $inot = PublicInbox::FakeInotify->new;
69         }
70         $self->{inot} = $inot;
71         $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
72         refresh($self, $pi_config);
73         $self;
74 }
75
76 sub event_step {
77         my ($self) = @_;
78         eval { $self->{inot}->poll }; # Linux::Inotify2::poll
79         warn "$self->{inot}->poll err: $@\n" if $@;
80 }
81
82 # for graceful shutdown in PublicInbox::Daemon,
83 # just ensure the FD gets closed ASAP and subscribers
84 sub busy { 0 }
85
86 1;