]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStoreErr.pm
www: drop --subject from "git send-email" instructions
[public-inbox.git] / lib / PublicInbox / LeiStoreErr.pm
1 # Copyright (C) 2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # forwards stderr from lei/store process to any lei clients using
5 # the same store, falls back to syslog if no matching clients exist.
6 package PublicInbox::LeiStoreErr;
7 use strict;
8 use v5.10.1;
9 use parent qw(PublicInbox::DS);
10 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
11 use Sys::Syslog qw(openlog syslog closelog);
12 use IO::Handle (); # ->blocking
13
14 sub new {
15         my ($cls, $rd, $lei) = @_;
16         my $self = bless { sock => $rd, store_path => $lei->store_path }, $cls;
17         $rd->blocking(0);
18         $self->SUPER::new($rd, EPOLLIN | EPOLLONESHOT);
19 }
20
21 sub event_step {
22         my ($self) = @_;
23         my $rbuf = $self->{rbuf} // \(my $x = '');
24         $self->do_read($rbuf, 8192, length($$rbuf)) or return;
25         my $cb;
26         my $printed;
27         for my $lei (values %PublicInbox::DS::DescriptorMap) {
28                 $cb = $lei->can('store_path') // next;
29                 next if $cb->($lei) ne $self->{store_path};
30                 my $err = $lei->{2} // next;
31                 print $err $$rbuf and $printed = 1;
32         }
33         if (!$printed) {
34                 openlog('lei/store', 'pid,nowait,nofatal,ndelay', 'user');
35                 for my $l (split(/\n/, $$rbuf)) { syslog('warning', '%s', $l) }
36                 closelog(); # don't share across fork
37         }
38 }
39
40 1;