阅读(4884)
赞(6)
Laravel 8 定义批处理任务
2021-07-06 09:25:04 更新
要构建可批处理的任务,你应该先 创建任务,
然后将 IlluminateBusBatchable
trait 添加到任务类,该 trait 提供了对 batch
方法的访问,该方法可用于检索任务当前执行的批处理:
<?php
namespace AppJobs;
use AppModelsPodcast;
use AppServicesAudioProcessor;
use IlluminateBusBatchable;
use IlluminateBusQueueable;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
class ProcessPodcast implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* 执行任务
*
* @return void
*/
public function handle()
{
if ($this->batch()->cancelled()) {
// 检测到批处理取消...
return;
}
// 批处理任务执行...
}
}