UNPKG

mcp-daily-tasks

Version:

MCP para manejar tareas y resúmenes con integración ClickUp.

70 lines (69 loc) 2.61 kB
import { loadCronTasks } from "../utils/cronStorage.js"; import { scheduleTask } from "../utils/cronScheduler.js"; import { generateReport } from "../../../aiProcessor.js"; import { postSlackThreadMessage } from "../../../tools/slack/utils/postSlackThreadMessage.js"; import { getDateRangeForPeriod } from "../../activities/tools/toolGetActivitiesByProjectAndPeriod.js"; import { storage } from "../../activities/utils/storageProvider.js"; export class CronService { constructor() { this.isRunning = false; } static getInstance() { if (!CronService.instance) { CronService.instance = new CronService(); } return CronService.instance; } async start() { if (this.isRunning) return; this.isRunning = true; const tasks = loadCronTasks(); tasks.forEach((task) => { this.scheduleTask(task); }); } async getActivitiesForReport(task) { try { const { period, startDate, endDate, projectName } = task.reportConfig; const range = getDateRangeForPeriod(period, startDate, endDate); return storage.getActivitiesByRange(range.start, range.end, projectName); } catch (error) { console.error("Error obteniendo actividades:", error); return []; } } scheduleTask(task) { scheduleTask(task, async (t) => { try { const currentTasks = loadCronTasks(); const currentTask = currentTasks.find((task) => task.id === t.id); if (!currentTask) { return; } const activities = await this.getActivitiesForReport(currentTask); const report = await generateReport(currentTask.prompt, { activities, context: { taskId: currentTask.id, channel: currentTask.channel, executionTime: new Date().toISOString(), reportConfig: currentTask.reportConfig } }); if (!report) { return; } await postSlackThreadMessage(currentTask.channel, "Reporte automático generado por MCP:", report); } catch (error) { // console.error(`❌ Error en tarea ${t.id}:`, error); } }); } stop() { this.isRunning = false; // console.log("🛑 Servicio de cron detenido"); } }