db status 조회로 변경 .
parent
662b6b80bc
commit
e3ed840d12
|
|
@ -22,7 +22,6 @@ type GenerationStatus = 'idle' | 'generating_lyric' | 'generating_song' | 'polli
|
|||
|
||||
interface SavedGenerationState {
|
||||
taskId: string;
|
||||
songId: string;
|
||||
lyrics: string;
|
||||
status: GenerationStatus;
|
||||
timestamp: number;
|
||||
|
|
@ -71,10 +70,9 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({
|
|||
const audioRef = useRef<HTMLAudioElement>(null);
|
||||
const languageDropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const saveToStorage = (taskId: string, songId: string, currentLyrics: string, currentStatus: GenerationStatus) => {
|
||||
const saveToStorage = (taskId: string, currentLyrics: string, currentStatus: GenerationStatus) => {
|
||||
const data: SavedGenerationState = {
|
||||
taskId,
|
||||
songId,
|
||||
lyrics: currentLyrics,
|
||||
status: currentStatus,
|
||||
timestamp: Date.now(),
|
||||
|
|
@ -114,7 +112,7 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({
|
|||
}
|
||||
setStatus('polling');
|
||||
setStatusMessage('노래를 생성하고 있습니다... (새로고침 후 복구됨)');
|
||||
resumePolling(savedState.taskId, savedState.songId, savedState.lyrics, 0);
|
||||
resumePolling(savedState.taskId, savedState.lyrics, 0);
|
||||
}
|
||||
}, []);
|
||||
|
||||
|
|
@ -146,16 +144,15 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({
|
|||
}
|
||||
}, [videoGenerationStatus, songTaskId, onNext]);
|
||||
|
||||
const resumePolling = async (taskId: string, songId: string, currentLyrics: string, currentRetryCount: number = 0) => {
|
||||
const resumePolling = async (taskId: string, currentLyrics: string, currentRetryCount: number = 0) => {
|
||||
try {
|
||||
const downloadResponse = await waitForSongComplete(
|
||||
taskId,
|
||||
songId,
|
||||
(pollStatus: string) => {
|
||||
if (pollStatus === 'pending') {
|
||||
setStatusMessage('노래를 생성하고 있습니다...');
|
||||
} else if (pollStatus === 'processing') {
|
||||
if (pollStatus === 'processing') {
|
||||
setStatusMessage('노래를 생성하고 있습니다...');
|
||||
} else if (pollStatus === 'uploading') {
|
||||
setStatusMessage('노래를 업로드하고 있습니다...');
|
||||
}
|
||||
}
|
||||
);
|
||||
|
|
@ -221,8 +218,8 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({
|
|||
throw new Error(songResponse.error_message || '음악 생성 요청에 실패했습니다.');
|
||||
}
|
||||
|
||||
saveToStorage(songResponse.task_id, songResponse.song_id, currentLyrics, 'polling');
|
||||
await resumePolling(songResponse.task_id, songResponse.song_id, currentLyrics, currentRetryCount);
|
||||
saveToStorage(songResponse.task_id, currentLyrics, 'polling');
|
||||
await resumePolling(songResponse.task_id, currentLyrics, currentRetryCount);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Song regeneration failed:', error);
|
||||
|
|
@ -396,9 +393,9 @@ const SoundStudioContent: React.FC<SoundStudioContentProps> = ({
|
|||
|
||||
setStatus('polling');
|
||||
setStatusMessage('노래를 생성하고 있습니다...');
|
||||
saveToStorage(songResponse.task_id, songResponse.song_id, lyricDetailResponse.lyric_result, 'polling');
|
||||
saveToStorage(songResponse.task_id, lyricDetailResponse.lyric_result, 'polling');
|
||||
|
||||
await resumePolling(songResponse.task_id, songResponse.song_id, lyricDetailResponse.lyric_result, 0);
|
||||
await resumePolling(songResponse.task_id, lyricDetailResponse.lyric_result, 0);
|
||||
|
||||
} catch (error) {
|
||||
console.error('Music generation failed:', error);
|
||||
|
|
|
|||
|
|
@ -83,23 +83,7 @@ export interface SongGenerateResponse {
|
|||
error_message: string | null;
|
||||
}
|
||||
|
||||
// 노래 상태 확인 응답
|
||||
export interface SongStatusResponse {
|
||||
success: boolean;
|
||||
status: string;
|
||||
message?: string;
|
||||
clips?: Array<{
|
||||
id: string;
|
||||
audio_url: string;
|
||||
stream_audio_url: string;
|
||||
image_url: string;
|
||||
title: string;
|
||||
status: string | null;
|
||||
duration: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
// 노래 다운로드 응답
|
||||
// 노래 다운로드 상태 조회 응답 (DB Polling)
|
||||
export interface SongDownloadResponse {
|
||||
success: boolean;
|
||||
status: string;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import {
|
|||
LyricDetailResponse,
|
||||
SongGenerateRequest,
|
||||
SongGenerateResponse,
|
||||
SongStatusResponse,
|
||||
SongDownloadResponse,
|
||||
VideoGenerateResponse,
|
||||
VideoStatusResponse,
|
||||
|
|
@ -154,21 +153,9 @@ export async function generateSong(taskId: string, request: SongGenerateRequest)
|
|||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 상태 확인 API (song_id 사용)
|
||||
export async function getSongStatus(songId: string): Promise<SongStatusResponse> {
|
||||
const response = await fetch(`${API_URL}/song/status/${songId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 다운로드 API
|
||||
export async function downloadSong(taskId: string): Promise<SongDownloadResponse> {
|
||||
// 노래 다운로드 상태 조회 API (DB Polling)
|
||||
// task_id를 기반으로 Song 테이블의 상태를 조회하고, completed인 경우 Project 정보와 노래 URL을 반환
|
||||
export async function getSongDownloadStatus(taskId: string): Promise<SongDownloadResponse> {
|
||||
const response = await fetch(`${API_URL}/song/download/${taskId}`, {
|
||||
method: 'GET',
|
||||
});
|
||||
|
|
@ -180,40 +167,44 @@ export async function downloadSong(taskId: string): Promise<SongDownloadResponse
|
|||
return response.json();
|
||||
}
|
||||
|
||||
// 노래 생성 완료까지 폴링 (2분 타임아웃)
|
||||
const POLL_TIMEOUT = 2 * 60 * 1000; // 2분
|
||||
const POLL_INTERVAL = 5000; // 5초
|
||||
// 노래 생성 완료까지 폴링 (5분 타임아웃, 3초 간격)
|
||||
// 새로운 API는 DB 상태를 직접 조회: processing, uploading, completed, failed, not_found, error
|
||||
const SONG_POLL_TIMEOUT = 5 * 60 * 1000; // 5분
|
||||
const SONG_POLL_INTERVAL = 3000; // 3초
|
||||
|
||||
export async function waitForSongComplete(
|
||||
taskId: string,
|
||||
songId: string,
|
||||
onStatusChange?: (status: string) => void
|
||||
): Promise<SongDownloadResponse> {
|
||||
const startTime = Date.now();
|
||||
|
||||
// 재귀적으로 폴링하는 방식으로 변경 (async/await 제대로 동작)
|
||||
const poll = async (): Promise<SongDownloadResponse> => {
|
||||
// 2분 타임아웃 체크
|
||||
if (Date.now() - startTime > POLL_TIMEOUT) {
|
||||
// 5분 타임아웃 체크
|
||||
if (Date.now() - startTime > SONG_POLL_TIMEOUT) {
|
||||
throw new Error('TIMEOUT');
|
||||
}
|
||||
|
||||
try {
|
||||
// 상태 확인은 song_id 사용
|
||||
const statusResponse = await getSongStatus(songId);
|
||||
onStatusChange?.(statusResponse.status);
|
||||
const response = await getSongDownloadStatus(taskId);
|
||||
onStatusChange?.(response.status);
|
||||
|
||||
// status가 "SUCCESS" (대문자)인 경우 완료
|
||||
if (statusResponse.status === 'SUCCESS' && statusResponse.success) {
|
||||
// 다운로드는 task_id 사용
|
||||
const downloadResponse = await downloadSong(taskId);
|
||||
return downloadResponse;
|
||||
} else if (statusResponse.status === 'FAILED' || statusResponse.status === 'failed') {
|
||||
throw new Error('Song generation failed');
|
||||
// completed: 모든 작업 완료, Blob URL 사용 가능
|
||||
if (response.status === 'completed' && response.success) {
|
||||
return response;
|
||||
}
|
||||
|
||||
// PENDING, PROCESSING 등은 대기 후 재시도
|
||||
await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL));
|
||||
// failed: 노래 생성 또는 업로드 실패
|
||||
if (response.status === 'failed' || response.status === 'error') {
|
||||
throw new Error(response.error_message || '노래 생성에 실패했습니다.');
|
||||
}
|
||||
|
||||
// not_found: task_id에 해당하는 Song 없음
|
||||
if (response.status === 'not_found') {
|
||||
throw new Error('노래를 찾을 수 없습니다.');
|
||||
}
|
||||
|
||||
// processing, uploading 등은 대기 후 재시도
|
||||
await new Promise(resolve => setTimeout(resolve, SONG_POLL_INTERVAL));
|
||||
return poll();
|
||||
} catch (error) {
|
||||
throw error;
|
||||
|
|
|
|||
Loading…
Reference in New Issue