fix(scheduler): 봇 중지 상태가 서버 재시작 후 유지되지 않는 문제 수정

DB 조회 시 WHERE enabled = 1 필터를 제거하여 비활성 봇도 시스템에서
인식되도록 변경. 이전에는 비활성 봇이 목록/검색에서 완전히 제외되어
재시작 불가 및 관리 UI에서 사라지는 문제가 있었음.
PUT 엔드포인트의 stopBot/startBot 조건 로직도 함께 정리.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
caadiq 2026-02-23 15:27:16 +09:00
parent 91d4442d30
commit 3feb23f67f
3 changed files with 27 additions and 8 deletions

View file

@ -16,7 +16,7 @@ async function schedulerPlugin(fastify, opts) {
*/
async function getYouTubeBotsFromDB() {
const [rows] = await fastify.db.query(
'SELECT * FROM bot_youtube WHERE enabled = 1'
'SELECT * FROM bot_youtube'
);
return rows.map(row => ({
id: `youtube-${row.id}`, // DB ID를 문자열 형식으로 변환
@ -52,7 +52,7 @@ async function schedulerPlugin(fastify, opts) {
*/
async function getXBotsFromDB() {
const [rows] = await fastify.db.query(
'SELECT * FROM bot_x WHERE enabled = 1'
'SELECT * FROM bot_x'
);
return rows.map(row => ({
id: `x-${row.id}`,
@ -161,6 +161,18 @@ async function schedulerPlugin(fastify, opts) {
return result.addedCount;
}
/**
* DB의 enabled 필드 업데이트 (정적 봇은 무시)
*/
async function setEnabled(botId, enabled) {
const match = botId.match(/^(youtube|x)-(\d+)$/);
if (!match) return; // 정적 봇 (meilisearch 등)
const table = match[1] === 'x' ? 'bot_x' : 'bot_youtube';
const dbId = match[2];
await fastify.db.query(`UPDATE ${table} SET enabled = ? WHERE id = ?`, [enabled ? 1 : 0, dbId]);
invalidateCache();
}
/**
* 시작
*/
@ -176,6 +188,9 @@ async function schedulerPlugin(fastify, opts) {
tasks.delete(botId);
}
// DB enabled 활성화
await setEnabled(botId, true);
const syncFn = getSyncFunction(bot);
if (!syncFn) {
throw new Error(`지원하지 않는 봇 타입: ${bot.type}`);
@ -222,6 +237,8 @@ async function schedulerPlugin(fastify, opts) {
tasks.get(botId).stop();
tasks.delete(botId);
}
// DB enabled 비활성화
await setEnabled(botId, false);
await updateStatus(botId, { status: 'stopped' });
fastify.log.info(`[${botId}] 스케줄 정지`);
}

View file

@ -327,11 +327,12 @@ export default async function xBotsRoutes(fastify) {
// 스케줄러 캐시 무효화 및 봇 재시작
scheduler.invalidateCache();
const botId = `x-${id}`;
const shouldBeEnabled = updates.enabled !== undefined
? updates.enabled
: existing[0].enabled === 1;
try {
await scheduler.stopBot(botId);
if (updates.enabled !== false && existing[0].enabled === 1) {
await scheduler.startBot(botId);
} else if (updates.enabled === true) {
if (shouldBeEnabled) {
await scheduler.startBot(botId);
}
} catch (err) {

View file

@ -334,11 +334,12 @@ export default async function youtubeBotsRoutes(fastify) {
// 스케줄러 캐시 무효화 및 봇 재시작
scheduler.invalidateCache();
const botId = `youtube-${id}`;
const shouldBeEnabled = updates.enabled !== undefined
? updates.enabled
: existing[0].enabled === 1;
try {
await scheduler.stopBot(botId);
if (updates.enabled !== false && existing[0].enabled === 1) {
await scheduler.startBot(botId);
} else if (updates.enabled === true) {
if (shouldBeEnabled) {
await scheduler.startBot(botId);
}
} catch (err) {