Skip to content

Commit

Permalink
a few more controller examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 26, 2015
1 parent 0ed85b1 commit d25c31d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/Mojo/Server/Prefork.pm
Expand Up @@ -103,24 +103,6 @@ sub run {

sub _heartbeat { shift->{writer}->syswrite("$$:$_[0]\n") or exit 0 }

sub _heartbeats {
my $self = shift;

# Poll for heartbeats
my $poll = $self->{poll};
$poll->poll(1);
return unless $poll->handles(POLLIN | POLLPRI);
return unless $self->{reader}->sysread(my $chunk, 4194304);

# Update heartbeats (and stop gracefully if necessary)
my $time = steady_time;
while ($chunk =~ /(\d+):(\d)\n/g) {
next unless my $w = $self->{pool}{$1};
@$w{qw(healthy time)} = (1, $time) and $self->emit(heartbeat => $1);
$w->{graceful} ||= $time if $2;
}
}

sub _manage {
my $self = shift;

Expand All @@ -134,7 +116,7 @@ sub _manage {
elsif (!keys %{$self->{pool}}) { return delete $self->{running} }

# Wait for heartbeats
$self->emit('wait')->_heartbeats;
$self->_wait;

my $interval = $self->heartbeat_interval;
my $ht = $self->heartbeat_timeout;
Expand Down Expand Up @@ -223,6 +205,24 @@ sub _term {
$self->{graceful} = 1 if $graceful;
}

sub _wait {
my $self = shift;

# Poll for heartbeats
my $poll = $self->emit('wait')->{poll};
$poll->poll(1);
return unless $poll->handles(POLLIN | POLLPRI);
return unless $self->{reader}->sysread(my $chunk, 4194304);

# Update heartbeats (and stop gracefully if necessary)
my $time = steady_time;
while ($chunk =~ /(\d+):(\d)\n/g) {
next unless my $w = $self->{pool}{$1};
@$w{qw(healthy time)} = (1, $time) and $self->emit(heartbeat => $1);
$w->{graceful} ||= $time if $2;
}
}

1;

=encoding utf8
Expand Down Expand Up @@ -505,7 +505,7 @@ Ensure L</"pid_file"> exists.
my $healthy = $prefork->healthy;
Number of active worker processes with a heartbeat.
Number of currently active worker processes with a heartbeat.
=head2 run
Expand Down
9 changes: 9 additions & 0 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -556,6 +556,9 @@ L<Mojolicious::Plugin::DefaultHelpers> and L<Mojolicious::Plugin::TagHelpers>.
# Make sure to use the "title" helper and not the controller method
$c->helpers->title('Welcome!');
# Use a nested helper instead of the "reply" controller method
$c->helpers->reply->not_found;
=head2 on
my $cb = $c->on(finish => sub {...});
Expand Down Expand Up @@ -666,6 +669,9 @@ L</"stash">.
# Render JSON
$c->render(json => {test => 'I ♥ Mojolicious!'});
# Render inline template
$c->render(inline => '<%= 1 + 1 %>');
# Render template "foo/bar.html.ep"
$c->render(template => 'foo/bar', format => 'html', handler => 'ep');
Expand Down Expand Up @@ -713,6 +719,9 @@ return C<undef>, all arguments get localized automatically and are only
available during this render operation, takes the same arguments as
L</"render">.
# Render inline template
my $two = $c->render_to_string(inline => '<%= 1 + 1 %>');
=head2 rendered
$c = $c->rendered;
Expand Down

0 comments on commit d25c31d

Please sign in to comment.