message_data = $message_data; } /** * Execute the job. * * @return void */ public function handle() { $list_data = $this->message_data['data']; if (empty($list_data)) return true; try { // 分批处理,每批 50-100 条 $chunks = array_chunk($list_data, 50); foreach ($chunks as $chunkIndex => $chunk) { foreach ($chunk as $key => $item) { $globalIndex = $chunkIndex * 50 + $key; $item['queue_now_limit'] = (int)$globalIndex + 1; $item['queue_page'] = $this->message_data['queue_page']; $item['queue_limit'] = $this->message_data['queue_limit']; $item['queue_total'] = $this->message_data['queue_total']; // 使用延迟分发,避免一次性创建太多任务 JdTmaoProductDataJobs::dispatch($item) ->delay(now()->addSeconds($globalIndex * 0.1)); // 每 0.1 秒分发一个 unset($item); } // 处理完一批后强制垃圾回收 gc_collect_cycles(); // 可选:添加短暂延迟 if (count($chunks) > 1 && $chunkIndex < count($chunks) - 1) { usleep(100000); // 0.1 秒 } } } catch (\Exception $e) { Log::info('job_error', '采集数据-药师帮店铺数据同步队列失败', ['error' => $e->getMessage()]); } } public function failed(\Throwable $exception) { Log::info('job_error', '采集数据-药师帮店铺数据同步队列完全失败', ['data' => $this->message_data, 'error' => $exception]); } }