私は機知に富んでいます。私の現在の (失敗した) MySQL ベースの小さな Web ページの実装は、Project::Connection への呼び出しで初期化される 2 つのパッケージ スコープ (私たちの) ハンドル (mysql_handle および memc_handle) を格納するモジュール Project::Connection に大きく依存しています。 :child_init PerlChildInitHandler フェーズ中。同様に、PerlChildExitHandler フェーズで接続を切断します (切断したい)。問題は、ChildInit サブルーチン中にハンドラーが適切に作成されていることをデバッグで確認したことですが、「GET /」を要求すると、要求を処理するモジュール (Project::Web::Home、これは Project::Connection を使用します) が報告します。両方のハンドルが未定義です!
関連するコードは次のとおりです。
startup.pl (サーバー構成で PerlRequire を使用して呼び出されます):
use Apache2::Reload; # PerlInitHandler inside /var/www
use Apache2::ServerUtil;
use lib "/var/www/app";
use Project; # has the response handler
use Project::Connection; # has the childinit and childexit handlers
$s = Apache2::ServerUtil->server;
$s->set_handlers('PerlChildInitHandler' => \&Project::Connection::child_init);
$s->set_handlers('PerlChildExitHandler' => \&Project::Connection::child_exit);
1;
Project/Connection.pm (接続ハンドルを保持):
package Project::Connection;
use DBI;
use Cache::Memcached;
use Project::Config;
our ($mysql_handle, $memc_handle);
sub mysql { $mysql_handle }
sub memc { $memc_handle }
sub child_init {
# create the mysql connection
my ($db, $host, $port, $user, $pass)
= Project::Config::get('db', 'db_host', 'db_port', 'db_user', 'db_pass');
$mysql_handle = DBI->connect("dbi:mysql:database=$db;host=$host;port=$port",
$user, $pass)
or die "Failed to establish a connection with mysqld: $DBI::errstr";
# create the memcache connection
my ($socket) = Project::Config::get('memcached_sock');
$memc_handle = Cache::Memcached->new('servers' => $socket)
or die "Failed to establish a connection with memcached";
}
sub child_exit {
$mysql_handle->disconnect;
$memc_handle->disconnect_all;
}
1;
Project.pm:
package Project;
use Apache2::Const;
use Apache2::Request;
use Project::Web::Home;
sub handler {
my ($request_rec) = @_;
my $request = Apache2::Request->new( $request_rec );
# load the module for the webpage
my $page = substr $request->uri, 1;
$page = length $page ? 'home' : $page; # take care of directory requests
eval "Project::Web::${page}::do";
if ($@) {
Project::Web::home::do; # do this rather than 404
}
Apache2::Const::OK;
}
1;
プロジェクト/Web/home.pm:
package Project::Web::home;
use strict;
use warnings;
use Project::Connection;
sub do {
my ($user, $args) = @_;
print "Content-Type: text/plain\n\n";
$dbh = Project::Connection::mysql; # this is undefined!
# ...
}
1;
ここで、Apache の起動時に、startup.pl をコンパイルして実行すると、すべてのモジュールが親プロセスにコンパイルされることを理解しています。次に、親プロセスは子プロセスにフォークし、コンパイルされたコードをコピーして、子が親のコピーになるようにします。その後、子プロセスは、mysql と memcache のハンドルを初期化する Project::Connection::child_init を実行する必要があります。これにより、子プロセスが「GET /」を処理するときに、Project::Web::home モジュールが「Project::Connection を使用できる」ようになります。 " それらのハンドルを取得します。最後に、これらのハンドルは、子プロセス全体で定義されたままにする必要があります。
あなたが知りたいかもしれない余分な情報を分けてください:
ログから: "Apache/2.2.16 (Debian) mod_apreq2-20090110/2.7.1 mod_perl/2.0.4 Perl/v5.10.1 構成" /etc/apache2/sites-available/project から:
<VirtualHost *:80>
ServerAdmin admin@project.com
ServerName project.com
DocumentRoot "/var/www"
PerlRequire "/var/www/bin/startup.pl"
<Directory "/var/www/">
SetHandler "perl-script"
PerlInitHandler "Apache2::Reload"
PerlResponseHandler "Project"
PerlOptions -SetupEnv +ParseHeaders
Options ExecCGI
Order allow,deny
Allow from all
</Directory>
</VirtualHost>