当我的 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 安装一个监视器,这在开发/测试您的应用程序时很有用。一些使用 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/