1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 package PublicInbox::InboxIdle;
6 use base qw(PublicInbox::DS);
7 use fields qw(pi_config inot pathmap);
10 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
11 my $IN_CLOSE = 0x08 | 0x10; # match Linux inotify
13 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
14 $IN_CLOSE = Linux::Inotify2::IN_CLOSE();
15 $ino_cls = 'Linux::Inotify2';
16 } elsif (eval { require PublicInbox::KQNotify }) {
17 $IN_CLOSE = PublicInbox::KQNotify::IN_CLOSE();
18 $ino_cls = 'PublicInbox::KQNotify';
20 require PublicInbox::In2Tie if $ino_cls;
22 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
23 my ($ibx, $self) = @_;
24 my $dir = abs_path($ibx->{inboxdir});
26 warn "W: $ibx->{inboxdir} not watched: $!\n";
29 my $inot = $self->{inot};
30 my $cur = $self->{pathmap}->{$dir} //= [];
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};
41 my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
42 $cur->[1] = $inot->watch($lock, $IN_CLOSE, sub { $ibx->on_unlock });
44 # TODO: detect deleted packs (and possibly other files)
48 my ($self, $pi_config) = @_;
49 $pi_config->each_inbox(\&in2_arm, $self);
53 my ($class, $pi_config) = @_;
54 my $self = fields::new($class);
57 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
59 tie *$sock, 'PublicInbox::In2Tie', $inot;
61 $inot->on_overflow(undef); # broadcasts everything on overflow
62 $self->SUPER::new($sock, EPOLLIN | EPOLLET);
64 require PublicInbox::FakeInotify;
65 $inot = PublicInbox::FakeInotify->new;
67 $self->{inot} = $inot;
68 $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
69 refresh($self, $pi_config);
75 eval { $self->{inot}->poll }; # Linux::Inotify2::poll
76 warn "$self->{inot}->poll err: $@\n" if $@;