user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 5/7] lei: dump and clear errors.log in daemon mode
Date: Wed, 20 Jan 2021 14:04:47 +0900	[thread overview]
Message-ID: <20210120050449.71330-6-e@80x24.org> (raw)
In-Reply-To: <20210119093435.17955-1-e@80x24.org>

Inspired by "dmesg -c", this should help users report bugs
and avoids eating up $XDG_RUNTIME_DIR.

Once lei is ready for release, hopefully the need for this
should be few an far between, but shit happens.
---
 lib/PublicInbox/LEI.pm | 27 ++++++++++++++++++++++-----
 t/lei.t                |  6 ++++++
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm
index 97ae2c41..6be6d10b 100644
--- a/lib/PublicInbox/LEI.pm
+++ b/lib/PublicInbox/LEI.pm
@@ -15,6 +15,7 @@ use Socket qw(AF_UNIX SOCK_SEQPACKET MSG_EOR pack_sockaddr_un);
 use Errno qw(EAGAIN EINTR ECONNREFUSED ENOENT ECONNRESET);
 use POSIX ();
 use IO::Handle ();
+use Fcntl qw(SEEK_SET);
 use Sys::Syslog qw(syslog openlog);
 use PublicInbox::Config;
 use PublicInbox::Syscall qw(SFD_NONBLOCK EPOLLIN EPOLLET);
@@ -26,7 +27,7 @@ use Text::Wrap qw(wrap);
 use File::Path qw(mkpath);
 use File::Spec;
 our $quit = \&CORE::exit;
-our $current_lei;
+our ($current_lei, $errors_log);
 my ($recv_cmd, $send_cmd);
 my $GLP = Getopt::Long::Parser->new;
 $GLP->configure(qw(gnu_getopt no_ignore_case auto_abbrev));
