1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
5 # pi_config: PublicInbox::Config ref
6 # inot: Linux::Inotify2-like object
7 # pathmap => { inboxdir => [ ibx, watch1, watch2, watch3... ] } mapping
8 package PublicInbox::InboxIdle;
10 use parent qw(PublicInbox::DS);
12 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
13 my $IN_MODIFY = 0x02; # match Linux inotify
15 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
16 $IN_MODIFY = Linux::Inotify2::IN_MODIFY();
17 $ino_cls = 'Linux::Inotify2';
18 } elsif (eval { require PublicInbox::KQNotify }) {
19 $IN_MODIFY = PublicInbox::KQNotify::NOTE_WRITE();
20 $ino_cls = 'PublicInbox::KQNotify';
22 require PublicInbox::In2Tie if $ino_cls;
24 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
25 my ($ibx, $self) = @_;
26 my $dir = abs_path($ibx->{inboxdir});
28 warn "W: $ibx->{inboxdir} not watched: $!\n";
31 my $inot = $self->{inot};
32 my $cur = $self->{pathmap}->{$dir} //= [];
34 # transfer old subscriptions to the current inbox, cancel the old watch
35 if (my $old_ibx = $cur->[0]) {
36 $ibx->{unlock_subs} and
37 die "BUG: $dir->{unlock_subs} should not exist";
38 $ibx->{unlock_subs} = $old_ibx->{unlock_subs};
43 my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
44 $cur->[1] = $inot->watch($lock, $IN_MODIFY, sub { $ibx->on_unlock });
46 # TODO: detect deleted packs (and possibly other files)
50 my ($self, $pi_config) = @_;
51 $pi_config->each_inbox(\&in2_arm, $self);
55 my ($class, $pi_config) = @_;
56 my $self = bless {}, $class;
59 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
60 my $io = PublicInbox::In2Tie::io($inot);
61 $self->SUPER::new($io, EPOLLIN | EPOLLET);
63 require PublicInbox::FakeInotify;
64 $inot = PublicInbox::FakeInotify->new;
66 $self->{inot} = $inot;
67 $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
68 refresh($self, $pi_config);
74 eval { $self->{inot}->poll }; # Linux::Inotify2::poll
75 warn "$self->{inot}->poll err: $@\n" if $@;
78 # for graceful shutdown in PublicInbox::Daemon,
79 # just ensure the FD gets closed ASAP and subscribers