db status 조회로 변경 .
This commit is contained in:
1 parent
662b6b80bc
commit
e3ed840d12
3 files changed
+37
-65
No files matched your search
+26
-35
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user