[英]Laravel Queue - remembering property state?
如果作業失敗,它將被推回到隊列。 有沒有辦法在再次處理作業時記住作業類中的屬性值?
例如:
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
}
}
如果要在同一時刻再次重新運行失敗的進程。 你可以做這樣的事情。
在重新運行作業時,對象在內存中,因此數據可用。
我沒有通過運行驗證它,但希望它能工作
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();
}
}}
processFailedisConfirm的示例可以是
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
}
過程邏輯失敗與否取決於您正在進行的操作。 因為我正在進行api調用,如果我得到200的響應,我的過程是成功的。 否則進程失敗。 這個例子,不同api的成功響應可能與api設計師的desigend不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.