WWW::Mechanize::PhantomJS 当 运行 处于后台模式时,Mojolicious Lite 脚本中的代码不起作用

WWW::Mechanize::PhantomJS code in Mojolicious Lite script doesn't work when running in background mode

我有这个非常简单的 Mojolicious Lite 脚本:

#!/usr/bin/env perl

use v5.10;
use WWW::Mechanize::PhantomJS;
use Mojolicious::Lite;

my $mech = WWW::Mechanize::PhantomJS->new();

$mech->get('http://whosebug.com/');

get '/test' => sub {
    my $c = shift;
    $mech->get("https://whosebug.com/questions");
    $c->render(template => 'activity');
};

app->secrets(['test secret']);
app->start;


__DATA__

@@ activity.html.ep
<!DOCTYPE html>
<html>
  <head><title>Test</title></head>
  <body><h2>Test</h2></body>
</html>

当我在前台模式下使用 hypnotoad 启动它时 (hypnotoad -f ./script.pl),并访问 /test url -- 我得到了我的测试页面,并清理了日志:

[Fri Dec 11 18:00:23 2015] [info] Listening at "http://*:8080"
[Fri Dec 11 18:00:23 2015] [info] Manager 3011 started
[Fri Dec 11 18:00:23 2015] [info] Creating process id file "/home/username/pc_activity/demo_site/hypnotoad.pid"

当我使用后台模式 (hypnotoad ./script.pl) 启动它并访问 /test url 时——我得到 "something went very wrong" 错误页面并抛出了 raptor。

[Fri Dec 11 17:58:07 2015] [info] Listening at "http://*:8080"
[Fri Dec 11 17:58:07 2015] [info] Manager 2964 started
[Fri Dec 11 17:58:07 2015] [info] Creating process id file "/home/username/pc_activity/demo_site/hypnotoad.pid"
[Fri Dec 11 17:58:14 2015] [error] Error while executing command: get: Server returned error message Can't connect to localhost:8910

Connection refused at /usr/local/share/perl/5.18.2/LWP/Protocol/http.pm line 47, <DATA> line 49.
 instead of data at /usr/local/share/perl/5.18.2/Selenium/Remote/Driver.pm line 310.

事实证明,localhost:8910 是 PhanomJS 在 webdriver 模式下的默认设置 运行。它不在我的机器上。但是即使我启动它,然后访问 URL,我仍然得到一个错误:

[Fri Dec 11 18:41:01 2015] [error] Error while executing command: get: Server returned error message Variable Resource Not Found - {"headers":{"Accept":"application/json","Connection":"TE, close","Content-Length":"45","Content-Type":"application/json; charset=utf-8","Host":"localhost:8910","TE":"deflate,gzip;q=0.3","User-Agent":"libwww-perl/6.15"},"httpVersion":"1.1","method":"POST","post":"{\"url\":\"https://whosebug.com/questions\"}","url":"/session/98799e80-a060-11e5-8907-0b365878087d/url","urlParsed":{"anchor":"","query":"","file":"url","directory":"/session/98799e80-a060-11e5-8907-0b365878087d/","path":"/session/98799e80-a060-11e5-8907-0b365878087d/url","relative":"/session/98799e80-a060-11e5-8907-0b365878087d/url","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/session/98799e80-a060-11e5-8907-0b365878087d/url","queryKey":{},"chunks":["session","98799e80-a060-11e5-8907-0b365878087d","url"]}} instead of data at /usr/local/share/perl/5.18.2/Selenium/Remote/Driver.pm line 310.

我想,我不明白,为什么它 运行 在前台模式下,而不是在后台模式下。然后,我该怎么做才能使它 运行 处于后台模式?

它不会 运行 在后台模式下的原因是线程。在后台启动 hypnotoad 后,它会生成一些进程。

 664161507 46028     1   0 10:34AM ??         0:00.01 script.pl
 664161507 46029 46028   0 10:34AM ??         0:00.08 script.pl
 664161507 46030 46028   0 10:34AM ??         0:00.01 script.pl
 664161507 46031 46028   0 10:34AM ??         0:00.01 script.pl
 664161507 46032 46028   0 10:34AM ??         0:00.01 script.pl

这些将无法访问您在父进程中创建的 PhantomJS。我没有研究这种通信是如何完成的,但如果你想为你的员工共享 PhantomJS,你需要将其作为一项单独的服务。

如果您希望每个请求都有一个 PhantomJS,您可以在请求中对其进行初始化,但我并不一定鼓励这种方法:

 get '/test' => sub {
     my $c = shift;
     my $mech = WWW::Mechanize::PhantomJS->new();
     $mech->get('http://whosebug.com/');
     $mech->get("https://whosebug.com/questions");
     $c->render(template => 'activity');
 };