javascript 从 CLI 或 Web 调用时 PhantomJS 挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16657744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:32:13  来源:igfitidea点击:

PhantomJS hanging when called from CLI or Web

javascriptweb-scrapingphantomjs

提问by Danoweb

I'm trying to use phantomJS to capture a screenshot of a URL, however when I call phantomJS (from either the command line or web app) it hangs and seesm to never execute the "exit()" call. I can't seem to find any error messages and it remains running until I kill it. This is the JS file that is passed to the phantomjs command:

我正在尝试使用 phantomJS 来捕获 URL 的屏幕截图,但是当我调用 phantomJS(从命令行或 Web 应用程序)时,它挂起并且似乎永远不会执行“exit()”调用。我似乎找不到任何错误消息,并且在我杀死它之前它一直在运行。这是传递给phantomjs命令的JS文件:

var page = require('webpage').create();
var system = require('system');
var script_address = '';
var page_to_load = '';
var members_id = '';
var activities_id = '';
var folder_path = '';

if (system.args.length < 5) 
{
    console.log('Usage: phantom_activity_fax.js script_address page_to_load members_id activities_id folder_path');
    console.log('#Args: '+system.args.length);
    phantom.exit();
}//END IF SYSTEM.ARGS.LENGTH === 1

//ASSIGN OUR ARGUMENTS RECIEVED
script_address = system.args[0];
page_to_load = system.args[1];
members_id = system.args[2];
activities_id = system.args[3];
folder_path = system.args[4];

console.log(system.args[0]);
console.log(system.args[1]);
console.log(system.args[2]);
console.log(system.args[3]);
console.log(system.args[4]);

//OPEN OUR PAGE WITH THE VALUES PROVIDED
page.open(page_to_load, function () {
    console.log("Entering Anonymous Function, Beginning RENDER:\n");
    page.render(folder_path+members_id+'_'+activities_id+'.png');
    phantom.exit();
});

I see the values pushed to the console, but after that it just hangs :( I've tried the web inspector, but could not understand where to execute the __run() call, and didn't see any change when I added the debugger-autorun=yes to the call :(.

我看到推送到控制台的值,但之后它只是挂起 :( 我试过网络检查器,但无法理解在哪里执行 __run() 调用,并且在我添加调试器时没有看到任何变化-autorun=yes 调用 :(。

This is the output I get from the command line when it hangs (as root user):

这是我在命令行挂起时从命令行获得的输出(以 root 用户身份):

[root@wv-wellvibe2 faxes]# phantomjs /var/www/wv-wellvibe2-test/javascripts/phantom_activity_fax.js https://wv-wellvibe2-test/manual_scripts/phantom_js_test_page.php 397 0 /var/www/wv-wellvibe2-test/uploads/images/faxes/
/var/www/wv-wellvibe2-test/javascripts/phantom_activity_fax.js
https://wv-wellvibe2-test/manual_scripts/phantom_js_test_page.php
397
0
/var/www/wv-wellvibe2-test/uploads/images/faxes/

And this is the output I get when running it as my own user, but I don't see the image file in the designated folder (faxes):

这是我以自己的用户身份运行时得到的输出,但我在指定文件夹(传真)中看不到图像文件:

[user@wv-wellvibe2 ~]$ phantomjs /var/www/wv-wellvibe2-test/javascripts/phantom_activity_fax.js https://wv-wellvibe2-test/manual_scripts/phantom_js_test_page.php 397 0 /var/www/wv-wellvibe2-test/uploads/images/faxes/
/var/www/wv-wellvibe2-test/javascripts/phantom_activity_fax.js
https://wv-wellvibe2-test/manual_scripts/phantom_js_test_page.php
397
0
/var/www/wv-wellvibe2-test/uploads/images/faxes/
Entering Anonymous Function, Beginning RENDER:
[user@wv-wellvibe2 ~]$ 

Unfortunately, as I said, the command completed but did not save a .png in the faxes folder. Here is the permissions for that folder:

不幸的是,正如我所说,该命令已完成但未在传真文件夹中保存 .png。这是该文件夹的权限:

[root@wv-wellvibe2 faxes]# ls -la
total 12
drwxr-xr-x 3 root   apache 4096 May 16 15:31 .
drwxr-xr-x 5 apache apache 4096 May 16 14:14 ..
drwxr-xr-x 6 apache apache 4096 May 20 15:05 .svn

Please let me know if there is anything else I can provide! Thank you!

如果还有什么我可以提供的,请告诉我!谢谢!

(As requested here is the PHP script that calls the Phantom JS process)

(这里要求的是调用 Phantom JS 进程的 PHP 脚本)

header("Date: " . date('Y-m-d H:i:s'));
//GET THE SMARTY CONFIG
include_once $_SERVER['DOCUMENT_ROOT'] . "/smarty/configs/config.php";

//VARS USED LATER
$process_script = $_SERVER['DOCUMENT_ROOT'] . '/javascripts/phantom_activity_fax.js';
$page_to_load = 'https://' . $_SERVER['HTTP_HOST'] . '/manual_scripts/phantom_js_test_page.php';
$members_id = $_SESSION['members_id'];
$activities_id = 0;
$folder_path = $_SERVER['DOCUMENT_ROOT'] . 'uploads/images/faxes/';
$system_response = '';


$call = "phantomjs --remote-debugger-port=65534 --remote-debugger-autorun=yes " .  $process_script . " " . $page_to_load . " " . $members_id . " " . $activities_id . " " . $folder_path;

echo 'CallingSystemWith: ' . $call . '<br />';

try 
{
    $system_response = system($call);

    echo '<br />SystemResponse: ' . $system_response . '<hr />';
} catch (Exception $exc) {
    echo $exc->getTraceAsString();
}//END TRY / CATCH

(The page it tells PhantomJS to "scrape" is a simple PHP script that outtputs a print_r() of $_SESSION and $_REQUEST)

(它告诉 PhantomJS “抓取”的页面是一个简单的 PHP 脚本,它输出 $_SESSION 和 $_REQUEST 的 print_r())

回答by Cybermaxs

If something goes wrong in your script (such as in page.render), phantom.exit()will never be called. That's why phantomJs seems to hang.

如果您的脚本出现问题(例如在 中page.render),phantom.exit()将永远不会被调用。这就是 phantomJs 似乎挂起的原因。

Maybe there is an issue in page.renderbut I don't think so. The most common causes of hangs are unhandled exception.

也许有问题,page.render但我不这么认为。挂起的最常见原因是未处理的异常。

I will suggest you 4 things to investigate the issue :

我会建议你 4 件事来调查这个问题:

  • add an handler to phantom.onErrorand/or to page.onError
  • encapsulate your code in try/catch blocks (such as for page.render)
  • Once the page is loaded, there is no test on callback status. It's better to check the status
  • seems to freeze when calling page.render. Have you tried a simpler filename in the current directory ? Maybe the freeze is because of the security or invalid filename (invalid characters ?)
  • phantom.onError和/或page.onError添加处理程序
  • 将您的代码封装在 try/catch 块中(例如 for page.render
  • 页面加载后,不会对回调状态进行测试。最好检查一下状态
  • 调用时似乎冻结page.render。您是否尝试过在当前目录中使用更简单的文件名?也许冻结是因为安全或无效的文件名(无效字符?)

Hope this will help you

希望能帮到你

回答by bluestart83

Use :

利用 :

$phantomjs --debug=true rasterize.js http://... test.pdf

In rasterize.js add a timeout on ressource, that was my problem:

在 rasterize.js 在资源上添加超时,那是我的问题:

page.settings.resourceTimeout = 10000; // Avoid freeze!!!