Laravel 隊列技巧:Fail、Retry 或者 Delay


Laravel

當創建隊列jobs、監聽器或訂閱服務器以推送到隊列中時,您可能會開始認為,一旦分派,隊列工作器決定如何處理您的邏輯就完全由您自己決定了。

嗯……並不是說你不能從作業內部與隊列工作器交互,但是通常情況下,哪怕你做了,也是沒必要的。

這個神奇的騷操作的出現是因為“InteractsWithQueue”這個trait。.當排隊作業正在從隊列中拉出, 這個 [CallQueuedListener](https://github.com/laravel/framework/blob/5.8/src/Illuminate/Events/CallQueuedListener.php#L90-L104) 會檢查它是否在使用 InteractsWithQueue trait, 如果是的話,框架會將底層的“隊列jobs”實例注入到內部。

這個 “任務” 實例類似於一個包裝了真正的 Job 類的驅動,其中包含隊列連接和嘗試等信息。


背景

我將以一個轉碼 Job 為例。 這是一個將廣播音頻文件轉換成192kbps MP3格式的任務。因為這是在自由轉碼隊列中設置的,所以它的作用有限。


檢查嘗試次數

attempts()是被調用的第一個方法, 顧名思義,它返回嘗試次數,一個隊列 job總是伴隨着一個attempt啟動。

此方法旨在與其他方法一起使用 ..., 類似 fail() 或者 release() (delay). 為了便於說明,我們將通知用戶第幾次重試: 每次我們嘗試在空閑隊列中轉換(轉換代碼)時,我們都會通知用戶我們正在第幾次重試,讓他可以選擇取消將來的轉換(轉換代碼)。

<?php
namespace App\Jobs;
use App\Podcast;
use Transcoder\Transcoder;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use App\Notifications\PodcastTranscoded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Notifications\RetyingPodcastTranscode;
class TranscodePodcast
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Transcoder Instance
     *
     * @var \App\Podcast
     */
    protected $podcast;
    /**
     * 創建一個新的轉碼podcast實例。
     *
     * @param \App\Podcast $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }
    /**
     * 執行隊列job.
     *
     * @param \Transcoder\Transcoder $podcast
     * @return void
     */
    public function handle(Transcoder $transcoder)
    {
        // 告訴用戶我們第幾次重試
        if ($this->attempts() > 1) {
            $this->podcast->publisher->notify(new RetryingPodcastTranscode($this->podcast, $this->attempts());
        }
        $transcoded = $this->transcoder->setFile($event->podcast)
            ->format('mp3')
            ->bitrate(192)
            ->start();
            

        // 將轉碼podcast與原始podcast關聯
        $this->podcast->transcode()->associate($transcoded);
        
        // 通知podcast的發布者他的podcast已經准備好了

        $this->publisher->notify(new PodcastTranscoded($this->podcast));
    }
}

告訴用戶我們將在第幾次時重試某些內容,這在邏輯預先失敗時很有用,讓用戶(或開發人員)檢查出了什么問題,但當然您可以做更多的事情。

就我個人而言,我喜歡在“Job作業”失敗后再這樣做,如果還有重試時間,告訴他我們稍后會重試當然,這個例子只是為了舉例說明。


刪除作業隊列 Job

第二個方法就是 delete(). 跟你猜想的一樣,您可以從隊列中刪除當前的“隊列 Job”。
當隊列 Job或偵聽器由於多種原因排隊后不應處理時,這將會很方便,例如,考慮一下這個場景:在轉碼發生之前,上傳podcast的發布者由於任何原因(比如TOS沖突)被停用,我們應該處理podcast。

我們將在前面的示例中添加該代碼:

<?php
namespace App\Jobs;
use App\Podcast;
use Transcoder\Transcoder;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use App\Notifications\PodcastTranscoded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Notifications\RetyingPodcastTranscode;
class TranscodePodcast
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Transcoder Instance
     *
     * @var \App\Podcast
     */
    protected $podcast;
    /**
     * 創建一個新的轉碼podcast實例。

     *
     * @param \App\Podcast $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }
    /**
     * 執行隊列 job.
     *
     * @param \Transcoder\Transcoder $podcast
     * @return void
     */
    public function handle(Transcoder $transcoder)
    {
        // 如果發布服務器已被停用,請刪除此隊列job
        if ($this->podcast->publisher->isDeactivated()) {
            $this->delete();
        }
        // 告訴用戶我們第幾次重試
        if ($this->attempts() > 1) {
            $this->podcast->publisher->notify(new RetryingPodcastTranscode($this->podcast, $this->attempts());
        }
        $transcoded = $this->transcoder->setFile($event->podcast)
            ->format('mp3')
            ->bitrate(192)
            ->start();
            
        // 將轉碼podcast與原始podcast關聯
        $this->podcast->transcode()->associate($transcoded);
        
        // 通知podcast的發布者他的podcast已經准備好了
        $this->publisher->notify(new PodcastTranscoded($this->podcast));
    }
}

如果需要刪除可能已刪除的模型上的作業,則可能需要
 設置 [$deleteWhenMissingModels](https://laravel.com/docs/5.8/queues#ignoring-missing-models) 為真 t避免處理不存在的東西。


失敗的隊列job

當您需要控制人為破壞邏輯時,這非常非常方便, 因為使用空的“return”語句會將“Job”標記為已成功完成。您可以強制使排隊的作業失敗,希望出現異常,允許處理程序在可能的情況下稍后重試。

這使您在作業失敗時可以更好地控制在任何情況下,也可以使用“failed()”方法, 它允許你 失敗后執行任何清潔操縱, 比如通知用戶或者刪除一些東西。

在此示例中,如果由於任何原因(如 CDN 關閉時)無法從存儲中檢索podcast ,則作業將失敗,並引發自定義異常。

<?php
namespace App\Jobs;
use App\Podcast;
use Transcoder\Transcoder;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use App\Exceptions\PodcastUnretrievable;
use App\Notifications\PodcastTranscoded;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Notifications\RetyingPodcastTranscode;
class TranscodePodcast
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * 轉碼器實例
     *
     * @var \App\Podcast
     */
    protected $podcast;
    /**
     * 創建一個新的轉碼Podcast實例。
     *
     * @param \App\Podcast $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }
    /**
     * 執行隊列 job.
     *
     * @param \Transcoder\Transcoder $podcast
     * @return void
     */
    public function handle(Transcoder $transcoder)
    {
        // 如果發布服務器已被停用,請刪除此隊列job
        if ($this->podcast->publisher->isDeactivated()) {
            $this->delete();
        }
        //如果podcast不能從storage存儲中檢索,我們就會失敗。
        if ($this->podcast->fileDoesntExists()) {
            $this->fail(new PodcastUnretrievable($this->podcast));
        }
        // 告訴用戶我們第幾次重試
        if ($this->attempts() > 1) {
            $this->podcast->publisher->notify(new RetryingPodcastTranscode($this->podcast, $this->attempts());
        }
        
        $transcoded = $this->transcoder->setFile($event->podcast)
                ->format('mp3')
                ->bitrate(192)
                ->start();
            
        // 將轉碼podcast與原始podcast關聯
        $this->podcast->transcode()->associate($transcoded);
        
        // 通知podcast的發布者他的podcast已經准備好了
        $this->publisher->notify(new PodcastTranscoded($this->podcast));
    }
}

現在,進入最后的方法。


釋放(延遲)隊列job

這可能是trait性狀的最有用的方法, 因為它可以讓你在未來進一步推動這項隊列job. 此方法用於
 隊列job速率限制.

除了速率限制之外,您還可以在某些不可用但希望在不久的將來使用它的情況下使用它同時,避免先發制人地失敗。

在最后一個示例中,我們將延遲轉碼以備稍后使用:如果轉碼器正在大量使用,我們將延遲轉碼5分鍾直到負載降低。

<?php
namespace App\Jobs;
use App\Podcast;
use Transcoder\Transcoder;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use App\Exceptions\PodcastUnretrievable;
use App\Notifications\PodcastTranscoded;
use Illuminate\Queue\InteractsWithQueue;
use App\Notifications\TranscoderHighUsage;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Notifications\RetyingPodcastTranscode;
class TranscodePodcast
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * Transcoder Instance
     *
     * @var \App\Podcast
     */
    protected $podcast;
    /**
     * 創建一個新的轉碼podcast實例。
     *
     * @param \App\Podcast $podcast
     * @return void
     */
    public function __construct(Podcast $podcast)
    {
        $this->podcast = $podcast;
    }
    /**
     * 執行隊列job.
     *
     * @param \Transcoder\Transcoder $podcast
     * @return void
     */
    public function handle(Transcoder $transcoder)
    {
        // 如果發布服務器已被停用,請刪除此隊列job
        if ($this->podcast->publisher->isDeactivated()) {
            $this->delete();
        }
        // 如果podcast不能從storage存儲中檢索,我們就會失敗。
        if ($this->podcast->fileDoesntExists()) {
            $this->fail(new PodcastUnretrievable($this->podcast));
        }
        
        // 如果轉碼器使用率很高,我們將
        // t延遲轉碼5分鍾. 否則我們可能會有拖延轉碼器進程的危險 
		// 它會把所有的轉碼子進程都記錄下來。
        if ($transcoder->getLoad()->isHigh()) {
            $delay = 60 * 5;
            $this->podcast->publisher->notify(new TranscoderHighUsage($this->podcast, $delay));
            $this->release($delay);
        }
        // 告訴用戶我們第幾次重試
        if ($this->attempts() > 1) {
            $this->podcast->publisher->notify(new RetryingPodcastTranscode($this->podcast, $this->attempts());
        }
        
        $transcoded = $this->transcoder->setFile($event->podcast)
                ->format('mp3')
                ->bitrate(192)
                ->start();
            
        // 將轉碼podcast與原始podcast關聯
        $this->podcast->transcode()->associate($transcoded);
        
        // 通知podcast的發布者他的podcast已經准備好了
        $this->publisher->notify(new PodcastTranscoded($this->podcast));
    }
}

我們可以使用一些特殊方法,例如,獲得分配給轉碼器的一些時隙,如果轉碼器時隙已滿,則延遲作業。
在排隊的工作中,你能做的就只有這些了。排隊愉快。

轉自:https://learnku.com/laravel/t/36062


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM