Laravel 8 定義批處理任務(wù)

2021-07-15 16:21 更新

要構(gòu)建可批處理的任務(wù),你應(yīng)該先 創(chuàng)建任務(wù),
然后將 Illuminate\Bus\Batchable trait 添加到任務(wù)類(lèi),該 trait 提供了對(duì) batch 方法的訪問(wèn),該方法可用于檢索任務(wù)當(dāng)前執(zhí)行的批處理:

<?php

namespace App\Jobs;

use App\Models\Podcast;
use App\Services\AudioProcessor;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessPodcast implements ShouldQueue
{
    use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * 執(zhí)行任務(wù)
     *
     * @return void
     */
    public function handle()
    {
        if ($this->batch()->cancelled()) {
            // 檢測(cè)到批處理取消...

            return;
        }

        // 批處理任務(wù)執(zhí)行...
    }
} 


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)