]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/InboxIdle.pm
support multiple CODE_URLs
[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 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 = $ibx->{inboxdir};
25         my $inot = $self->{inot};
26         my $cur = $self->{pathmap}->{$dir} //= [];
27
28         # transfer old subscriptions to the current inbox, cancel the old watch
29         if (my $old_ibx = $cur->[0]) {
30                 $ibx->{unlock_subs} and
31                         die "BUG: $dir->{unlock_subs} should not exist";
32                 $ibx->{unlock_subs} = $old_ibx->{unlock_subs};
33                 $cur->[1]->cancel; # Linux::Inotify2::Watch::cancel
34         }
35         $cur->[0] = $ibx;
36
37         my $lock = "$dir/".($ibx->version >= 2 ? 'inbox.lock' : 'ssoma.lock');
38         if (my $w = $cur->[1] = $inot->watch($lock, $IN_MODIFY)) {
39                 $self->{on_unlock}->{$w->name} = $ibx;
40         } else {
41                 warn "E: ".ref($inot)."->watch($lock, IN_MODIFY) failed: $!\n";
42         }
43
44         # TODO: detect deleted packs (and possibly other files)
45 }
46
47 sub refresh {
48         my ($self, $pi_cfg) = @_;
49         $pi_cfg->each_inbox(\&in2_arm, $self);
50 }
51
52 sub new {
53         my ($class, $pi_cfg) = @_;
54         my $self = bless {}, $class;
55         my $inot;
56         if ($ino_cls) {
57                 $inot = $ino_cls->new or die "E: $ino_cls->new: $!";
58                 my $io = PublicInbox::In2Tie::io($inot);
59                 $self->SUPER::new($io, EPOLLIN | EPOLLET);
60         } else {
61                 require PublicInbox::FakeInotify;
62                 $inot = PublicInbox::FakeInotify->new;
63         }
64         $self->{inot} = $inot;
65         $self->{pathmap} = {}; # inboxdir => [ ibx, watch1, watch2, watch3...]
66         $self->{on_unlock} = {}; # lock path => ibx
67         refresh($self, $pi_cfg);
68         PublicInbox::FakeInotify::poll_once($self) if !$ino_cls;
69         $self;
70 }
71
72 sub event_step {
73         my ($self) = @_;
74         eval {
75                 my @events = $self->{inot}->read; # Linux::Inotify2::read
76                 my $on_unlock = $self->{on_unlock};
77                 for my $ev (@events) {
78                         if (my $ibx = $on_unlock->{$ev->fullname}) {
79                                 $ibx->on_unlock;
80                         }
81                 }
82         };
83         warn "{inot}->read err: $@\n" if $@;
84 }
85
86 # for graceful shutdown in PublicInbox::Daemon,
87 # just ensure the FD gets closed ASAP and subscribers
88 sub busy { 0 }
89
90 1;