如何处理有错误的Beanstalkd作业

时间:2013-04-08 05:20:02

标签: php queue laravel laravel-4 beanstalkd

当我的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应该如何知道错误发生并将其标记为失败,以便再次重试?

2 个答案:

答案 0 :(得分:6)

为beanstalkd安装监视器,这在开发/测试应用程序时非常有用。一些使用PHP的替代方案:http://mnapoli.github.io/phpBeanstalkdAdmin/https://github.com/ptrofimov/beanstalk_console

至于处理错误,您可以为beanstalkd作业定义自己的错误处理程序,并在该处理程序中决定是否要:

  • 埋葬(将工作放在一边供以后检查)
  • 踢(把它放回队列)
  • 删除(删除它,如果这对您的应用程序是安全的)

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

如果你想尝试为你的对象定义一个自己的错误处理程序,我之前已经为你的类设置了一个自定义错误处理程序。尝试通过$ 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);
                }
            }
        }
    }
}

答案 1 :(得分: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