ProcessImageGenerationQueueCommand.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\AIGeneration\AIImageGenerationService;
  4. use Illuminate\Console\Command;
  5. class ProcessImageGenerationQueueCommand extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'image-generation:process-queue';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '处理图片生成任务队列,检查任务状态并处理下一个排队任务';
  19. private $imageGenerationService;
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct(AIImageGenerationService $imageGenerationService)
  26. {
  27. parent::__construct();
  28. $this->imageGenerationService = $imageGenerationService;
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $this->info('开始处理图片生成任务队列...');
  38. try {
  39. // 更新所有待处理任务的状态
  40. $this->imageGenerationService->updatePendingTasks();
  41. // 获取队列状态
  42. $queueStatus = $this->imageGenerationService->getQueueStatus();
  43. $this->info("队列状态: 处理中任务数: {$queueStatus['processing_count']}, 排队任务数: {$queueStatus['pending_count']}");
  44. if ($queueStatus['can_submit_new_task'] && $queueStatus['pending_count'] > 0) {
  45. $this->info('开始处理下一个排队任务...');
  46. $this->imageGenerationService->processNextQueuedTask();
  47. }
  48. $this->info('任务队列处理完成');
  49. return 0;
  50. } catch (\Exception $e) {
  51. $this->error('处理任务队列时发生错误: ' . $e->getMessage());
  52. return 1;
  53. }
  54. }
  55. }