2013-04-08 58 views
4

当我的Beanstalkd作业出现错误时,如"exception 'ErrorException' with message 'Notice: Undefined index: id in /var/www/mysite/app/libraries/lib.php line 248' in /var/www/mysite/app/libraries/lib.php:248,Beanstalkd应该如何知道发生了错误并将其标记为失败,以便可以再次重试?如何处理发生错误的Beanstalkd作业

+0

这是不是错误它是通知。当您尝试使用未分配的变量时会发生这种情况。 – 2013-04-08 05:24:27

+0

在这种情况下,函数将退出,但有一个例外。工作是否已完成?或者返回到就绪状态? – Nyxynyx 2013-04-08 05:28:05

回答

6

在开发/测试您的应用程序时,安装beanstalkd监视器可能很有用。使用PHP的一些替代品:http://mnapoli.github.io/phpBeanstalkdAdmin/https://github.com/ptrofimov/beanstalk_console

至于处理错误,你可以定义自己的错误处理程序beanstalkd工作,在该处理程序,如果你想决定:

  • 埋葬(把工作搁置后检查)
  • 踢(把它放回队列)
  • 删除(删除它,如果这是安全的,您的应用程序)

编辑 - 您是否设法解决您的问题? 最好的方法可能是在工作中使用try/catch来捕捉异常,然后在工作人员提出异常时将其埋掉。如果在生产者中引发异常,它可能永远不会被添加到队列中,因此不需要埋葬()然后使用监视器来确定。

如果你想尝试为你的对象定义一个自己的错误处理程序,我之前通过为你的类设置了一个自定义的错误处理程序来做类似的事情。这可能是一个丑陋的黑客试图通过$ errcontext获得pheanstalk(工作)对象 - 但可能是尝试的东西..这里是一些伪代码,如果你想尝试一下,我很快就把它们放在一起(创建一个子类来避免将代码放入pheanstalk类中):

class MyPheanstalk extends Pheanstalk { 

    function __construct() { 
     //register your custom error_handler for objects of this class 
     set_error_handler(array($this, 'myPheanstalk_error_handler')); 

     //call parent constructor 
     parent::__construct(); 
    } 

    function myPheanstalk_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { 
     // get the current job that failed 
     foreach($errcontext as $val) //print_r($errcontext) to find info on the object(job) you are looking for 
     { 
      if(is_object($val)) { 
       if(get_class($val) == 'Pheanstalk') { //and replace with correct class here 
        //optionally check errstr to decide if you want to delete() or kick() instead of bury() 
        $this->bury($val); 
       } 
      } 
     } 
    } 
} 
0

它的脚本有错误,而不是beanstalkd。

try { 

//your code from Line 248 here 

} catch (ErrorException $e) { //this section catches the error. 

//you can print the error 
echo 'An error occurred'. $e->getMessage(); 
//or do something else like try reporting the ID is bad. 

if (!isset($id)) { 
echo 'The id was not set!'; 
} 


} //end of try/catch