-- 실패한 배치 Job과 Step을 ABANDONED 상태로 변경
-- 주의: 이 스크립트는 실패한 job을 강제로 종료시킵니다.
-- 재시도가 필요한 경우 이 스크립트를 실행하지 마세요.

-- 1. 현재 실패 상태 확인
SELECT
    '=== BEFORE UPDATE ===' as status,
    COUNT(*) as failed_jobs
FROM BATCH_JOB_EXECUTION
WHERE STATUS = 'FAILED';

SELECT
    '=== BEFORE UPDATE ===' as status,
    COUNT(*) as failed_steps
FROM BATCH_STEP_EXECUTION
WHERE STATUS = 'FAILED';

-- 2. 실패한 STEP을 ABANDONED로 변경
UPDATE BATCH_STEP_EXECUTION
SET
    STATUS = 'ABANDONED',
    EXIT_CODE = 'ABANDONED',
    EXIT_MESSAGE = 'Manually marked as ABANDONED - Original status: FAILED',
    END_TIME = COALESCE(END_TIME, CURRENT_TIMESTAMP),
    LAST_UPDATED = CURRENT_TIMESTAMP
WHERE STATUS = 'FAILED';

-- 3. 실패한 JOB을 ABANDONED로 변경
UPDATE BATCH_JOB_EXECUTION
SET
    STATUS = 'ABANDONED',
    EXIT_CODE = 'ABANDONED',
    EXIT_MESSAGE = 'Manually marked as ABANDONED - Original status: FAILED',
    END_TIME = COALESCE(END_TIME, CURRENT_TIMESTAMP),
    LAST_UPDATED = CURRENT_TIMESTAMP
WHERE STATUS = 'FAILED';

-- 4. 업데이트 후 상태 확인
SELECT
    '=== AFTER UPDATE ===' as status,
    COUNT(*) as failed_jobs
FROM BATCH_JOB_EXECUTION
WHERE STATUS = 'FAILED';

SELECT
    '=== AFTER UPDATE ===' as status,
    COUNT(*) as failed_steps
FROM BATCH_STEP_EXECUTION
WHERE STATUS = 'FAILED';

SELECT
    '=== ABANDONED COUNT ===' as status,
    COUNT(*) as abandoned_jobs
FROM BATCH_JOB_EXECUTION
WHERE STATUS = 'ABANDONED';

SELECT
    '=== ABANDONED COUNT ===' as status,
    COUNT(*) as abandoned_steps
FROM BATCH_STEP_EXECUTION
WHERE STATUS = 'ABANDONED';

-- 5. 최근 ABANDONED 처리된 Job 목록 확인
SELECT
    JOB_EXECUTION_ID,
    JOB_INSTANCE_ID,
    START_TIME,
    END_TIME,
    STATUS,
    EXIT_CODE,
    EXIT_MESSAGE
FROM BATCH_JOB_EXECUTION
WHERE STATUS = 'ABANDONED'
ORDER BY JOB_EXECUTION_ID DESC
LIMIT 10;
