New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

systemd support #983

Closed
kraih opened this Issue Jul 29, 2016 · 2 comments

Comments

Projects
None yet
2 participants
@kraih
Member

kraih commented Jul 29, 2016

It looks like Hypnotoad almost works with systemd, just zero downtime software upgrades still need a little work. The old manager process removes the PID file when it shuts down, and there is a small delay before the new manager process detects this and creates a new one, and systemd considers Hypnotoad dead during this delay.

diff --git a/lib/Mojo/Server/Hypnotoad.pm b/lib/Mojo/Server/Hypnotoad.pm
index 3a0af10..195d914 100644
--- a/lib/Mojo/Server/Hypnotoad.pm
+++ b/lib/Mojo/Server/Hypnotoad.pm
@@ -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,16 @@ 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
@@ -121,7 +131,7 @@ sub _manage {
     }

     # Timeout
-    kill 'KILL', $self->{new}
+    $prefork->cleanup(1) and kill 'KILL', $self->{new}
       if $self->{upgrade} + $self->upgrade_timeout <= steady_time;
   }
 }
diff --git a/lib/Mojo/Server/Prefork.pm b/lib/Mojo/Server/Prefork.pm
index 4ccac85..bd4ee7a 100644
--- a/lib/Mojo/Server/Prefork.pm
+++ b/lib/Mojo/Server/Prefork.pm
@@ -39,7 +39,7 @@ sub check_pid {
 }

 sub ensure_pid_file {
-  my $self = shift;
+  my ($self, $pid) = (shift, shift // $$);

   # Check if PID file already exists
   return if -e (my $file = $self->pid_file);
@@ -50,7 +50,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;
 }

 sub healthy {
@@ -433,6 +433,7 @@ not running.
 =head2 ensure_pid_file

   $prefork->ensure_pid_file;
+  $prefork->ensure_pid_file($pid);

 Ensure L</"pid_file"> exists.

This patch should mostly work, but still needs some cleaning up and tests.

@s1037989

This comment has been minimized.

Show comment
Hide comment
@s1037989

s1037989 Jul 29, 2016

Contributor

I would love to see better built-in support and official recommendations (Reference Manual / Cookbook / FAQ) for how to set up hypnotoad for system-managed service control.

Same with Minion!

e.g. Apache and Nginx include scripts for controlling the starting and stopping of their services, scripts that are designed for various system-level control. (Or perhaps I'm mistaken and it's actually the distros that create and maintain these, e.g. Debian, Ubuntu, Redhat, etc...)

Contributor

s1037989 commented Jul 29, 2016

I would love to see better built-in support and official recommendations (Reference Manual / Cookbook / FAQ) for how to set up hypnotoad for system-managed service control.

Same with Minion!

e.g. Apache and Nginx include scripts for controlling the starting and stopping of their services, scripts that are designed for various system-level control. (Or perhaps I'm mistaken and it's actually the distros that create and maintain these, e.g. Debian, Ubuntu, Redhat, etc...)

@kraih

This comment has been minimized.

Show comment
Hide comment
@kraih

kraih Jul 29, 2016

Member

And done. 2547129

Member

kraih commented Jul 29, 2016

And done. 2547129

@kraih kraih closed this Jul 29, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment