| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Console\Commands;
- use App\Services\AIGeneration\AIImageGenerationService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use App\Consts\BaseConst;
- class CheckImageGenerationTasksCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'AIGeneration:checkImgTasks';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '检查图片生成任务状态并更新';
- /**
- * Execute the console command.
- *
- * @param AIImageGenerationService $aiImageGenerationService
- * @return int
- */
- public function handle(AIImageGenerationService $aiImageGenerationService)
- {
- dLog('generate')->info('开始检查图片生成任务状态...');
- // 执行50s
- $time_start = time();
- try {
- // 统计需要处理的任务数量(即梦AI的processing + 火山API的pending和processing)
- $jimengProcessingCount = DB::table('mp_generate_pic_tasks')
- ->where('status', 'processing')
- ->where('model', 'jimeng_4.0')
- ->count('id');
-
- $volcPendingCount = DB::table('mp_generate_pic_tasks')
- ->where('status', 'pending')
- ->whereIn('model', BaseConst::VOLC_PIC_MODELS)
- ->count('id');
-
- $totalCount = $jimengProcessingCount + $volcPendingCount;
-
- dLog('generate')->info("待处理任务统计 - 即梦AI处理中: {$jimengProcessingCount}, 火山API待处理: {$volcPendingCount}");
-
- while ($totalCount > 0) {
- $time_diff = time() - $time_start;
- if ($time_diff > 50) {
- dLog('generate')->info('定时任务执行超过50秒,退出循环');
- break;
- }
-
- sleep(3);
-
- // 执行任务更新
- $aiImageGenerationService->updatePendingTasks();
-
- // 重新统计任务数量
- $jimengProcessingCount = DB::table('mp_generate_pic_tasks')
- ->where('status', 'processing')
- ->where('model', 'jimeng_4.0')
- ->count('id');
-
- $volcPendingCount = DB::table('mp_generate_pic_tasks')
- ->where('status', 'pending')
- ->whereIn('model', BaseConst::VOLC_PIC_MODELS)
- ->count('id');
-
- $totalCount = $jimengProcessingCount + $volcPendingCount;
- }
-
- dLog('generate')->info('任务状态检查完成');
- } catch (\Exception $e) {
- dLog('generate')->error('任务状态检查失败: ' . $e->getMessage());
- return 1;
- }
- return 0;
- }
- }
|