Skip to content

Commit

Permalink
Log PID and location in one entry
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Oct 7, 2023
1 parent 93fa75e commit d3001eb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
7 changes: 5 additions & 2 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
Revision history for Perl extension PGXN::Manager

0.31.2
- Combined the pgxn_consumer PID number and file location into one
output.

0.31.1 2023-10-07T21:40:53Z
- Restored the writing of the PID file, accidentally removed in v0.31.0.
Added tests to ensure continues to be passed properly.
- Restored the writing of the pgxn_consumer PID file, accidentally
removed in v0.31.0. Added tests to ensure continues to be passed
properly.

0.31.0 2023-10-07T19:02:24Z
- Added the application names (pgxn_manager and pgxn_consumer) to the
Expand Down
7 changes: 5 additions & 2 deletions lib/PGXN/Manager/Consumer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ sub go {
pid_file => $cfg->{'pid-file'},
);
if (my $pid = $daemon->Init) {
_log(_log_fh($cfg->{'log-file'}), "INFO: Forked PID $pid");
my $pid_file = $cfg->{'pid-file'} || 'STDOUT';
_log(
_log_fh($cfg->{'log-file'}),
"INFO: Forked PID $pid written to $pid_file",
);
return 0;
}
}

# In the child process. Set up log file handle and go.
$cfg->{log_fh} = _log_fh delete $cfg->{'log-file'};
_log($cfg->{log_fh}, "INFO: PID written to " . ($cfg->{'pid-file'} || 'STDOUT'));
$cfg->{pid_file} = delete $cfg->{'pid-file'} if exists $cfg->{'pid-file'};
my $cmd = $class->new( $cfg );
$SIG{TERM} = sub { $cmd->continue(0) };
Expand Down
40 changes: 26 additions & 14 deletions t/consumer.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use Encode qw(encode_utf8);
use JSON::XS;
use File::Temp ();

use Test::More tests => 108;
use Test::More tests => 113;
# use Test::More 'no_plan';
use Test::Output;
use Test::MockModule;
Expand Down Expand Up @@ -210,10 +210,6 @@ LOAD: {
##############################################################################
# Test go.
DAEMONIZE: {
my $tmp = File::Temp->new(UNLINK => 0);
my $pid_file = $tmp->filename;
file_exists_ok $pid_file, 'PID file should exist';

# Mock Proc::Daemon
my $mock_proc = Test::MockModule->new('Proc::Daemon');
$mock_proc->mock(Init => 0);
Expand All @@ -228,28 +224,45 @@ DAEMONIZE: {
my $mocker = Test::MockModule->new($CLASS);
my $ran;
$mocker->mock(run => sub { $ran = 1; 0 });
local @ARGV = (qw(--env test --daemonize --pid-file), $pid_file);
local @ARGV = (qw(--env test --daemonize --pid-file foo));
stdout_is { is $CLASS->go, 0, 'Should get zero from go' }
"$logtime - INFO: PID written to $pid_file\n",
'Should have logged PID file location';
"", 'Should have logged nothing';
is_deeply \%pd_params, {
work_dir => Cwd::getcwd,
dont_close_fh => [qw(STDERR STDOUT)],
pid_file => $pid_file,
pid_file => 'foo',
}, 'Should have passed params to Proc::Daemon->new';

ok $ran, 'Should have run';
is output(), '', 'Should have no output';
ok defined delete $SIG{TERM}, 'Should have set term signal';
is delete $ENV{PLACK_ENV}, 'test', 'Should have set test env';
file_not_exists_ok $pid_file, 'Should have deleted pid file';

# Now make a pid.
my $tmp = File::Temp->new(UNLINK => 0);
my $pid_file = $tmp->filename;
file_exists_ok $pid_file, 'PID file should exist';
$mock_proc->mock(Init => 42);
$ran = 0;
@ARGV = (qw(-D --pid-file), $pid_file);
stdout_is { is $CLASS->go, 0, 'Should get zero from go' }
"$logtime - INFO: Forked PID 42 written to $pid_file\n",
'Should have emitted PID';
ok !$ran, 'Should not have run';
is $SIG{TERM}, undef, 'Should not have set term signal';
is delete $ENV{PLACK_ENV}, 'development', 'Should have set development env';
is_deeply \%pd_params, {
work_dir => Cwd::getcwd,
dont_close_fh => [qw(STDERR STDOUT)],
pid_file => $pid_file,
}, 'Should have passed params to Proc::Daemon->new';

# Try with no PID file (shoud log it was written to STDOUT).
$ran = 0;
@ARGV = qw(-D);
stdout_is { is $CLASS->go, 0, 'Should get zero from go' }
"$logtime - INFO: Forked PID 42\n", 'Should have emitted PID';
"$logtime - INFO: Forked PID 42 written to STDOUT\n",
'Should have emitted PID';
ok !$ran, 'Should not have run';
is $SIG{TERM}, undef, 'Should not have set term signal';
is delete $ENV{PLACK_ENV}, 'development', 'Should have set development env';
Expand All @@ -267,9 +280,8 @@ GO: {
my $mocker = Test::MockModule->new($CLASS);
my $ran;
$mocker->mock(run => sub { $ran = 1; 0 });
stdout_is { is $CLASS->go, 0, 'Should get zero from go' }
"$logtime - INFO: PID written to STDOUT\n",
'Should have logged PID written to STDOUT';
stdout_is { is $CLASS->go, 0, 'Should get zero from go' } '',
'Should have logged nothing';
ok $ran, 'Should have run';
is_deeply output(), '', 'Should have no output';
ok defined delete $SIG{TERM}, 'Should have set term signal';
Expand Down

0 comments on commit d3001eb

Please sign in to comment.