Просмотр исходного кода

新增检查资产图片生成结果的定时任务

lh 1 месяц назад
Родитель
Сommit
c5bafa4df8

+ 54 - 0
app/Console/Commands/CheckProductPicTasksCommand.php

@@ -0,0 +1,54 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Services\Anime\AnimeService;
+use Illuminate\Console\Command;
+
+class CheckProductPicTasksCommand extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Anime:checkProductPicTasks';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '检查产品图片生成任务状态并更新产品记录';
+
+    protected $animeService;
+
+    public function __construct(AnimeService $animeService)
+    {
+        parent::__construct();
+        $this->animeService = $animeService;
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        dLog('command')->info('====================开始检查产品图片生成任务状态====================');
+        $time_start = microtime(true);
+
+        $stats = $this->animeService->checkProductPicTasks();
+
+        $time_end = microtime(true);
+        $this->info('检查完成: ' . json_encode($stats, JSON_UNESCAPED_UNICODE));
+        dLog('command')->info('产品图片生成任务检查完成', [
+            'stats'     => $stats,
+            'cost_time' => round($time_end - $time_start, 2)
+        ]);
+        dLog('command')->info('====================结束检查产品图片生成任务状态====================');
+
+        return 0;
+    }
+}

+ 3 - 0
app/Console/Kernel.php

@@ -38,6 +38,9 @@ class Kernel extends ConsoleKernel
         // 检查动漫对话图片生成状态
         $schedule->command('Anime:checkImgUrl')->everyMinute();
 
+        // 检查产品图片生成任务状态
+        $schedule->command('Anime:checkProductPicTasks')->everyMinute()->withoutOverlapping(5);
+
         // 检查分镜音频生成状态
         $schedule->command('Segment:checkAudio')->everyMinute();
         

+ 104 - 0
app/Services/Anime/AnimeService.php

@@ -8998,6 +8998,110 @@ class AnimeService
     }
 
     /**
+     * 定时任务:检查产品图片生成任务状态并更新产品记录
+     * 扫描 mp_products 中 pic_task_id 非空且 pic_task_status 为生成中的记录,
+     * 通过 pic_task_id 查询 mp_generate_pic_tasks 获取生成结果,按结果更新状态
+     *
+     * @return array ['total' => 总数, 'success' => 成功数, 'failed' => 失败数, 'pending' => 待处理数]
+     */
+    public function checkProductPicTasks() {
+        // 查询所有生成中的产品图片任务
+        $products = DB::table('mp_products')
+            ->whereNotNull('pic_task_id')
+            ->where('pic_task_status', '生成中')
+            ->where('is_deleted', 0)
+            ->get();
+
+        $stats = [
+            'total'   => $products->count(),
+            'success' => 0,
+            'failed'  => 0,
+            'pending' => 0,
+        ];
+
+        foreach ($products as $product) {
+            try {
+                $task_id = $product->pic_task_id;
+
+                // 通过 pic_task_id 查询图片生成任务结果
+                $task = DB::table('mp_generate_pic_tasks')
+                    ->where('id', $task_id)
+                    ->first();
+
+                if ($task) {
+                    if ($task->status === 'success' && $task->result_url) {
+                        // 解析图片URL
+                        $result_urls = $task->result_url;
+                        if (is_string($result_urls)) {
+                            $result_urls = json_decode($result_urls, true);
+                        }
+                        $img_url = is_array($result_urls) ? ($result_urls[0] ?? $task->result_url) : $task->result_url;
+
+                        // 更新资产表状态
+                        DB::table('mp_products')
+                            ->where('id', $product->id)
+                            ->update([
+                                'pic_task_status' => '生成成功',
+                                'url'             => $img_url,
+                                'updated_at'      => now()
+                            ]);
+
+                        $stats['success']++;
+
+                        dLog('anime')->info('资产图片生成成功', [
+                            'product_id' => $product->id,
+                            'task_id'    => $task_id,
+                            'img_url'    => $img_url
+                        ]);
+                    } elseif ($task->status === 'failed') {
+                        // 更新资产表状态
+                        DB::table('mp_products')
+                            ->where('id', $product->id)
+                            ->update([
+                                'pic_task_status' => '生成失败',
+                                'updated_at'      => now()
+                            ]);
+
+                        $stats['failed']++;
+
+                        dLog('anime')->warning('资产图片生成失败', [
+                            'product_id' => $product->id,
+                            'task_id'    => $task_id,
+                            'error'      => $task->error_message ?? '未知错误'
+                        ]);
+                    } else {
+                        // 任务还在处理中
+                        $stats['pending']++;
+                    }
+                } else {
+                    // 任务不存在,标记为失败
+                    DB::table('mp_products')
+                        ->where('id', $product->id)
+                        ->update([
+                            'pic_task_status' => '生成失败',
+                            'updated_at'      => now()
+                        ]);
+
+                    $stats['failed']++;
+
+                    dLog('anime')->error('图片生成任务不存在', [
+                        'product_id' => $product->id,
+                        'task_id'    => $task_id
+                    ]);
+                }
+            } catch (\Exception $e) {
+                dLog('anime')->error('检查产品图片生成任务异常', [
+                    'product_id' => $product->id,
+                    'task_id'    => $product->pic_task_id ?? null,
+                    'error'      => $e->getMessage()
+                ]);
+            }
+        }
+
+        return $stats;
+    }
+
+    /**
      * 获取剧本关联的资产列表
      * @param array $data 请求参数
      * @return array