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);
9 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
10 my $IN_CLOSE = 0x08 | 0x10; # match Linux inotify
12 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
13 $IN_CLOSE = Linux::Inotify2::IN_CLOSE();
14 $ino_cls = 'Linux::Inotify2';
15 } elsif (eval { require PublicInbox::KQNotify }) {
16 $IN_CLOSE = PublicInbox::KQNotify::IN_CLOSE();
17 $ino_cls = 'PublicInbox::KQNotify';
19 require PublicInbox::In2Tie if $ino_cls;
21 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
22 my ($ibx, $inot) = @_;
23 my $path = "$ibx->{inboxdir}/";
24 $path .= $ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock';
25 $inot->watch($path, $IN_CLOSE, sub { $ibx->on_unlock });
26 # TODO: detect deleted packs (and possibly other files)
30 my ($class, $pi_config) = @_;
31 my $self = fields::new($class);
34 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
36 tie *$sock, 'PublicInbox::In2Tie', $inot;
38 $inot->on_overflow(undef); # broadcasts everything on overflow
39 $self->SUPER::new($sock, EPOLLIN | EPOLLET);
41 require PublicInbox::FakeInotify;
42 $inot = PublicInbox::FakeInotify->new;
44 $self->{inot} = $inot;
45 $pi_config->each_inbox(\&in2_arm, $inot);
51 eval { $self->{inot}->poll }; # Linux::Inotify2::poll
52 warn "$self->{inot}->poll err: $@\n" if $@;