CheckImageGenerationTasksCommand.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // 更新分镜表中的图片任务状态
  66. $this->updateSegmentPicStatus();
  67. } catch (\Exception $e) {
  68. dLog('generate')->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('generate')->info('开始更新分镜图片任务状态', ['count' => $segments->count()]);
  92. logDB('generate', 'info', '开始更新分镜图片任务状态', ['count' => $segments->count()]);
  93. $successCount = 0;
  94. $failedCount = 0;
  95. $processingCount = 0;
  96. foreach ($segments as $segment) {
  97. $picTaskId = $segment->pic_task_id;
  98. $segmentId = $segment->segment_id;
  99. // 查询图片生成任务状态
  100. $picTask = DB::table('mp_generate_pic_tasks')
  101. ->where('id', $picTaskId)
  102. ->select('id', 'status', 'result_url', 'error_message')
  103. ->first();
  104. if (!$picTask) {
  105. dLog('generate')->warning('分镜关联的图片任务不存在', [
  106. 'segment_id' => $segmentId,
  107. 'pic_task_id' => $picTaskId
  108. ]);
  109. continue;
  110. }
  111. $updateData = ['updated_at' => date('Y-m-d H:i:s')];
  112. // 根据任务状态更新分镜表
  113. if ($picTask->status === 'success') {
  114. $updateData['pic_task_status'] = '已完成';
  115. // 处理 result_url(可能是JSON数组或字符串)
  116. $resultUrl = $picTask->result_url;
  117. if (is_string($resultUrl) && is_json($resultUrl)) {
  118. $resultUrls = json_decode($resultUrl, true);
  119. $updateData['img_url'] = is_array($resultUrls) ? $resultUrls[0] : $resultUrl;
  120. } else {
  121. $updateData['img_url'] = $resultUrl;
  122. }
  123. $successCount++;
  124. dLog('generate')->info('分镜图片生成成功', [
  125. 'segment_id' => $segmentId,
  126. 'pic_task_id' => $picTaskId,
  127. 'img_url' => $updateData['img_url']
  128. ]);
  129. logDB('generate', 'info', '分镜图片生成成功', [
  130. 'segment_id' => $segmentId,
  131. 'pic_task_id' => $picTaskId
  132. ]);
  133. } elseif ($picTask->status === 'failed') {
  134. $updateData['pic_task_status'] = '失败';
  135. $failedCount++;
  136. dLog('generate')->error('分镜图片生成失败', [
  137. 'segment_id' => $segmentId,
  138. 'pic_task_id' => $picTaskId,
  139. 'error' => $picTask->error_message
  140. ]);
  141. logDB('generate', 'error', '分镜图片生成失败', [
  142. 'segment_id' => $segmentId,
  143. 'pic_task_id' => $picTaskId,
  144. 'error' => $picTask->error_message
  145. ]);
  146. } else {
  147. // 仍在处理中,不更新
  148. $processingCount++;
  149. continue;
  150. }
  151. // 更新分镜表
  152. DB::table('mp_episode_segments')
  153. ->where('segment_id', $segmentId)
  154. ->update($updateData);
  155. }
  156. dLog('generate')->info('分镜图片任务状态更新完成', [
  157. 'total' => $segments->count(),
  158. 'success' => $successCount,
  159. 'failed' => $failedCount,
  160. 'processing' => $processingCount
  161. ]);
  162. logDB('generate', 'info', '分镜图片任务状态更新完成', [
  163. 'total' => $segments->count(),
  164. 'success' => $successCount,
  165. 'failed' => $failedCount,
  166. 'processing' => $processingCount
  167. ]);
  168. } catch (\Exception $e) {
  169. dLog('generate')->error('更新分镜图片任务状态异常: ' . $e->getMessage());
  170. logDB('generate', 'error', '更新分镜图片任务状态异常', [
  171. 'error' => $e->getMessage(),
  172. 'trace' => $e->getTraceAsString()
  173. ]);
  174. }
  175. }
  176. }