@@ -246,6 +247,7 @@ sub x_it ($$) {
 	my ($self, $code) = @_;
 	# make sure client sees stdout before exit
 	$self->{1}->autoflush(1) if $self->{1};
+	dump_and_clear_log();
 	if (my $sock = $self->{sock}) {
 		send($sock, "x_it $code", MSG_EOR);
 	} elsif (!($code & 127)) { # oneshot, ignore signals
@@ -264,7 +266,7 @@ sub out ($;@) { print { shift->{1} } @_ }
 
 sub err ($;@) {
 	my $self = shift;
-	my $err = $self->{2} // *STDERR{IO};
+	my $err = $self->{2} // ($self->{pgr} // [])->[2] // *STDERR{IO};
 	print $err @_, (substr($_[-1], -1, 1) eq "\n" ? () : "\n");
 }
 
@@ -300,6 +302,7 @@ sub atfork_child_wq {
 	$self->{sock} = $sock if -S $sock;
 	$self->{l2m}->{-wq_s1} = $l2m_wq_s1 if $l2m_wq_s1 && -S $l2m_wq_s1;
 	%PATH2CFG = ();
+	undef $errors_log;
 	$quit = \&CORE::exit;
 	@TO_CLOSE_ATFORK_CHILD = ();
 	(__WARN__ => sub { err($self, @_) },
@@ -483,6 +486,7 @@ sub optparse ($$$) {
 sub dispatch {
 	my ($self, $cmd, @argv) = @_;
 	local $current_lei = $self; # for __WARN__
+	dump_and_clear_log("from previous run\n");
 	return _help($self, 'no command given') unless defined($cmd);
 	my $func = "lei_$cmd";
 	$func =~ tr/-/_/;
@@ -772,6 +776,7 @@ sub event_step {
 	my ($self) = @_;
 	local %ENV = %{$self->{env}};
 	my $sock = $self->{sock};
+	local $current_lei = $self;
 	eval {
 		while (my @fds = $recv_cmd->($sock, my $buf, 4096)) {
 			if (scalar(@fds) == 1 && !defined($fds[0])) {
@@ -805,6 +810,15 @@ sub noop {}
 
 our $oldset; sub oldset { $oldset }
 
+sub dump_and_clear_log {
+	if (defined($errors_log) && -s STDIN && seek(STDIN, 0, SEEK_SET)) {
+		my @pfx = @_;
+		unshift(@pfx, "$errors_log ") if @pfx;
+		warn @pfx, do { local $/; <STDIN> };
+		truncate(STDIN, 0) or warn "ftruncate ($errors_log): $!";
+	}
+}
+
 # lei(1) calls this when it can't connect
 sub lazy_start {
 	my ($path, $errno, $narg) = @_;
@@ -836,9 +850,12 @@ sub lazy_start {
 	require PublicInbox::Listener;
 	require PublicInbox::EOFpipe;
 	(-p STDOUT) or die "E: stdout must be a pipe\n";
-	my ($err) = ($path =~ m!\A(.+?/)[^/]+\z!);
-	$err .= 'errors.log';
-	open(STDIN, '+>>', $err) or die "open($err): $!";
+	local $errors_log;
+	($errors_log) = ($path =~ m!\A(.+?/)[^/]+\z!);
+	$errors_log .= 'errors.log';
+	open(STDIN, '+>>', $errors_log) or die "open($errors_log): $!";
+	STDIN->autoflush(1);
+	dump_and_clear_log("from previous daemon process:\n");
 	POSIX::setsid() > 0 or die "setsid: $!";
 	my $pid = fork // die "fork: $!";
 	return if $pid;
diff --git a/t/lei.t b/t/lei.t
index d49dc01a..ef820fe3 100644
--- a/t/lei.t
+++ b/t/lei.t
@@ -258,6 +258,7 @@ SKIP: { # real socket
 	} // skip 'Socket::MsgHdr or Inline::C missing or unconfigured', 115;
 	local $ENV{XDG_RUNTIME_DIR} = "$home/xdg_run";
 	my $sock = "$ENV{XDG_RUNTIME_DIR}/lei/5.seq.sock";
+	my $err_log = "$ENV{XDG_RUNTIME_DIR}/lei/errors.log";
 
 	ok($lei->('daemon-pid'), 'daemon-pid');
 	is($err, '', 'no error from daemon-pid');
@@ -267,10 +268,15 @@ SKIP: { # real socket
 	ok(-S $sock, 'sock created');
 
 	$test_lei_common->();
+	is(-s $err_log, 0, 'nothing in errors.log');
+	open my $efh, '>>', $err_log or BAIL_OUT $!;
+	print $efh "phail\n" or BAIL_OUT $!;
+	close $efh or BAIL_OUT $!;
 
 	ok($lei->('daemon-pid'), 'daemon-pid');
 	chomp(my $pid_again = $out);
 	is($pid, $pid_again, 'daemon-pid idempotent');
+	like($err, qr/phail/, 'got mock "phail" error previous run');
 
 	ok($lei->(qw(daemon-kill)), 'daemon-kill');
 	is($out, '', 'no output from daemon-kill');

  parent reply	other threads:[~2021-01-20  5:04 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-19  9:34 [PATCH 0/9] lei bugfixes and error handling Eric Wong
2021-01-19  9:34 ` [PATCH 1/9] lei q: start ->mset while query_prepare runs Eric Wong
2021-01-19  9:34 ` [PATCH 2/9] lei q: fix SIGPIPE handling from lei2mail workers Eric Wong
2021-01-19  9:34 ` [PATCH 3/9] lei q: do not spawn MUA early Eric Wong
2021-01-19  9:34 ` [PATCH 4/9] lei: write daemon errors to the sock directory Eric Wong
2021-01-19  9:34 ` [PATCH 5/9] lei q: fix augment of compressed mailboxes Eric Wong
2021-01-19  9:34 ` [PATCH 6/9] lei_overview: do not write if $lei->{1} is gone Eric Wong
2021-01-19  9:34 ` [PATCH 7/9] t/lei: fix double-running of socket test with oneshot Eric Wong
2021-01-19  9:34 ` [PATCH 8/9] lei: test some likely errors due to misuse Eric Wong
2021-01-19  9:34 ` [PATCH 9/9] lei_overview: start implementing format detection Eric Wong
2021-01-20  5:04 ` [PATCH 0/7] lei: fixes piled higher and deeper Eric Wong
2021-01-20  5:16   ` misnumbered, should be [PATCH 10/9]..[PATCH 16/9] :x Eric Wong
2021-01-20  5:04 ` [PATCH 1/7] lei: allow more mbox inode types Eric Wong
2021-01-20  5:04 ` [PATCH 2/7] lei: exit code in oneshot mode Eric Wong
2021-01-20  5:04 ` [PATCH 3/7] overidx: eidx_prep: fix leftover dbh reference Eric Wong
2021-01-20  5:04 ` [PATCH 4/7] lei q: cleanup store initialization Eric Wong
2021-01-20  5:04 ` Eric Wong [this message]
2021-01-20  5:04 ` [PATCH 6/7] lei_xsearch: keep l2m->{-wq_s1} while preparing query Eric Wong
2021-01-20  5:04 ` [PATCH 7/7] lei_to_mail: call PublicInbox::IPC::DESTROY Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210120050449.71330-6-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).