| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace App\Jobs\Manager\CollectData\Ysbang;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldBeUnique;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- use App\Facades\Servers\Logs\Log;
- use App\Jobs\Manager\CollectData\Ysbang\YsbangProductDataJobs;
- /**
- * 采集数据-药师帮店铺数据
- * @author 唐远望
- * @version 1.0
- * @date 2026-02-05
- */
- class YsbangStoreJobs implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $message_data;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(array $message_data)
- {
- $this->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'];
- // 使用延迟分发,避免一次性创建太多任务
- YsbangProductDataJobs::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]);
- }
- }
|