fix: 봇 스케줄러 botId 타입 불일치 수정

- registerBot, unregisterBot, isBotRunning에서 botId를 parseInt()로 정수 변환
- URL 파라미터(문자열)와 DB 조회 결과(숫자) 간 타입 일치 문제 해결
- 10초 상태 동기화에서 메모리 스케줄러를 찾지 못해 stopped로 변경되던 버그 수정
This commit is contained in:
caadiq 2026-01-09 19:31:11 +09:00
parent fc35a35987
commit b161f1a9f7

View file

@ -9,41 +9,44 @@ const schedulers = new Map();
* 봇이 메모리에서 실행 중인지 확인 * 봇이 메모리에서 실행 중인지 확인
*/ */
export function isBotRunning(botId) { export function isBotRunning(botId) {
return schedulers.has(botId); const id = parseInt(botId);
return schedulers.has(id);
} }
/** /**
* 개별 스케줄 등록 * 개별 스케줄 등록
*/ */
export function registerBot(botId, intervalMinutes = 2, cronExpression = null) { export function registerBot(botId, intervalMinutes = 2, cronExpression = null) {
const id = parseInt(botId);
// 기존 스케줄이 있으면 제거 // 기존 스케줄이 있으면 제거
unregisterBot(botId); unregisterBot(id);
// cron 표현식: 지정된 표현식 사용, 없으면 기본값 생성 // cron 표현식: 지정된 표현식 사용, 없으면 기본값 생성
const expression = cronExpression || `1-59/${intervalMinutes} * * * *`; const expression = cronExpression || `1-59/${intervalMinutes} * * * *`;
const task = cron.schedule(expression, async () => { const task = cron.schedule(expression, async () => {
console.log(`[Bot ${botId}] 동기화 시작...`); console.log(`[Bot ${id}] 동기화 시작...`);
try { try {
const result = await syncNewVideos(botId); const result = await syncNewVideos(id);
console.log(`[Bot ${botId}] 동기화 완료: ${result.addedCount}개 추가`); console.log(`[Bot ${id}] 동기화 완료: ${result.addedCount}개 추가`);
} catch (error) { } catch (error) {
console.error(`[Bot ${botId}] 동기화 오류:`, error.message); console.error(`[Bot ${id}] 동기화 오류:`, error.message);
} }
}); });
schedulers.set(botId, task); schedulers.set(id, task);
console.log(`[Bot ${botId}] 스케줄 등록됨 (cron: ${expression})`); console.log(`[Bot ${id}] 스케줄 등록됨 (cron: ${expression})`);
} }
/** /**
* 개별 스케줄 해제 * 개별 스케줄 해제
*/ */
export function unregisterBot(botId) { export function unregisterBot(botId) {
if (schedulers.has(botId)) { const id = parseInt(botId);
schedulers.get(botId).stop(); if (schedulers.has(id)) {
schedulers.delete(botId); schedulers.get(id).stop();
console.log(`[Bot ${botId}] 스케줄 해제됨`); schedulers.delete(id);
console.log(`[Bot ${id}] 스케줄 해제됨`);
} }
} }