diff --git a/src/cron/calendar/index.js b/src/cron/calendar/index.js index 197bfa9..db4c09b 100644 --- a/src/cron/calendar/index.js +++ b/src/cron/calendar/index.js @@ -26,49 +26,43 @@ const isTestMode = () => { /** * Start the calendar notification cron job + * @returns {{ name: string, schedule: string }} */ export const startCalendarNotificationCron = () => { let cronExpression; - let description; + let schedule; if (isTestMode()) { - // Test mode: run every minute cronExpression = '* * * * *'; - description = 'každú minútu (TEST MODE)'; - logger.warn('NOTIFICATION_TEST_MODE je zapnutý - cron beží každú minútu!'); + schedule = 'every minute (TEST)'; + logger.warn('NOTIFICATION_TEST_MODE enabled - notifications run every minute!'); } else { - // Production mode: run at specified time const { hour, minute } = parseNotificationTime(); cronExpression = `${minute} ${hour} * * *`; - description = `${hour}:${minute.padStart(2, '0')} každý deň`; + schedule = `${hour}:${minute.padStart(2, '0')}`; } - logger.info(`Nastavujem cron pre kalendárne notifikácie: ${cronExpression} (${description})`); - - const task = cron.schedule(cronExpression, async () => { - logger.info('Cron job spustený - kontrolujem zajtrajšie udalosti'); - + cron.schedule(cronExpression, async () => { + logger.info('Running calendar notifications...'); try { const stats = await sendEventNotifications(); - logger.info(`Cron job dokončený - výsledky: ${JSON.stringify(stats)}`); + logger.info(`Notifications done: sent=${stats.sent}, failed=${stats.failed}, skipped=${stats.skipped}`); } catch (error) { - logger.error('Chyba pri spúšťaní cron jobu', error); + logger.error('Calendar notification cron failed', error); } }, { scheduled: true, timezone: 'Europe/Bratislava', }); - logger.success(`Kalendárny notifikačný cron naplánovaný: ${description} (Europe/Bratislava)`); - - return task; + return { name: `Calendar (${schedule})`, schedule }; }; /** * Manually trigger event notifications (for testing) */ export const triggerEventNotifications = async () => { - logger.info('Manuálne spúšťam kontrolu notifikácií...'); + logger.info('Manually triggering notifications...'); return sendEventNotifications(); }; @@ -78,6 +72,6 @@ export const triggerEventNotifications = async () => { * @param {string} adminUserId - Admin user ID (sender) */ export const triggerSingleEventNotification = async (eventId, adminUserId) => { - logger.info(`Manuálne spúšťam notifikáciu pre event ${eventId} od admina ${adminUserId}...`); + logger.info(`Manually triggering notification for event ${eventId}`); return sendSingleEventNotification(eventId, adminUserId); }; diff --git a/src/cron/cleanupAuditLogs.js b/src/cron/cleanupAuditLogs.js index 9ce58a9..293e8d2 100644 --- a/src/cron/cleanupAuditLogs.js +++ b/src/cron/cleanupAuditLogs.js @@ -1,7 +1,7 @@ import cron from 'node-cron'; import { db } from '../config/database.js'; import { auditLogs } from '../db/schema.js'; -import { lt, sql } from 'drizzle-orm'; +import { lt } from 'drizzle-orm'; import { logger } from '../utils/logger.js'; const RETENTION_DAYS = 7; @@ -13,8 +13,6 @@ export const cleanupOldAuditLogs = async () => { const cutoffDate = new Date(); cutoffDate.setDate(cutoffDate.getDate() - RETENTION_DAYS); - logger.info(`[Audit Cleanup] Spúšťam čistenie audit logov starších ako ${RETENTION_DAYS} dní (pred ${cutoffDate.toISOString()})`); - try { const result = await db .delete(auditLogs) @@ -24,14 +22,12 @@ export const cleanupOldAuditLogs = async () => { const deletedCount = result.length; if (deletedCount > 0) { - logger.info(`[Audit Cleanup] Úspešne zmazaných ${deletedCount} starých audit logov`); - } else { - logger.info('[Audit Cleanup] Žiadne staré audit logy na zmazanie'); + logger.info(`Audit cleanup: deleted ${deletedCount} old logs`); } return deletedCount; } catch (error) { - logger.error('[Audit Cleanup] Chyba pri mazaní audit logov:', error); + logger.error('Audit cleanup failed', error); throw error; } }; @@ -39,19 +35,12 @@ export const cleanupOldAuditLogs = async () => { /** * Start the audit logs cleanup cron job * Runs every day at midnight (00:00) + * @returns {{ name: string, schedule: string }} */ export const startAuditCleanupCron = () => { - // Schedule: '0 0 * * *' = every day at 00:00 cron.schedule('0 0 * * *', () => { - logger.info('[Audit Cleanup] Cron job spustený'); - cleanupOldAuditLogs() - .then(() => { - logger.info('[Audit Cleanup] Cron job dokončený'); - }) - .catch((error) => { - logger.error('[Audit Cleanup] Cron job zlyhal:', error); - }); + cleanupOldAuditLogs().catch(() => {}); }); - logger.info('[Audit Cleanup] Cron job naplánovaný - každý deň o 00:00'); + return { name: 'Audit cleanup (00:00)', schedule: '00:00' }; }; diff --git a/src/cron/index.js b/src/cron/index.js index cf0c957..ba5e7c1 100644 --- a/src/cron/index.js +++ b/src/cron/index.js @@ -6,15 +6,18 @@ import { startAuditCleanupCron, cleanupOldAuditLogs } from './cleanupAuditLogs.j * Start all cron jobs */ export const startAllCronJobs = () => { - logger.info('=== Inicializujem cron jobs ==='); + const jobs = []; // Calendar event notifications - startCalendarNotificationCron(); + const calendarJob = startCalendarNotificationCron(); + jobs.push(calendarJob); - // Audit logs cleanup (daily at midnight) - startAuditCleanupCron(); + // Audit logs cleanup + const auditJob = startAuditCleanupCron(); + jobs.push(auditJob); - logger.info('=== Všetky cron jobs inicializované ==='); + // Log summary + logger.info(`Cron jobs initialized: ${jobs.map(j => j.name).join(', ')}`); }; // Export individual functions for testing/manual triggers