1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # internal command for dealing with inotify, kqueue vnodes, etc
5 # it is a semi-persistent worker
6 package PublicInbox::LeiNoteEvent;
9 use parent qw(PublicInbox::IPC);
13 our $to_flush; # { cfgpath => $lei }
16 my ($lei, $manual) = @_;
17 my $lne = delete $lei->{cfg}->{-lei_note_event} // return;
18 $lne->{lei_sock} = $lei->{sock} if $manual;
19 $lne->wq_close; # runs _lei_wq_eof;
22 # we batch up writes and flush every 5s (matching Linux default
23 # writeback behavior) since MUAs can trigger a storm of inotify events
24 sub flush_task { # PublicInbox::DS timer callback
25 my $todo = $to_flush // return;
27 for my $lei (values %$todo) { flush_lei($lei) }
30 # sets a timer to flush
31 sub note_event_arm_done ($) {
33 PublicInbox::DS::add_uniq_timer('flush_timer', 5, \&flush_task);
34 $to_flush->{$lei->{cfg}->{'-f'}} //= $lei;
37 sub eml_event ($$$$) {
38 my ($self, $eml, $vmd, $state) = @_;
39 my $sto = $self->{lei}->{sto};
40 if ($state =~ /\Aimport-(?:rw|ro)\z/) {
41 $sto->wq_do('set_eml', $eml, $vmd);
42 } elsif ($state =~ /\Aindex-(?:rw|ro)\z/) {
43 my $xoids = $self->{lei}->ale->xoids_for($eml);
44 $sto->wq_do('index_eml_only', $eml, $vmd, $xoids);
45 } elsif ($state =~ /\Atag-(?:rw|ro)\z/) {
48 $self->{lse}->kw_changed($eml, $vmd->{kw}, $docids);
49 } // 1; # too new, assume changed since still to-be-committed.
50 if (scalar @$docids) { # already in lei/store
51 $sto->wq_do('set_eml_vmd', undef, $vmd, $docids) if $c;
52 } elsif (my $xoids = $self->{lei}->ale->xoids_for($eml)) {
53 # it's in an external, only set kw, here
54 $sto->wq_do('set_xvmd', $xoids, $eml, $vmd);
55 } # else { totally unknown: ignore
57 warn "unknown state: $state (in $self->{lei}->{cfg}->{'-f'})\n";
61 sub maildir_event { # via wq_io_do
62 my ($self, $fn, $vmd, $state) = @_;
63 if (my $eml = PublicInbox::InboxWritable::eml_from_path($fn)) {
64 eml_event($self, $eml, $vmd, $state);
65 } elsif ($! == ENOENT) {
66 $self->{lms}->clear_src(@{$vmd->{sync_info}});
67 } # else: eml_from_path already warns
71 my ($lei, $folder, $new_cur, $bn, $fn, @rest) = @_;
72 die "BUG: unexpected: @rest" if @rest;
73 my $cfg = $lei->_lei_cfg or return; # gone (race)
74 my $sto = $lei->_lei_store or return; # gone
75 return flush_lei($lei, 1) if $folder eq 'done'; # special case
76 my $lms = $lei->lms or return;
77 $lms->lms_write_prepare if $new_cur eq ''; # for ->clear_src below
78 $lei->{opt}->{quiet} = 1;
79 $lms->arg2folder($lei, [ $folder ]);
80 my $state = $cfg->get_1("watch.$folder.state") // 'tag-rw';
81 return if $state eq 'pause';
82 return $lms->clear_src($folder, \$bn) if $new_cur eq '';
85 $sto->write_prepare($lei);
86 require PublicInbox::MdirReader;
87 my $self = $cfg->{-lei_note_event} //= do {
88 my $wq = bless { lms => $lms }, __PACKAGE__;
89 # MUAs such as mutt can trigger massive rename() storms so
90 # use some CPU, but don't overwhelm slower storage, either
91 my $jobs = $wq->detect_nproc // 1;
92 $jobs = 4 if $jobs > 4; # same default as V2Writable
93 my ($op_c, $ops) = $lei->workers_start($wq, $jobs);
94 $lei->wait_wq_events($op_c, $ops);
95 note_event_arm_done($lei);
98 if ($folder =~ /\Amaildir:/i) {
99 my $fl = PublicInbox::MdirReader::maildir_basename_flags($bn)
101 return if index($fl, 'T') >= 0;
102 my $kw = PublicInbox::MdirReader::flags2kw($fl);
103 my $vmd = { kw => $kw, sync_info => [ $folder, \$bn ] };
104 $self->wq_do('maildir_event', $fn, $vmd, $state);
108 sub ipc_atfork_child {
110 $self->{lei}->_lei_atfork_child(1); # persistent, for a while
111 $self->{lms}->lms_write_prepare;
112 $self->{lse} = $self->{lei}->{sto}->search;
113 $self->SUPER::ipc_atfork_child;
116 sub _lei_wq_eof { # EOF callback for main lei daemon
118 my $lne = delete $lei->{lne} or return $lei->fail;
119 $lei->sto_done_request($lne->{lei_sock});