Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve Mojo::Server::Prefork to log if the process id file could not…
… be created (closes #862)
  • Loading branch information
kraih committed Oct 29, 2015
1 parent 3b377ad commit 60e8a4f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@

6.27 2015-10-29
- Updated HTML5 entities in Mojo::Util.
- Improved Mojo::Server::Prefork to log if the process id file could not be
created.

6.26 2015-10-28
- Renamed built-in templates with more descriptive names.
Expand Down
5 changes: 3 additions & 2 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -45,9 +45,10 @@ sub ensure_pid_file {
return if -e (my $file = $self->pid_file);

# Create PID file
$self->app->log->info(qq{Creating process id file "$file"});
die qq{Can't create process id file "$file": $!}
$self->app->log->info(qq{Can't create process id file "$file": $!})
and die qq{Can't create process id file "$file": $!}
unless open my $handle, '>', $file;
$self->app->log->info(qq{Creating process id file "$file"});
chmod 0644, $handle;
print $handle $$;
}
Expand Down
17 changes: 15 additions & 2 deletions t/mojo/prefork.t
Expand Up @@ -7,6 +7,8 @@ use Test::More;
plan skip_all => 'set TEST_PREFORK to enable this test (developer only!)'
unless $ENV{TEST_PREFORK};

use File::Basename 'dirname';
use File::Spec::Functions 'catfile';
use Mojo::IOLoop::Server;
use Mojo::Server::Prefork;
use Mojo::UserAgent;
Expand All @@ -26,6 +28,17 @@ is $prefork->check_pid, $$, 'right process id';
undef $prefork;
ok !-e $file, 'file has been cleaned up';

# Bad PID file
my $bad = catfile dirname(__FILE__), 'does_not_exist', 'test.pid';
$prefork = Mojo::Server::Prefork->new(pid_file => $bad);
my $log = '';
my $cb = $prefork->app->log->on(message => sub { $log .= pop });
eval { $prefork->ensure_pid_file };
like $@, qr/Can't create process id file/, 'right error';
unlike $log, qr/Creating process id file/, 'right message';
like $log, qr/Can't create process id file/, 'right message';
$prefork->app->log->unsubscribe(message => $cb);

# Multiple workers and graceful shutdown
my $port = Mojo::IOLoop::Server::->generate_port;
$prefork = Mojo::Server::Prefork->new(
Expand Down Expand Up @@ -54,8 +67,8 @@ $prefork->once(
);
$prefork->on(reap => sub { push @reap, pop });
$prefork->on(finish => sub { $graceful = pop });
my $log = '';
my $cb = $prefork->app->log->on(message => sub { $log .= pop });
$log = '';
$cb = $prefork->app->log->on(message => sub { $log .= pop });
is $prefork->healthy, 0, 'no healthy workers';
$prefork->run;
ok $healthy >= 1, 'healthy workers';
Expand Down

0 comments on commit 60e8a4f

Please sign in to comment.