4

I have simple web application written in perl/Mojolicious and running under hypnotoad.

I need to define some handler for the "spawn" event (emited by Mojo::Server::Prefork).

But i dont know, how to insert this hander definitiion in the code of startup method of Mojolicious application. $self->on("spawn"=>sub {}) doesnt work :( And Dumper($self) was not helpful at all: there are no $self->server or $server->prefork ...

Tell me please, how to do it.

Thanks!

drvtiny
  • 705
  • 3
  • 13
  • The server is not the application. It's in front of the application. The Hypnotoad object has your Mojo::Server::Prefork object. I am not sure you can reach that from the application. See https://metacpan.org/pod/Mojo::Server::Hypnotoad#prefork - from looking [at the code](https://metacpan.org/source/SRI/Mojolicious-7.30/lib/Mojo/Server/Hypnotoad.pm#L12) I think you would have to implement a subclass of either Mojo::Server::Prefork or Mojo::Server::Hypnotoad and put your `on("span")` handler there, then get Hypnotoad to use that. But I don't see how. Not familiar enough with Mojo, sorry. – simbabque Apr 10 '17 at 12:47
  • Hmm... Maybe you know, how to run some code in the hypnotoad worker whenever it spawns? I tried Mojo::IOLopp->next_tick as described in the Mojo documentation, but it seems doesnt work – drvtiny Apr 10 '17 at 13:35
  • I'm afraid I don't. I just read the documentation and took a look at the code. I can peek some more later, but I've never actually used this stuff. – simbabque Apr 10 '17 at 13:39

1 Answers1

0

Although i still dont know how to define handler fired on process "spawn", i can tell that absolutely the same thing can be done by using

Mojo::IOLoop->singleton->next_tick(sub {
  doingSomethingOnProcSpawn()
});

As it described in Mojolicious Cookbook (http://mojolicious.org/perldoc/Mojolicious/Guides/Cookbook#Pre-forking):

During startup your application is preloaded in the manager process, which does not run an event loop, so you can use "next_tick" in Mojo::IOLoop to run code whenever a new worker process has been forked and its event loop gets started.

Hint: As i see in my real application, Mojo::IOLoop->singleton->next_tick and Mojo::IOLoop->next_tick works absolutely identically, so i dont know what is the difference between them.

drvtiny
  • 705
  • 3
  • 13