Skip to content

Instantly share code, notes, and snippets.

/healthy.diff Secret

Created January 25, 2015 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/faf1202ba0235fb4d39d to your computer and use it in GitHub Desktop.
Save anonymous/faf1202ba0235fb4d39d to your computer and use it in GitHub Desktop.
diff --git a/lib/Mojo/Server/Hypnotoad.pm b/lib/Mojo/Server/Hypnotoad.pm
index d8e1682..5b09716 100644
--- a/lib/Mojo/Server/Hypnotoad.pm
+++ b/lib/Mojo/Server/Hypnotoad.pm
@@ -93,6 +93,9 @@ sub _hot_deploy {
sub _manage {
my $self = shift;
+ # Make sure all workers have a heartbeat
+ return unless $self->prefork->is_healthy;
+
# Upgraded
my $log = $self->prefork->app->log;
if ($ENV{HYPNOTOAD_PID} && $ENV{HYPNOTOAD_PID} ne $$) {
diff --git a/lib/Mojo/Server/Prefork.pm b/lib/Mojo/Server/Prefork.pm
index 1052111..3a24ad7 100644
--- a/lib/Mojo/Server/Prefork.pm
+++ b/lib/Mojo/Server/Prefork.pm
@@ -58,6 +58,10 @@ sub ensure_pid_file {
print $handle $$;
}
+sub is_healthy {
+ !grep { !$_->{ok} } values %{shift->{pool}};
+}
+
sub run {
my $self = shift;
@@ -78,8 +82,9 @@ sub run {
local $SIG{INT} = local $SIG{TERM} = sub { $self->_term };
local $SIG{CHLD} = sub {
while ((my $pid = waitpid -1, WNOHANG) > 0) {
- $self->app->log->debug("Worker $pid stopped.")
- if delete $self->emit(reap => $pid)->{pool}{$pid};
+ next unless my $w = delete $self->emit(reap => $pid)->{pool}{$pid};
+ $self->app->log->debug("Worker $pid stopped.");
+ $self->workers($self->workers - 1) unless $w->{ok};
}
};
local $SIG{QUIT} = sub { $self->_term(1) };
@@ -109,7 +114,7 @@ sub _heartbeat {
my $time = steady_time;
while ($chunk =~ /(\d+):(\d)\n/g) {
next unless my $w = $self->{pool}{$1};
- $self->emit(heartbeat => $1) and $w->{time} = $time;
+ $self->emit(heartbeat => $1) and @$w{qw(ok time)} = (1, $time);
$w->{graceful} ||= $time if $2;
}
}
@@ -119,7 +124,8 @@ sub _manage {
# Spawn more workers if necessary and check PID file
if (!$self->{finished}) {
- $self->_spawn while keys %{$self->{pool}} < $self->workers;
+ $self->{finished}++ if (my $workers = $self->workers) <= 0;
+ $self->_spawn while keys %{$self->{pool}} < $workers;
$self->ensure_pid_file;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment