2017-08-03 79 views
4

如果作業失敗,它將被推回隊列。在再次處理工作時,有沒有辦法記住工作班中財產的價值?Laravel隊列 - 記住財產狀態?

例如:

class MailJob extends Job 
{ 
    public $tries = 3; 

    public $status; 


    public function __construct() 
    { 
     $this->status = false; // set to false 
    } 


    /** 
    * Execute the job. 
    */ 
    public function handle() 
    { 
     $this->status = true; 
     // Assume job has failed, it went back to the Queue. 
     // status should be true when this job start processing again 
    } 
} 
+0

我想你必須寫一些東西給數據庫以保存狀態並確保作業引用數據庫中的數據。 – haakym

+0

@haakym看起來只有這樣:( –

+0

@haakym的答案是正確的,因爲當你在隊列中輸出一個工作時,它與主要的PHP應用程序分開運行,所以你可以做的只是將失敗的工作記錄在db,並在下次運行期間通過獲取重要信息重新運行失敗的作業 – mchampaneri

回答

1

如果你想再次運行在同一時刻失敗的過程。你可以做這樣的事情。

這裏的對象在內存中,同時重新運行作業,所以數據將可用。

我還沒有運行它證實此事,但希望它會工作processFailedisConfirm的

class MailJob extends Job{ 
public $tries = 3; 
public $status; 


public function __construct() 
{ 
    $this->status = false; // set to false 
} 


/** 
* Execute the job. 
*/ 
public function handle() 
{ 
    $this->status = true; 
    // Assume job has failed, it went back to the Queue. 
    // status should be true when this job start processing again 

    $failed = processFailedisConfirm(); 

    if $failed == true && $this->tries > -1 { 
     $this->tries = $this->tries - 1; 
     $this->handel(); 
    } 
}} 

例子可以

public function processFailedisConfirm(){ 

    // Core Process to be done in the Job 
    $state = (Do Some Api Call); // Here just example, you may send email 
            // Or can do the core Job Process 
            // And depending on the Response of the 
            // Process return true or false 

    // Is Job failed or not ? 
    if ($state == "200"){ 
    return false; // Job is not failed 
    } else { 
    return true; // Job is failed 
} 

過程的邏輯失敗與否的操作depened你在做。正如我正在做一個API調用,如果我得到200的迴應我的過程是成功的。 否則進程失敗。 這只是一個例子,不同api的成功響應可以不同於由api設計者設計的。

+0

你應該顯示'processFailedisConfirm()的例子' –