PHP Mink/Zombie-致命异常后处理挂起进程? [英] PHP Mink/Zombie - handling hanging processes after a fatal exception?

查看:74
本文介绍了PHP Mink/Zombie-致命异常后处理挂起进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将PHP脚本与Mink + Zombie驱动程序配合使用(安装在):

I'm using the PHP script with Mink+Zombie driver (install as on nodejs cannot find module 'zombie' with PHP mink) from here: PHP Mink/Zombie - page visit returns status code 0? ; re-posting for completeness:

<?php
$nodeModPath = "/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules";

# composer autoload:
require_once __DIR__ . '/vendor/autoload.php';

$URL = "https://demo.centreon.com/centreon";
$USERNAME = "admin";
$PASSWORD = "centreon";
$LFID = "useralias";
$PFID = "password";
$BFID = "submitLogin";


$zsrv = new \Behat\Mink\Driver\NodeJS\Server\ZombieServer();
$zsrv->setNodeModulesPath($nodeModPath . "/"); # needs to end with a trailing '/'
$driver = new \Behat\Mink\Driver\ZombieDriver( $zsrv );
$session = new \Behat\Mink\Session($driver);

// start the session
$session->start();
$session->setRequestHeader('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36');

$statcode = 0;
while ($statcode != 200) {
  $isleep = rand(2, 7); echo "sleeping $isleep sec...\n";
  sleep($isleep);
  $session->visit($URL);
  // $session->wait(20000, '(0 === jQuery.active)'); # Uncaught exception 'RuntimeException' with message 'Could not establish connection: Connection refused (111)'
  $session->wait(20000, '(browser.statusCode > 0)'); ### THIS makes things work?!
  $statcode = $session->getStatusCode();
  echo "  current URL: " . $session->getCurrentUrl() ."\n";
  echo "  status code: " . $statcode ."\n";
}

$page = $session->getPage();
$el_login = $page->findField($LFID);
$el_password = $page->findField($PFID);
$el_button = $page->find('xpath', '//*[@name="'.$BFID.'"]');//findById($BFID);//findField($BFID);

$el_login->setValue($USERNAME);
$el_password->setValue($PASSWORD);

echo "  pressing/clicking login button\n";
$el_button->click();

echo "Page URL after click: ". $session->getCurrentUrl() . "\n";
$page = $session->getPage();
?>

问题是,当脚本运行并单击登录时,Mink无法检测到该页面使用的某些JavaScript库,并且引发了异常:

The problem is, when the script runs, and does a login click, some JavaScript libraries that the page uses cannot be detected by Mink, and an exception is raised:

$ php test_php_mink.php 
sleeping 4 sec...
  current URL: https://demo.centreon.com/centreon/
  status code: 200
  pressing/clicking login button
PHP Fatal error:  Uncaught exception 'Behat\Mink\Exception\DriverException' with message 'Error while processing event 'click': "ReferenceError: Effect is not defined\n    at https://demo.centreon.com/centreon/include/common/javascript/modalbox.js:517:1\n    at Object.exports.runInContext (vm.js:44:17)\n    at window._evaluate (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/lib/document.js:253:75)\n    at Object.DOM.languageProcessors.javascript (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/lib/dom/scripts.js:26:12)\n    at define.proto._eval (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:1477:47)\n    at /home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/browser/resource-loader.js:32:22\n    at Object.item.check (/home/USERNAME/.nvm/versions/node/v4.0.0/lib/node_modules/zombie/node_modules/jsdom/lib/jsdom/level2/html.js:188:11)\n    at Object.item.check (/home in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/ZombieDriver.php on line 880

例外是ReferenceError: Effect is not defined,这是由于使用此javascript的页面导致的:

The exception is ReferenceError: Effect is not defined, and it is due to the page using this javascript:

<script type="text/javascript" src="./include/common/javascript/scriptaculous/scriptaculous.js?load=effects,dragdrop"></script>
...
    new Effect.toggle('header', 'appear', { afterFinish: function() {

但是,更大的问题是发生此崩溃之后,然后当我第二次运行脚本时,它几乎立即崩溃,这次是使用Error: listen EADDRINUSE 127.0.0.1:8124:

However, the even bigger problem is that after this crash happens, then when I run the script for a second time, it crashes almost immediately, this time with Error: listen EADDRINUSE 127.0.0.1:8124:

$ php test_php_mink.php 
PHP Fatal error:  Uncaught exception 'RuntimeException' with message 'Server process has been terminated: (1) [events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE 127.0.0.1:8124
    at Object.exports._errnoException (util.js:837:11)
    at exports._exceptionWithHostPort (util.js:860:20)
    at Server._listen2 (net.js:1231:14)
    at listen (net.js:1267:10)
    at net.js:1376:9
    at doNTCallback3 (node.js:440:9)
    at process._tickCallback (node.js:346:17)
    at Function.Module.runMain (module.js:473:11)
    at startup (node.js:117:18)
    at node.js:951:3
]' in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php:413
Stack trace:
#0 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php(306): Behat\Mink\Driver\NodeJS\Server->checkAvailability()
#1 /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/ZombieDriver. in /path/to/test_php_mink/vendor/behat/mink-zombie-driver/src/NodeJS/Server.php on line 413

在第一次崩溃之后,似乎仍然有mink个进程徘徊-实际上,有:

It seems that after the first crash, there are still mink processes hanging around - and indeed, there are:

$ pgrep -fl mink
7659 sh
7660 node

$ ps axf | grep mink
 7687 pts/0    S+     0:00  |       \_ grep --color=tty mink
 7659 pts/0    S      0:00 sh -c 'node' '/path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js'
 7660 pts/0    Sl     0:03  \_ node /path/to/test_php_mink/vendor/behat/mink-zombie-driver/bin/mink-zombie-server.js

在这一点上,如果我杀死了这些进程(例如,使用pkill -f mink),那么它们将从进程树中消失,然后我可以再次运行该脚本,其结果与OP中的第一次调用相同(并且那么我必须再次杀死进程,等等.

At this point, if I kill these processes - say, with pkill -f mink - then they disappear from the process tree, and then I can run the script again, with the same results as the first call in the OP (and then I have to kill processes again, etc).

什么是最好的/推荐的处理方式,所以我不必在脚本每次遇到致命异常时都手动终止进程?在Mink库中有什么可以允许的吗?

What would be the best/recommended way to handle this, so I don't have to manually kill processes, each time the script reaches a fatal exception? Is there something in the Mink library that would allow for this?

推荐答案

为避免致命异常,要做的第一件事是实施/使用具有适当错误处理的方法. 您可以创建一个方法来检查元素是否不为null以及是否引发自定义异常.

First thing to do in order to avoid fatal exceptions is to implement/use methods with proper error handling. You can create a method to check if the element is not null and if it is to throw a custom exception.

例如,在您的情况下,如果未找到元素,则 findField 方法将返回null,在这种情况下, setValue 将用于非对象,导致致命错误.

For example in your case findField method will return null if the element is not found, in this case setValue will e used on a non-object resulting in a fatal error.

这篇关于PHP Mink/Zombie-致命异常后处理挂起进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