Skip to content

Hypnotoad prefork web server

yuki-kimoto edited this page Mar 13, 2011 · 13 revisions

Hypnotoad Prefork Web Server

Mojo::Server::Hypnotoad is built-in prefork web server in production environment. hypnotoad is command line interface to run Mojolicious application by Mojo::Server::Hypnotoad.

Note that Mojo::Server::Hypnotoad and hypnotoad is EXPERIMENTAL and might be changed without warnings.

Management

Start Server

Start Server.

hypnotoad myapp.pl

Server start on port 8080 by default. you can access it from web browser.

http://localhost:8080

Process ID file is created in the same directory.

hypnotoad.pid

Stop Server immediately

If you want to stop server immediately, send TERM signal to server porcess ID which is saved in pid file.

cat hypnotoad.pid | xargs kill -TERM

How to start server automatically after OS start.

If you want to run server from root user, you switch application user and type hypnotoad command.

su - appuser -c 'hypnotoad $HOME/webapp/myapp/myapp.pl'

And you add this into OS startup config file such as rc.local

/etc/rc.d/rc.local

Database connection problem in preforking

If you connect to database before preforking server and you use the connection to execute SQL, You see the following error in log or STDERR, for example, in MySQL.

MySQL server has gone away

The most simple solution is that you connect to database after preforking server. so you can establish the following two rules.

1. you must not connect to database in startup method.
2. Database initialization process must be written as default value of has function or attr method.

The following is Mojolicious and Mojolicious::Lite example.

Mojolicious:

package MyApp;
use Mojo::Base 'Mojolicious';

use DBI;

has dbh => sub {
    my $self = shift;
    
    my $data_source = "dbi:mysql:database=usertest";
    my $user = "ken";
    my $password = "ijdiuef";
    
    my $dbh = DBI->connect(
        $data_source,
        $user,
        $password,
        {RaiseError => 1}
    );

    return $dbh;
};

sub startup {
    my $self = shift;
    
    # You must not connect to database.

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

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

1;

You use dbh from controller.

package MyApp::Example;
use Mojo::Base 'Mojolicious::Controller';

# This action will render a template
sub welcome {
    my $self = shift;
    
    my $dbh = $self->app->dbh;
    
    my $sth = $dbh->prepare('select * from table1');
    
    $sth->execute;
    
    $self->render_text('Hello');
}

1;

Mojolicious::Lite

use Mojolicious::Lite;

use DBI;

# You must not connect to database in this location

# dbh attribute
app->attr(dbh => sub {
    my $self = shift;
    
    my $data_source = "dbi:mysql:database=test";
    my $user = undef;
    my $password = undef;
    
    my $dbh = DBI->connect($data_source, $user, $password);
    
    return $dbh;
});

get '/' => sub {
    my $self = shift;
    
    my $dbh = $self->app->dbh;
    
    my $sth = $dbh->prepare('select * from table1');
    
    $sth->execute;
    
    $self->render_text('Hello');
};

app->start;

Clone this wiki locally