Skip to content

Commit

Permalink
Delete PID file on exit
Browse files Browse the repository at this point in the history
I had thought that Proc::Daemon did it, but apparently not, because the
service was failing to restart when the host was reboot. This should fix
it.
  • Loading branch information
theory committed Oct 7, 2023
1 parent 016ef3b commit 0fb99d0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Revision history for Perl extension PGXN::Manager
0.30.2
- Added the application names (pgxn_manager and pgxn_consumer) to the
Postgres connections.
- Updated pgxn_consumer to delete the PID file on exit.

0.30.1 2023-02-18T23:15:06Z
- Added the --log-file option to `pgxn_consumer`.
Expand Down
13 changes: 11 additions & 2 deletions lib/PGXN/Manager/Consumer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use constant CHANNELS => qw(release new_user new_mirror);
has verbose => (is => 'ro', isa => 'Int', required => 1, default => 0);
has interval => (is => 'ro', isa => 'Num', required => 1, default => 5);
has continue => (is => 'rw', isa => 'Bool', required => 1, default => 1);
has pid_file => (is => 'rw', isa => 'Str');
has log_fh => (is => 'ro', isa => 'IO::Handle', required => 1, default => sub {
_log_fh()
});
Expand Down Expand Up @@ -64,7 +65,7 @@ sub go {
my $daemon = Proc::Daemon->new(
work_dir => getcwd,
dont_close_fh => [qw(STDERR STDOUT)],
pid_file => $cfg->{pid},
pid_file => $cfg->{pid_file},
);
if (my $pid = $daemon->Init) {
_log(_log_fh($cfg->{'log-file'}), "INFO: Forked PID $pid");
Expand All @@ -74,11 +75,19 @@ sub go {

# In the child process. Set up log file handle and go.
$cfg->{log_fh} = _log_fh delete $cfg->{'log-file'};
$cfg->{pid_file} = delete $cfg->{'pid-file'} if exists $cfg->{'pid-file'};
my $cmd = $class->new( $cfg );
$SIG{TERM} = sub { $cmd->continue(0) };
$cmd->run(@ARGV);
}

sub DEMOLISH {
my $self = shift;
if (my $path = $self->pid_file) {
unlink $path;
}
}

sub run {
my $self = shift;
my $pgxn = PGXN::Manager->instance;
Expand Down Expand Up @@ -199,7 +208,7 @@ sub _config {
\%opts,
'env|E=s',
'daemonize|D',
'pid=s',
'pid-file|pid=s',
'log-file|l=s',
'interval|i=s',
'verbose|V+',
Expand Down
31 changes: 25 additions & 6 deletions t/consumer.t
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use Encode qw(encode_utf8);
use JSON::XS;
use File::Temp ();

use Test::More tests => 98;
use Test::More tests => 103;
# use Test::More 'no_plan';
use Test::Output;
use Test::MockModule;
use Test::Exception;
use Test::File;
use Test::File::Contents;

##############################################################################
Expand Down Expand Up @@ -57,7 +58,8 @@ DEFAULTS: {
my $consumer = new_ok $CLASS;
is $consumer->verbose, 0, 'Default verbosity is 0';
is $consumer->interval, 5, 'Default interval is 5';
is $consumer->continue, 1, 'Defaault continue is 1';
is $consumer->continue, 1, 'Default continue is 1';
is $consumer->pid_file, undef, 'Default pid file is undef';
ok $consumer->log_fh, 'Should have log file handle';
}

Expand Down Expand Up @@ -97,6 +99,18 @@ sub output {
}
my $chans = join(', ', PGXN::Manager::Consumer::CHANNELS);

##############################################################################
# Test pid file cleanup.
PID: {
my $tmp = File::Temp->new(UNLINK => 0);
my $fn = $tmp->filename;
do {
my $consumer = $CLASS->new(pid_file => $fn);
file_exists_ok $fn, 'PID file should exist';
};
file_not_exists_ok $fn, 'PID file be gone';
}

##############################################################################
# Load the test environment configuration.
sub load_config {
Expand All @@ -119,7 +133,7 @@ CONFIG: {
my $opts = $CLASS->_config;
is_deeply $opts, {
daemonize => 1,
pid => 'pid.txt',
'pid-file' => 'pid.txt',
interval => 2.2,
verbose => 1,
'log-file' => 'log.txt',
Expand Down Expand Up @@ -195,18 +209,23 @@ LOAD: {
##############################################################################
# Test go.
DAEMONIZE: {
# Mock Proc::Daemon
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);
my $mocker = Test::MockModule->new($CLASS);
my $ran;
$mocker->mock(run => sub { $ran = 1; 0 });
local @ARGV = qw(--env test --daemonize);
local @ARGV = (qw(--env test --daemonize --pid-file), $pid_file);
is $CLASS->go, 0, 'Should get zero from go';
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.
$mock_proc->mock(Init => 42);
Expand All @@ -215,7 +234,7 @@ DAEMONIZE: {
stdout_is { is $CLASS->go, 0, 'Should get zero from go' }
"$logtime - INFO: Forked PID 42\n", 'Should have emitted PID';
ok !$ran, 'Should not have run';
is $SIG{TERM}, undef, 'Should not ahve set term signal';
is $SIG{TERM}, undef, 'Should not have set term signal';
is delete $ENV{PLACK_ENV}, 'development', 'Should have set development env';
$mocker->unmock('run');
}
Expand Down

0 comments on commit 0fb99d0

Please sign in to comment.