CheckImageGenerationTasksCommand.php 7.8 KB

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