php - 如何处理有错误的 Beanstalkd 作业

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

最佳答案

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

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

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

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

如果您想尝试为您的对象定义一个自己的错误处理程序,我之前通过为您的类设置一个自定义错误处理程序来完成类似的事情。尝试通过 $errcontext 获取 pheanstalk (job) 对象可能是一个丑陋的 hack - 但可能是尝试的东西..如果你想尝试一下,我会快速整理一些伪代码(创建一个子类来避免将代码放入 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);
                }
            }
        }
    }
}

关于php - 如何处理有错误的 Beanstalkd 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15871801/

相关文章:

javascript - 如何将24小时格式的日期时间转换为12小时格式

Erlang队列问题

laravel - 在 VueJS 组件中使用 sass 变量

php - Laravel 获取 Service Provider 的 url 参数

javascript - 基于Javascript Condition渲染 Blade 模板

php - Cakephp不明索引问题

php - 如何通过文本验证将值从 html 页面发送到 php 页面

php - mysql查询循环,结果存储在数组中

laravel - 作业尝试次数过多或运行时间过长

c# - 如何(以及是否)使用 TPL 编写单一消费者队列?