diff --git a/backend/src/config/bots.js b/backend/src/config/bots.js index 9efc14f..579c6a2 100644 --- a/backend/src/config/bots.js +++ b/backend/src/config/bots.js @@ -1,4 +1,11 @@ export default [ + { + id: 'meilisearch-sync', + type: 'meilisearch', + name: 'Meilisearch 동기화', + cron: '5 4 * * *', + enabled: true, + }, { id: 'youtube-fromis9', type: 'youtube', diff --git a/backend/src/plugins/scheduler.js b/backend/src/plugins/scheduler.js index fb381b4..a6f5052 100644 --- a/backend/src/plugins/scheduler.js +++ b/backend/src/plugins/scheduler.js @@ -1,6 +1,7 @@ import fp from 'fastify-plugin'; import cron from 'node-cron'; import bots from '../config/bots.js'; +import { syncAllSchedules } from '../services/meilisearch/index.js'; const REDIS_PREFIX = 'bot:status:'; @@ -42,6 +43,11 @@ async function schedulerPlugin(fastify, opts) { return fastify.youtubeBot.syncNewVideos; } else if (bot.type === 'x') { return fastify.xBot.syncNewTweets; + } else if (bot.type === 'meilisearch') { + return async () => { + const count = await syncAllSchedules(fastify.meilisearch, fastify.db); + return { addedCount: count, total: count }; + }; } return null; } @@ -108,13 +114,15 @@ async function schedulerPlugin(fastify, opts) { await updateStatus(botId, { status: 'running' }); fastify.log.info(`[${botId}] 스케줄 시작 (cron: ${bot.cron})`); - // 즉시 1회 실행 - try { - const result = await syncFn(bot); - const addedCount = await handleSyncResult(botId, result); - fastify.log.info(`[${botId}] 초기 동기화 완료: ${addedCount}개 추가`); - } catch (err) { - fastify.log.error(`[${botId}] 초기 동기화 오류: ${err.message}`); + // 즉시 1회 실행 (meilisearch는 스케줄 시간에만 실행) + if (bot.type !== 'meilisearch') { + try { + const result = await syncFn(bot); + const addedCount = await handleSyncResult(botId, result); + fastify.log.info(`[${botId}] 초기 동기화 완료: ${addedCount}개 추가`); + } catch (err) { + fastify.log.error(`[${botId}] 초기 동기화 오류: ${err.message}`); + } } } @@ -175,5 +183,5 @@ async function schedulerPlugin(fastify, opts) { export default fp(schedulerPlugin, { name: 'scheduler', - dependencies: ['db', 'redis', 'youtubeBot', 'xBot'], + dependencies: ['db', 'redis', 'meilisearch', 'youtubeBot', 'xBot'], }); diff --git a/backend/src/routes/admin/bots.js b/backend/src/routes/admin/bots.js index 913080a..8dbd2b7 100644 --- a/backend/src/routes/admin/bots.js +++ b/backend/src/routes/admin/bots.js @@ -1,5 +1,6 @@ import bots from '../../config/bots.js'; import { errorResponse } from '../../schemas/index.js'; +import { syncAllSchedules } from '../../services/meilisearch/index.js'; // 봇 관련 스키마 const botResponse = { @@ -7,7 +8,7 @@ const botResponse = { properties: { id: { type: 'string' }, name: { type: 'string' }, - type: { type: 'string', enum: ['youtube', 'x'] }, + type: { type: 'string', enum: ['youtube', 'x', 'meilisearch'] }, status: { type: 'string', enum: ['running', 'stopped', 'error'] }, last_check_at: { type: 'string', format: 'date-time' }, last_added_count: { type: 'integer' }, @@ -67,7 +68,7 @@ export default async function botsRoutes(fastify) { result.push({ id: bot.id, - name: bot.channelName || bot.username || bot.id, + name: bot.name || bot.channelName || bot.username || bot.id, type: bot.type, status: status.status, last_check_at: status.lastCheckAt, @@ -190,6 +191,9 @@ export default async function botsRoutes(fastify) { result = await fastify.youtubeBot.syncAllVideos(bot); } else if (bot.type === 'x') { result = await fastify.xBot.syncAllTweets(bot); + } else if (bot.type === 'meilisearch') { + const count = await syncAllSchedules(fastify.meilisearch, fastify.db); + result = { addedCount: count, total: count }; } else { return reply.code(400).send({ error: '지원하지 않는 봇 타입입니다.' }); }