I'm using Mojolicious (full, not lite) with hypnotoad to provide an internal API.

I was looking for a way to send some output to connections that would be closed when the worker times out.

The goal would be to be able to notify clients that the worker has been stopped.

I've tried a destructor, an END block and the handling of the STOP, QUIT, etcc signals inside the controller without any success (I guess that it's too late when the worker dies).

So I tried using the following code and although I can view when a worker is stopped, next I don't know how to send any output from those hooks.

package MyApi;
use Mojo::Base 'Mojolicious', -signatures;
use Mojo::Server::Hypnotoad;

# This method will run once at server start
sub startup ($self) {
    # Load configuration from config file
    my $config = $self->plugin('NotYAMLConfig');

    # Configure the application
    $self->secrets($config->{secrets});

    $self->hook(before_server_start => sub ($server, $app) {

        $server->on(reap => sub ($prefork, $pid) {
                                print "Closing worker $pid on reap event\n";
                            });

        $server->ioloop->on(finish => sub {
            my $loop = shift;
            print "Closing worker $$ on finish event\n";
        });
    });


    $self->app->config(hypnotoad => {
        listen => ['http://*:2003'], 
        workers => $ENV{HYPNOTOAD_WORKERS} // 10,
        inactivity_timeout =>  40,  
        heartbeat_timeout  =>  40, 
        heartbeat_interval =>   5, 
        graceful_timeout   =>   5, 
        clients => 1, # 1 worker 1 client
        accepts => 200
    });

    # Load the "api_nationals.yml" specification
    $self->plugin(
        "OpenAPI" => {
            url => $self->home->rel_file("api_nationals.yml")
        }
    );

}

Any idea or suggestion is welcome.

0

There are 0 best solutions below