Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improve support for systemd
  • Loading branch information
kraih committed Jul 29, 2016
1 parent d87c59f commit 2547129
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
5 changes: 4 additions & 1 deletion Changes
@@ -1,5 +1,8 @@

7.01 2016-07-26
7.01 2016-07-30
- Improved support for systemd.
- Fixed a bug in Mojo::Server::Prefork where PID files would not contain a
newline character.

7.0 2016-07-19
- Code name "Doughnut", this is a major release.
Expand Down
13 changes: 12 additions & 1 deletion lib/Mojo/Server/Hypnotoad.pm
Expand Up @@ -56,7 +56,7 @@ sub run {
weaken $self;
$prefork->on(wait => sub { $self->_manage });
$prefork->on(reap => sub { $self->_cleanup(pop) });
$prefork->on(finish => sub { $self->{finished} = 1 });
$prefork->on(finish => sub { $self->_finish });
# Testing
_exit('Everything looks good!') if $ENV{HYPNOTOAD_TEST};
Expand Down Expand Up @@ -87,6 +87,17 @@ sub _cleanup {
sub _exit { say shift and exit 0 }
sub _finish {
my $self = shift;
$self->{finish} = 1;
return unless my $new = $self->{new};
my $prefork = $self->prefork->cleanup(0);
unlink $prefork->pid_file;
$prefork->ensure_pid_file($new);
}
sub _hot_deploy {
# Make sure server is running
Expand Down
20 changes: 6 additions & 14 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -14,15 +14,7 @@ has heartbeat_interval => 5;
has pid_file => sub { catfile tmpdir, 'prefork.pid' };
has workers => 4;

sub DESTROY {
my $self = shift;

# Worker
return unless $self->cleanup;

# Manager
if (my $file = $self->pid_file) { unlink $file if -w $file }
}
sub DESTROY { unlink $_[0]->pid_file if $_[0]->cleanup }

sub check_pid {
my $file = shift->pid_file;
Expand All @@ -34,12 +26,12 @@ sub check_pid {
return $pid if $pid && kill 0, $pid;

# Not running
unlink $file if -w $file;
unlink $file;
return undef;
}

sub ensure_pid_file {
my $self = shift;
my ($self, $pid) = @_;

# Check if PID file already exists
return if -e (my $file = $self->pid_file);
Expand All @@ -50,7 +42,7 @@ sub ensure_pid_file {
unless open my $handle, '>', $file;
$self->app->log->info(qq{Creating process id file "$file"});
chmod 0644, $handle;
print $handle $$;
print $handle "$pid\n";
}

sub healthy {
Expand Down Expand Up @@ -99,7 +91,7 @@ sub _manage {
# Spawn more workers if necessary and check PID file
if (!$self->{finished}) {
$self->_spawn while keys %{$self->{pool}} < $self->workers;
$self->ensure_pid_file;
$self->ensure_pid_file($$);
}

# Shutdown
Expand Down Expand Up @@ -432,7 +424,7 @@ not running.
=head2 ensure_pid_file
$prefork->ensure_pid_file;
$prefork->ensure_pid_file($pid);
Ensure L</"pid_file"> exists.
Expand Down
17 changes: 17 additions & 0 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -148,6 +148,23 @@ C<X-Forwarded-For> and C<X-Forwarded-Proto> headers.
# myapp.conf
{hypnotoad => {proxy => 1}};

To manage Hypnotoad with systemd, you can use a unit configuration file like

This comment has been minimized.

Copy link
@s1037989

s1037989 Jul 29, 2016

Contributor

Woo hoo! Love it! 👍

this.

[Unit]
Description=My Mojolicious application
After=default.target

[Service]
Type=forking
PIDFile=/home/sri/myapp/script/hypnotoad.pid
ExecStart=/path/to/hypnotoad /home/sri/myapp/script/my_app
ExecReload=/path/to/hypnotoad /home/sri/myapp/script/my_app
KillMode=process

[Install]
WantedBy=default.target

=head2 Zero downtime software upgrades

Hypnotoad makes zero downtime software upgrades (hot deployment) very simple,
Expand Down
10 changes: 6 additions & 4 deletions t/mojo/prefork.t
Expand Up @@ -12,18 +12,20 @@ use File::Spec::Functions 'catfile';
use Mojo::IOLoop::Server;
use Mojo::Server::Prefork;
use Mojo::UserAgent;
use Mojo::Util 'spurt';
use Mojo::Util 'slurp';

# Manage and clean up PID file
my $prefork = Mojo::Server::Prefork->new;
my $file = $prefork->pid_file;
ok !$prefork->check_pid, 'no process id';
spurt "\n", $file;
$prefork->ensure_pid_file(-23);
ok -e $file, 'file exists';
is slurp($file), "-23\n", 'right process id';
ok !$prefork->check_pid, 'no process id';
ok !-e $file, 'file has been cleaned up';
$prefork->ensure_pid_file;
$prefork->ensure_pid_file($$);
ok -e $file, 'file exists';
is slurp($file), "$$\n", 'right process id';
is $prefork->check_pid, $$, 'right process id';
undef $prefork;
ok !-e $file, 'file has been cleaned up';
Expand All @@ -34,7 +36,7 @@ $prefork = Mojo::Server::Prefork->new(pid_file => $bad);
$prefork->app->log->level('fatal');
my $log = '';
my $cb = $prefork->app->log->on(message => sub { $log .= pop });
eval { $prefork->ensure_pid_file };
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';
Expand Down

0 comments on commit 2547129

Please sign in to comment.