CheckImageGenerationTasksCommand.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\AIGeneration\AIImageGenerationService;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\DB;
  6. use App\Consts\BaseConst;
  7. class CheckImageGenerationTasksCommand extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'AIGeneration:checkImgTasks';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '检查图片生成任务状态并更新';
  21. /**
  22. * Execute the console command.
  23. *
  24. * @param AIImageGenerationService $aiImageGenerationService
  25. * @return int
  26. */
  27. public function handle(AIImageGenerationService $aiImageGenerationService)
  28. {
  29. dLog('generate')->info('开始检查图片生成任务状态...');
  30. // 执行50s
  31. $time_start = time();
  32. try {
  33. // 统计需要处理的任务数量(即梦AI的processing + 火山API的pending和processing)
  34. $jimengProcessingCount = DB::table('mp_generate_pic_tasks')
  35. ->where('status', 'processing')
  36. ->where('model', 'jimeng_4.0')
  37. ->count('id');
  38. $volcPendingCount = DB::table('mp_generate_pic_tasks')
  39. ->where('status', 'pending')
  40. ->whereIn('model', BaseConst::VOLC_PIC_MODELS)
  41. ->count('id');
  42. $totalCount = $jimengProcessingCount + $volcPendingCount;
  43. dLog('generate')->info("待处理任务统计 - 即梦AI处理中: {$jimengProcessingCount}, 火山API待处理: {$volcPendingCount}");
  44. while ($totalCount > 0) {
  45. $time_diff = time() - $time_start;
  46. if ($time_diff > 50) {
  47. dLog('generate')->info('定时任务执行超过50秒,退出循环');
  48. break;
  49. }
  50. sleep(3);
  51. // 执行任务更新
  52. $aiImageGenerationService->updatePendingTasks();
  53. // 重新统计任务数量
  54. $jimengProcessingCount = DB::table('mp_generate_pic_tasks')
  55. ->where('status', 'processing')
  56. ->where('model', 'jimeng_4.0')
  57. ->count('id');
  58. $volcPendingCount = DB::table('mp_generate_pic_tasks')
  59. ->where('status', 'pending')
  60. ->whereIn('model', BaseConst::VOLC_PIC_MODELS)
  61. ->count('id');
  62. $totalCount = $jimengProcessingCount + $volcPendingCount;
  63. }
  64. dLog('generate')->info('任务状态检查完成');
  65. } catch (\Exception $e) {
  66. dLog('generate')->error('任务状态检查失败: ' . $e->getMessage());
  67. return 1;
  68. }
  69. return 0;
  70. }
  71. }