]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxIdle.pm
rename {pi_config} fields to {pi_cfg}
[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 # fields:
5 # inot: Linux::Inotify2-like object
6 # pathmap => { inboxdir => [ ibx, watch1, watch2, watch3... ] } mapping
7 package PublicInbox::InboxIdle;
8 use strict;
9 use parent qw(PublicInbox::DS);
10 use Cwd qw(abs_path);
11 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
12 my $IN_MODIFY = 0x02; # match Linux inotify
13 my $ino_cls;
14 if ($^O eq 'linux' && eval { require Linux::Inotify2; 1 }) {
15         $IN_MODIFY = Linux::Inotify2::IN_MODIFY();
16         $ino_cls = 'Linux::Inotify2';
17 } elsif (eval { require PublicInbox::KQNotify }) {
18         $IN_MODIFY = PublicInbox::KQNotify::NOTE_WRITE();
19         $ino_cls = 'PublicInbox::KQNotify';
20 }
21 require PublicInbox::In2Tie if $ino_cls;
22
23 sub in2_arm ($$) { # PublicInbox::Config::each_inbox callback
24         my ($ibx, $self) = @_;
25         my $dir = abs_path($ibx->{inboxdir});
26         if (!defined($dir)) {
27                 warn "W: $ibx->{inboxdir} not watched: $!\n";
28                 return;
29         }
30         my $inot = $self->{inot};
31         my $cur = $self->{pathmap}->{$dir} //= [];
32
33         # transfer old subscriptions to the current inbox, cancel the old watch
34         if (my $old_ibx = $cur->[0]) {
35                 $ibx->{unlock_subs} and
36                         die "BUG: $dir->{unlock_subs} should not exist";
37                 $ibx->{unlock_subs} = $old_ibx->{unlock_subs};
38                 $cur->[1]->cancel; # Linux::Inotify2::Watch::cancel
39         }
40         $cur->[0] = $ibx;
41
42         my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
43         if (my $w = $cur->[1] = $inot->watch($lock, $IN_MODIFY)) {
44                 $self->{on_unlock}->{$w->name} = $ibx;
45         } else {
46                 warn "E: ".ref($inot)."->watch($lock, IN_MODIFY) failed: $!\n";
47         }
48
49         # TODO: detect deleted packs (and possibly other files)
50 }
51
52 sub refresh {
53         my ($self, $pi_cfg) = @_;
54         $pi_cfg->each_inbox(\&in2_arm, $self);
55 }
56
57 sub new {
58         my ($class, $pi_cfg) = @_;
59         my $self = bless {}, $class;
60         my $inot;
61         if ($ino_cls) {
62                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
63                 my $io = PublicInbox::In2Tie::io($inot);
64                 $self->SUPER::new($io, EPOLLIN | EPOLLET);
65         } else {
66                 require PublicInbox::FakeInotify;
67                 $inot = PublicInbox::FakeInotify->new;
68         }
69         $self->{inot} = $inot;
70         $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
71         $self->{on_unlock} = {}; # lock path => ibx
72         refresh($self, $pi_cfg);
73         PublicInbox::FakeInotify::poll_once($self) if !$ino_cls;
74         $self;
75 }
76
77 sub event_step {
78         my ($self) = @_;
79         eval {
80                 my @events = $self->{inot}->read; # Linux::Inotify2::read
81                 my $on_unlock = $self->{on_unlock};
82                 for my $ev (@events) {
83                         if (my $ibx = $on_unlock->{$ev->fullname}) {
84                                 $ibx->on_unlock;
85                         }
86                 }
87         };
88         warn "{inot}->read err: $@\n" if $@;
89 }
90
91 # for graceful shutdown in PublicInbox::Daemon,
92 # just ensure the FD gets closed ASAP and subscribers
93 sub busy { 0 }
94
95 1;