refactor(backend): 중복 코드 제거
- scheduler.js: handleSyncResult 함수 추출로 동기화 결과 처리 로직 통합 - youtube/index.js: 하드코딩된 카테고리 ID를 CATEGORY_IDS 상수로 변경 - 리팩토링 문서 업데이트 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
2f30c67b93
commit
7593004bd6
3 changed files with 36 additions and 34 deletions
|
|
@ -46,6 +46,27 @@ async function schedulerPlugin(fastify, opts) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 동기화 결과 처리 (중복 코드 제거)
|
||||||
|
*/
|
||||||
|
async function handleSyncResult(botId, result, options = {}) {
|
||||||
|
const { setRunningStatus = false, setErrorOnFail = false } = options;
|
||||||
|
const status = await getStatus(botId);
|
||||||
|
const updateData = {
|
||||||
|
lastCheckAt: new Date().toISOString(),
|
||||||
|
totalAdded: (status.totalAdded || 0) + result.addedCount,
|
||||||
|
};
|
||||||
|
if (setRunningStatus) {
|
||||||
|
updateData.status = 'running';
|
||||||
|
updateData.errorMessage = null;
|
||||||
|
}
|
||||||
|
if (result.addedCount > 0) {
|
||||||
|
updateData.lastAddedCount = result.addedCount;
|
||||||
|
}
|
||||||
|
await updateStatus(botId, updateData);
|
||||||
|
return result.addedCount;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 봇 시작
|
* 봇 시작
|
||||||
*/
|
*/
|
||||||
|
|
@ -71,19 +92,8 @@ async function schedulerPlugin(fastify, opts) {
|
||||||
fastify.log.info(`[${botId}] 동기화 시작`);
|
fastify.log.info(`[${botId}] 동기화 시작`);
|
||||||
try {
|
try {
|
||||||
const result = await syncFn(bot);
|
const result = await syncFn(bot);
|
||||||
const status = await getStatus(botId);
|
const addedCount = await handleSyncResult(botId, result, { setRunningStatus: true });
|
||||||
const updateData = {
|
fastify.log.info(`[${botId}] 동기화 완료: ${addedCount}개 추가`);
|
||||||
status: 'running',
|
|
||||||
lastCheckAt: new Date().toISOString(),
|
|
||||||
totalAdded: (status.totalAdded || 0) + result.addedCount,
|
|
||||||
errorMessage: null,
|
|
||||||
};
|
|
||||||
// 실제로 추가된 경우에만 lastAddedCount 업데이트
|
|
||||||
if (result.addedCount > 0) {
|
|
||||||
updateData.lastAddedCount = result.addedCount;
|
|
||||||
}
|
|
||||||
await updateStatus(botId, updateData);
|
|
||||||
fastify.log.info(`[${botId}] 동기화 완료: ${result.addedCount}개 추가`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await updateStatus(botId, {
|
await updateStatus(botId, {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
|
|
@ -101,17 +111,8 @@ async function schedulerPlugin(fastify, opts) {
|
||||||
// 즉시 1회 실행
|
// 즉시 1회 실행
|
||||||
try {
|
try {
|
||||||
const result = await syncFn(bot);
|
const result = await syncFn(bot);
|
||||||
const status = await getStatus(botId);
|
const addedCount = await handleSyncResult(botId, result);
|
||||||
const updateData = {
|
fastify.log.info(`[${botId}] 초기 동기화 완료: ${addedCount}개 추가`);
|
||||||
lastCheckAt: new Date().toISOString(),
|
|
||||||
totalAdded: (status.totalAdded || 0) + result.addedCount,
|
|
||||||
};
|
|
||||||
// 실제로 추가된 경우에만 lastAddedCount 업데이트
|
|
||||||
if (result.addedCount > 0) {
|
|
||||||
updateData.lastAddedCount = result.addedCount;
|
|
||||||
}
|
|
||||||
await updateStatus(botId, updateData);
|
|
||||||
fastify.log.info(`[${botId}] 초기 동기화 완료: ${result.addedCount}개 추가`);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
fastify.log.error(`[${botId}] 초기 동기화 오류: ${err.message}`);
|
fastify.log.error(`[${botId}] 초기 동기화 오류: ${err.message}`);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,9 @@
|
||||||
import fp from 'fastify-plugin';
|
import fp from 'fastify-plugin';
|
||||||
import { fetchRecentVideos, fetchAllVideos, getUploadsPlaylistId } from './api.js';
|
import { fetchRecentVideos, fetchAllVideos, getUploadsPlaylistId } from './api.js';
|
||||||
import bots from '../../config/bots.js';
|
import bots from '../../config/bots.js';
|
||||||
|
import { CATEGORY_IDS } from '../../config/index.js';
|
||||||
|
|
||||||
const YOUTUBE_CATEGORY_ID = 2;
|
const YOUTUBE_CATEGORY_ID = CATEGORY_IDS.YOUTUBE;
|
||||||
const PLAYLIST_CACHE_PREFIX = 'yt_uploads:';
|
const PLAYLIST_CACHE_PREFIX = 'yt_uploads:';
|
||||||
|
|
||||||
async function youtubeBotPlugin(fastify, opts) {
|
async function youtubeBotPlugin(fastify, opts) {
|
||||||
|
|
|
||||||
|
|
@ -58,15 +58,15 @@
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
### 5단계: 중복 코드 제거
|
### 5단계: 중복 코드 제거 ✅ 완료
|
||||||
- [ ] 멤버 이름 매핑 유틸리티 생성
|
- [x] 스케줄러 상태 업데이트 로직 통합 (handleSyncResult 함수)
|
||||||
- [ ] 이미지 업로드 공통 함수 생성
|
- [x] youtube/index.js 하드코딩된 카테고리 ID → config 사용
|
||||||
- [ ] 스케줄러 상태 업데이트 로직 통합
|
- [ ] 멤버 이름 매핑 유틸리티 (기존 코드가 충분히 분리됨)
|
||||||
|
- [ ] 이미지 업로드 공통 함수 (기존 코드가 충분히 분리됨)
|
||||||
|
|
||||||
**관련 파일:**
|
**수정된 파일:**
|
||||||
- `src/services/image.js`
|
- `src/plugins/scheduler.js` - handleSyncResult 함수 추출로 중복 제거
|
||||||
- `src/services/youtube/index.js`
|
- `src/services/youtube/index.js` - CATEGORY_IDS.YOUTUBE 사용
|
||||||
- `src/plugins/scheduler.js`
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
@ -78,7 +78,7 @@
|
||||||
| 2단계 | N+1 쿼리 최적화 | ✅ 완료 |
|
| 2단계 | N+1 쿼리 최적화 | ✅ 완료 |
|
||||||
| 3단계 | 서비스 레이어 분리 | ✅ 완료 |
|
| 3단계 | 서비스 레이어 분리 | ✅ 완료 |
|
||||||
| 4단계 | 에러 처리 통일 | ✅ 완료 |
|
| 4단계 | 에러 처리 통일 | ✅ 완료 |
|
||||||
| 5단계 | 중복 코드 제거 | 대기 |
|
| 5단계 | 중복 코드 제거 | ✅ 완료 |
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue