Permalink
Browse files

improve support for systemd

  • Loading branch information...
1 parent d87c59f commit 25471294ef714e2116dd2bbe98de4893285c1121 @kraih committed Jul 29, 2016
Showing with 45 additions and 20 deletions.
  1. +4 −1 Changes
  2. +12 −1 lib/Mojo/Server/Hypnotoad.pm
  3. +6 −14 lib/Mojo/Server/Prefork.pm
  4. +17 −0 lib/Mojolicious/Guides/Cookbook.pod
  5. +6 −4 t/mojo/prefork.t
View
@@ -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.
@@ -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};
@@ -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
View
@@ -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;
@@ -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);
@@ -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 {
@@ -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
@@ -432,7 +424,7 @@ not running.
=head2 ensure_pid_file
- $prefork->ensure_pid_file;
+ $prefork->ensure_pid_file($pid);
Ensure L</"pid_file"> exists.
@@ -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
@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,
View
@@ -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';
@@ -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';

0 comments on commit 2547129

Please sign in to comment.