-- 修复卡住的任务（将长时间running的任务标记为失败）

-- 查看将要修复的任务
SELECT 
    id,
    user_id,
    task_type,
    status,
    created_at,
    updated_at,
    TIMESTAMPDIFF(MINUTE, updated_at, NOW()) as stuck_minutes,
    JSON_EXTRACT(payload, '$.song_title') as song_title
FROM nf_generation_tasks
WHERE status = 'running'
AND TIMESTAMPDIFF(MINUTE, updated_at, NOW()) > 10
ORDER BY id;

-- 确认后执行以下语句修复

-- 方案1: 标记为失败（推荐）
UPDATE nf_generation_tasks
SET 
    status = 'failed',
    error_msg = '任务处理超时，已自动标记为失败',
    updated_at = NOW()
WHERE status = 'running'
AND TIMESTAMPDIFF(MINUTE, updated_at, NOW()) > 10;

-- 查看修复结果
SELECT 
    id,
    status,
    error_msg,
    updated_at
FROM nf_generation_tasks
WHERE id IN (
    SELECT id FROM (
        SELECT id FROM nf_generation_tasks 
        WHERE error_msg LIKE '%超时%'
        ORDER BY id DESC
        LIMIT 10
    ) as tmp
);

-- 方案2: 重置为pending状态（让系统重试）
/*
UPDATE nf_generation_tasks
SET 
    status = 'pending',
    error_msg = NULL,
    updated_at = NOW()
WHERE status = 'running'
AND TIMESTAMPDIFF(MINUTE, updated_at, NOW()) > 10;
*/
