]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiStoreErr.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / LeiStoreErr.pm
1 # Copyright (C) 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 v5.12;
8 use parent qw(PublicInbox::DS);
9 use PublicInbox::Syscall qw(EPOLLIN);
10 use Sys::Syslog qw(openlog syslog closelog);
11 use IO::Handle (); # ->blocking
12
13 sub new {
14         my ($cls, $rd, $lei) = @_;
15         my $self = bless { sock => $rd, store_path => $lei->store_path }, $cls;
16         $rd->blocking(0);
17         $self->SUPER::new($rd, EPOLLIN); # level-trigger
18 }
19
20 sub event_step {
21         my ($self) = @_;
22         my $n = sysread($self->{sock}, my $buf, 8192);
23         return ($!{EAGAIN} ? 0 : $self->close) if !defined($n);
24         return $self->close if !$n;
25         my $printed;
26         for my $lei (values %PublicInbox::DS::DescriptorMap) {
27                 my $cb = $lei->can('store_path') // next;
28                 next if $cb->($lei) ne $self->{store_path};
29                 my $err = $lei->{2} // next;
30                 print $err $buf and $printed = 1;
31         }
32         if (!$printed) {
33                 openlog('lei/store', 'pid,nowait,nofatal,ndelay', 'user');
34                 for my $l (split(/\n/, $buf)) { syslog('warning', '%s', $l) }
35                 closelog(); # don't share across fork
36         }
37 }
38
39 1;