|
@@ -2529,18 +2529,13 @@ class AnimeService
|
|
|
|
|
|
|
|
// 检查角色任务状态
|
|
// 检查角色任务状态
|
|
|
foreach ($roles as $index => &$role) {
|
|
foreach ($roles as $index => &$role) {
|
|
|
- // 如果已经有 URL,跳过检查
|
|
|
|
|
- $existing_url = getProp($role, 'url');
|
|
|
|
|
- if (!empty($existing_url)) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
$task_id = getProp($role, 'task_id');
|
|
$task_id = getProp($role, 'task_id');
|
|
|
$task_status = getProp($role, 'task_status');
|
|
$task_status = getProp($role, 'task_status');
|
|
|
|
|
+ $existing_url = getProp($role, 'url');
|
|
|
|
|
|
|
|
- // 只处理有任务ID且状态为processing的角色
|
|
|
|
|
- if ($task_id && $task_status === 'processing') {
|
|
|
|
|
- $hasProcessing = true;
|
|
|
|
|
|
|
+ // 只处理有任务ID且状态为processing的角色,或者状态为processing但没有URL的角色
|
|
|
|
|
+ if ($task_id && ($task_status === 'processing' || (in_array($task_status, ['success', 'completed']) && empty($existing_url)))) {
|
|
|
|
|
+ $hasProcessing = ($task_status === 'processing');
|
|
|
|
|
|
|
|
// 查询任务状态
|
|
// 查询任务状态
|
|
|
$task = DB::table('mp_generate_pic_tasks')
|
|
$task = DB::table('mp_generate_pic_tasks')
|
|
@@ -2559,12 +2554,16 @@ class AnimeService
|
|
|
if ($status === 'success') {
|
|
if ($status === 'success') {
|
|
|
$result_url = getProp($task, 'result_url');
|
|
$result_url = getProp($task, 'result_url');
|
|
|
if ($result_url) {
|
|
if ($result_url) {
|
|
|
- $result_urls = json_decode($result_url, true);
|
|
|
|
|
- $role['url'] = is_array($result_urls) ? $result_urls[0] : $result_url;
|
|
|
|
|
|
|
+ $result_urls = is_json($result_url) ? json_decode($result_url, true) : [$result_url];
|
|
|
|
|
+ if (is_array($result_urls) && !empty($result_urls[0])) {
|
|
|
|
|
+ $role['url'] = $result_urls[0];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$updated = true;
|
|
$updated = true;
|
|
|
|
|
+ } else if ($status === 'processing') {
|
|
|
|
|
+ $hasProcessing = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -2572,18 +2571,13 @@ class AnimeService
|
|
|
|
|
|
|
|
// 检查场景任务状态
|
|
// 检查场景任务状态
|
|
|
foreach ($scenes as $index => &$scene) {
|
|
foreach ($scenes as $index => &$scene) {
|
|
|
- // 如果已经有 URL,跳过检查
|
|
|
|
|
- $existing_url = getProp($scene, 'url');
|
|
|
|
|
- if (!empty($existing_url)) {
|
|
|
|
|
- continue;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
$task_id = getProp($scene, 'task_id');
|
|
$task_id = getProp($scene, 'task_id');
|
|
|
$task_status = getProp($scene, 'task_status');
|
|
$task_status = getProp($scene, 'task_status');
|
|
|
|
|
+ $existing_url = getProp($scene, 'url');
|
|
|
|
|
|
|
|
- // 只处理有任务ID且状态为processing的场景
|
|
|
|
|
- if ($task_id && $task_status === 'processing') {
|
|
|
|
|
- $hasProcessing = true;
|
|
|
|
|
|
|
+ // 只处理有任务ID且状态为processing的场景,或者状态为success但没有URL的场景
|
|
|
|
|
+ if ($task_id && ($task_status === 'processing' || (in_array($task_status, ['success', 'completed']) && empty($existing_url)))) {
|
|
|
|
|
+ $hasProcessing = ($task_status === 'processing');
|
|
|
|
|
|
|
|
// 查询任务状态
|
|
// 查询任务状态
|
|
|
$task = DB::table('mp_generate_pic_tasks')
|
|
$task = DB::table('mp_generate_pic_tasks')
|
|
@@ -2602,12 +2596,16 @@ class AnimeService
|
|
|
if ($status === 'success') {
|
|
if ($status === 'success') {
|
|
|
$result_url = getProp($task, 'result_url');
|
|
$result_url = getProp($task, 'result_url');
|
|
|
if ($result_url) {
|
|
if ($result_url) {
|
|
|
- $result_urls = json_decode($result_url, true);
|
|
|
|
|
- $scene['url'] = is_array($result_urls) ? $result_urls[0] : $result_url;
|
|
|
|
|
|
|
+ $result_urls = is_json($result_url) ? json_decode($result_url, true) : [$result_url];
|
|
|
|
|
+ if (is_array($result_urls) && !empty($result_urls[0])) {
|
|
|
|
|
+ $scene['url'] = $result_urls[0];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$updated = true;
|
|
$updated = true;
|
|
|
|
|
+ } else if ($status === 'processing') {
|
|
|
|
|
+ $hasProcessing = true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -2645,11 +2643,73 @@ class AnimeService
|
|
|
} catch (\Exception $e) {
|
|
} catch (\Exception $e) {
|
|
|
dLog('anime')->error('更新角色/场景信息失败: ' . $e->getMessage());
|
|
dLog('anime')->error('更新角色/场景信息失败: ' . $e->getMessage());
|
|
|
}
|
|
}
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 即使哈希相同,如果有URL更新也要发送事件
|
|
|
|
|
+ $hasUrlUpdate = false;
|
|
|
|
|
+ foreach ($roles as $role) {
|
|
|
|
|
+ if (!empty(getProp($role, 'url')) && getProp($role, 'task_status') === 'success') {
|
|
|
|
|
+ $hasUrlUpdate = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!$hasUrlUpdate) {
|
|
|
|
|
+ foreach ($scenes as $scene) {
|
|
|
|
|
+ if (!empty(getProp($scene, 'url')) && getProp($scene, 'task_status') === 'success') {
|
|
|
|
|
+ $hasUrlUpdate = true;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($hasUrlUpdate) {
|
|
|
|
|
+ // 强制更新数据库和发送事件
|
|
|
|
|
+ try {
|
|
|
|
|
+ DB::table($table_name)->where($id_field, $id_value)->update([
|
|
|
|
|
+ 'roles' => json_encode($roles, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
+ 'scenes' => json_encode($scenes, JSON_UNESCAPED_UNICODE),
|
|
|
|
|
+ 'updated_at' => date('Y-m-d H:i:s')
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 更新缓存
|
|
|
|
|
+ Redis::setex($cacheKey, 600, $newHash);
|
|
|
|
|
+
|
|
|
|
|
+ // 发送更新事件
|
|
|
|
|
+ echo "data: " . json_encode([
|
|
|
|
|
+ 'type' => 'update',
|
|
|
|
|
+ 'data' => [
|
|
|
|
|
+ 'roles' => $roles,
|
|
|
|
|
+ 'scenes' => $scenes,
|
|
|
|
|
+ 'elapsed_time' => time() - $startTime
|
|
|
|
|
+ ]
|
|
|
|
|
+ ], JSON_UNESCAPED_UNICODE) . "\n\n";
|
|
|
|
|
+ ob_flush();
|
|
|
|
|
+ flush();
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ dLog('anime')->error('强制更新角色/场景信息失败: ' . $e->getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 如果所有任务都已完成,发送完成事件并退出
|
|
// 如果所有任务都已完成,发送完成事件并退出
|
|
|
if (!$hasProcessing) {
|
|
if (!$hasProcessing) {
|
|
|
|
|
+ // 查询数据库中的最终数据,确保返回最新的完整数据
|
|
|
|
|
+ try {
|
|
|
|
|
+ $finalSource = DB::table($table_name)->where($id_field, $id_value)->first();
|
|
|
|
|
+ if ($finalSource) {
|
|
|
|
|
+ $finalSource = (array)$finalSource;
|
|
|
|
|
+ $finalRoles = json_decode(getProp($finalSource, 'roles', '[]'), true) ?: [];
|
|
|
|
|
+ $finalScenes = json_decode(getProp($finalSource, 'scenes', '[]'), true) ?: [];
|
|
|
|
|
+
|
|
|
|
|
+ // 使用数据库中的最终数据
|
|
|
|
|
+ $roles = $finalRoles;
|
|
|
|
|
+ $scenes = $finalScenes;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ dLog('anime')->error('查询最终数据失败: ' . $e->getMessage());
|
|
|
|
|
+ // 如果查询失败,继续使用内存中的数据
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 清理缓存
|
|
// 清理缓存
|
|
|
Redis::del($cacheKey);
|
|
Redis::del($cacheKey);
|
|
|
|
|
|
|
@@ -2672,6 +2732,23 @@ class AnimeService
|
|
|
|
|
|
|
|
// 超时处理
|
|
// 超时处理
|
|
|
if (time() - $startTime >= $maxDuration) {
|
|
if (time() - $startTime >= $maxDuration) {
|
|
|
|
|
+ // 查询数据库中的最终数据,即使超时也要返回最新数据
|
|
|
|
|
+ try {
|
|
|
|
|
+ $finalSource = DB::table($table_name)->where($id_field, $id_value)->first();
|
|
|
|
|
+ if ($finalSource) {
|
|
|
|
|
+ $finalSource = (array)$finalSource;
|
|
|
|
|
+ $finalRoles = json_decode(getProp($finalSource, 'roles', '[]'), true) ?: [];
|
|
|
|
|
+ $finalScenes = json_decode(getProp($finalSource, 'scenes', '[]'), true) ?: [];
|
|
|
|
|
+
|
|
|
|
|
+ // 使用数据库中的最终数据
|
|
|
|
|
+ $roles = $finalRoles;
|
|
|
|
|
+ $scenes = $finalScenes;
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (\Exception $e) {
|
|
|
|
|
+ dLog('anime')->error('超时时查询最终数据失败: ' . $e->getMessage());
|
|
|
|
|
+ // 如果查询失败,继续使用内存中的数据
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 清理缓存
|
|
// 清理缓存
|
|
|
Redis::del($cacheKey);
|
|
Redis::del($cacheKey);
|
|
|
|
|
|