2013-04-08 56 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