CheckImageGenerationTasksCommand.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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('command')->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('command')->info("待处理任务统计 - 即梦AI处理中: {$jimengProcessingCount}, 火山API待处理: {$volcPendingCount}");
  44. while ($totalCount > 0) {
  45. $time_diff = time() - $time_start;
  46. if ($time_diff > 50) {
  47. dLog('command')->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('command')->info('任务状态检查完成');
  65. // 更新分镜表中的图片任务状态
  66. $this->updateSegmentPicStatus();
  67. } catch (\Exception $e) {
  68. dLog('command')->error('任务状态检查失败: ' . $e->getMessage());
  69. return 1;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * 更新分镜表中的图片任务状态
  75. *
  76. * @return void
  77. */
  78. private function updateSegmentPicStatus()
  79. {
  80. try {
  81. // 获取所有状态为"生成中"的分镜
  82. $segments = DB::table('mp_episode_segments')
  83. ->where('pic_task_status', '生成中')
  84. ->whereNotNull('pic_task_id')
  85. ->where('pic_task_id', '<>', '')
  86. ->select('segment_id', 'pic_task_id')
  87. ->get();
  88. if ($segments->isEmpty()) {
  89. return;
  90. }
  91. dLog('command')->info('开始更新分镜图片任务状态', ['count' => $segments->count()]);
  92. $successCount = 0;
  93. $failedCount = 0;
  94. $processingCount = 0;
  95. foreach ($segments as $segment) {
  96. $picTaskId = $segment->pic_task_id;
  97. $segmentId = $segment->segment_id;
  98. // 查询图片生成任务状态
  99. $picTask = DB::table('mp_generate_pic_tasks')
  100. ->where('id', $picTaskId)
  101. ->select('id', 'status', 'result_url', 'error_message')
  102. ->first();
  103. if (!$picTask) {
  104. dLog('command')->warning('分镜关联的图片任务不存在', [
  105. 'segment_id' => $segmentId,
  106. 'pic_task_id' => $picTaskId
  107. ]);
  108. continue;
  109. }
  110. $updateData = ['updated_at' => date('Y-m-d H:i:s')];
  111. // 根据任务状态更新分镜表
  112. if ($picTask->status === 'success') {
  113. $updateData['pic_task_status'] = '已完成';
  114. // 处理 result_url(可能是JSON数组或字符串)
  115. $resultUrl = $picTask->result_url;
  116. if (is_string($resultUrl) && is_json($resultUrl)) {
  117. $resultUrls = json_decode($resultUrl, true);
  118. $updateData['img_url'] = is_array($resultUrls) ? $resultUrls[0] : $resultUrl;
  119. } else {
  120. $updateData['img_url'] = $resultUrl;
  121. }
  122. $successCount++;
  123. dLog('command')->info('分镜图片生成成功', [
  124. 'segment_id' => $segmentId,
  125. 'pic_task_id' => $picTaskId,
  126. 'img_url' => $updateData['img_url']
  127. ]);
  128. } elseif ($picTask->status === 'failed') {
  129. $updateData['pic_task_status'] = '失败';
  130. $failedCount++;
  131. dLog('command')->error('分镜图片生成失败', [
  132. 'segment_id' => $segmentId,
  133. 'pic_task_id' => $picTaskId,
  134. 'error' => $picTask->error_message
  135. ]);
  136. logDB('command', 'error', '分镜图片生成失败', [
  137. 'segment_id' => $segmentId,
  138. 'pic_task_id' => $picTaskId,
  139. 'error' => $picTask->error_message
  140. ]);
  141. } else {
  142. // 仍在处理中,不更新
  143. $processingCount++;
  144. continue;
  145. }
  146. // 更新分镜表
  147. DB::table('mp_episode_segments')
  148. ->where('segment_id', $segmentId)
  149. ->update($updateData);
  150. }
  151. dLog('command')->info('分镜图片任务状态更新完成', [
  152. 'total' => $segments->count(),
  153. 'success' => $successCount,
  154. 'failed' => $failedCount,
  155. 'processing' => $processingCount
  156. ]);
  157. } catch (\Exception $e) {
  158. dLog('command')->error('更新分镜图片任务状态异常: ' . $e->getMessage());
  159. logDB('command', 'error', '更新分镜图片任务状态异常', [
  160. 'error' => $e->getMessage(),
  161. 'trace' => $e->getTraceAsString()
  162. ]);
  163. }
  164. }
  165. }