How does hypnotoad handle the startup function?

51 views
Skip to first unread message

张建雷

unread,
Oct 24, 2016, 8:47:46 PM10/24/16
to Mojolicious
I used the Mojo::Pg module as an example.

--MyApp.pm--
package MyApp;
use Mojo::Base 'Mojolicious';
use Mojo::Pg;

# This method will run once at server start
sub startup {
  my $self = shift;

  # Documentation browser under "/perldoc"
  $self->plugin('PODRenderer');

  $self->config(hypnotoad => {workers => 4});

  $self->helper(pg => sub{ state $pg = Mojo::Pg->new('postgresql://jlzhang@/db1')});

  $self->pg->pubsub->listen(
    foo => sub {
      my ($pubsub, $payload) = @_;
      say $$;
    });

  # Router
  my $r = $self->routes;

  # Normal route to controller
  $r->get('/')->to('example#welcome');
}

1;

--Example.pm--
package MyApp::Controller::Example;
use Mojo::Base 'Mojolicious::Controller';

# This action will render a template
sub welcome {
  my $self = shift;

  $self->pg->pubsub->notify(foo => 'PostgreSQL rocks!');
  # Render template "example/welcome.html.ep" with message
  $self->render(msg => 'Welcome to the Mojolicious real-time web framework!');
}

1;

--hypnotoad -f script/my_app--
[Tue Oct 25 08:23:42 2016] [info] Listening at "http://*:8080"
Server available at http://127.0.0.1:8080
[Tue Oct 25 08:23:42 2016] [info] Manager 16422 started
[Tue Oct 25 08:23:42 2016] [info] Creating process id file "/home/jlzhang/tmp/my_app/script/hypnotoad.pid"
16426


There is 4 workers, but only one print process id.

I try to use Mojo::IOLoop::Delay:

Mojo::IOLoop->delay(
  sub {
    $self->pg->pubsub->listen(
      foo => sub {
        my ($pubsub, $payload) = @_;
        say $$;
      });
  });

--hypnotoad -f script/my_app--
[Tue Oct 25 08:37:47 2016] [info] Listening at "http://*:8080"
Server available at http://127.0.0.1:8080
[Tue Oct 25 08:37:47 2016] [info] Manager 16594 started
[Tue Oct 25 08:37:47 2016] [info] Creating process id file "/home/jlzhang/tmp/my_app/script/hypnotoad.pid"
16595
16596
16597
16598

Yes, all workers print process id.

How does hypnotoad handle the startup function?

BTW. Use Mojo::IOLoop::Delay in a statup function, does this comply with the specification?

sri

unread,
Oct 25, 2016, 2:16:57 AM10/25/16
to Mojolicious
The behaviour you've observed doesn't really have anything to do with Hypnotoad, it's an implementation detail of Mojo::Pg, and caused by the lack of a fork hook in Perl.

--
sebastian

张建雷

unread,
Oct 25, 2016, 3:20:23 AM10/25/16
to Mojolicious
Use Mojo::IOLoop::Delay in a statup function, does this comply with the specification?

在 2016年10月25日星期二 UTC+8下午2:16:57,sri写道:

Sebastian Riedel

unread,
Oct 25, 2016, 3:38:08 AM10/25/16
to mojol...@googlegroups.com
> Use Mojo::IOLoop::Delay in a statup function, does this comply with the
> specification?

You'd normally use Mojo::IOLoop->next_tick(sub {...}).

--
Sebastian Riedel
http://mojolicio.us
http://github.com/kraih
http://twitter.com/kraih
Reply all
Reply to author
Forward
0 new messages