Laravel 8 分發(fā)批處理

2021-07-06 11:30 更新

要分發(fā)任務(wù),你應(yīng)該使用 Bus facade 的 batch 方法。當(dāng)然,你可以和批處理的回調(diào)結(jié)合使用。因此,你可以使用 then ,catchfinally 方法來定義批處理的回調(diào),這些回調(diào)中每一個在調(diào)用時都會接收到一個 Illuminate\Bus\Batch 實例:

use App\Jobs\ProcessPodcast;
use App\Podcast;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Batch;
use Throwable;

$batch = Bus::batch([
    new ProcessPodcast(Podcast::find(1)),
    new ProcessPodcast(Podcast::find(2)),
    new ProcessPodcast(Podcast::find(3)),
    new ProcessPodcast(Podcast::find(4)),
    new ProcessPodcast(Podcast::find(5)),
])->then(function (Batch $batch) {
    // 所有任務(wù)已成功完成...
})->catch(function (Batch $batch, Throwable $e) {
    // 檢測到第一個失敗的任務(wù)...
})->finally(function (Batch $batch) {
    // 批處理執(zhí)行完畢...
})->dispatch();

return $batch->id; 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號