refactor: Clean up cron initialization logs
Single summary line instead of verbose duplicates: [INFO] Cron jobs initialized: Calendar (07:00), Audit cleanup (00:00) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -26,49 +26,43 @@ const isTestMode = () => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Start the calendar notification cron job
|
* Start the calendar notification cron job
|
||||||
|
* @returns {{ name: string, schedule: string }}
|
||||||
*/
|
*/
|
||||||
export const startCalendarNotificationCron = () => {
|
export const startCalendarNotificationCron = () => {
|
||||||
let cronExpression;
|
let cronExpression;
|
||||||
let description;
|
let schedule;
|
||||||
|
|
||||||
if (isTestMode()) {
|
if (isTestMode()) {
|
||||||
// Test mode: run every minute
|
|
||||||
cronExpression = '* * * * *';
|
cronExpression = '* * * * *';
|
||||||
description = 'každú minútu (TEST MODE)';
|
schedule = 'every minute (TEST)';
|
||||||
logger.warn('NOTIFICATION_TEST_MODE je zapnutý - cron beží každú minútu!');
|
logger.warn('NOTIFICATION_TEST_MODE enabled - notifications run every minute!');
|
||||||
} else {
|
} else {
|
||||||
// Production mode: run at specified time
|
|
||||||
const { hour, minute } = parseNotificationTime();
|
const { hour, minute } = parseNotificationTime();
|
||||||
cronExpression = `${minute} ${hour} * * *`;
|
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})`);
|
cron.schedule(cronExpression, async () => {
|
||||||
|
logger.info('Running calendar notifications...');
|
||||||
const task = cron.schedule(cronExpression, async () => {
|
|
||||||
logger.info('Cron job spustený - kontrolujem zajtrajšie udalosti');
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const stats = await sendEventNotifications();
|
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) {
|
} catch (error) {
|
||||||
logger.error('Chyba pri spúšťaní cron jobu', error);
|
logger.error('Calendar notification cron failed', error);
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
scheduled: true,
|
scheduled: true,
|
||||||
timezone: 'Europe/Bratislava',
|
timezone: 'Europe/Bratislava',
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.success(`Kalendárny notifikačný cron naplánovaný: ${description} (Europe/Bratislava)`);
|
return { name: `Calendar (${schedule})`, schedule };
|
||||||
|
|
||||||
return task;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manually trigger event notifications (for testing)
|
* Manually trigger event notifications (for testing)
|
||||||
*/
|
*/
|
||||||
export const triggerEventNotifications = async () => {
|
export const triggerEventNotifications = async () => {
|
||||||
logger.info('Manuálne spúšťam kontrolu notifikácií...');
|
logger.info('Manually triggering notifications...');
|
||||||
return sendEventNotifications();
|
return sendEventNotifications();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -78,6 +72,6 @@ export const triggerEventNotifications = async () => {
|
|||||||
* @param {string} adminUserId - Admin user ID (sender)
|
* @param {string} adminUserId - Admin user ID (sender)
|
||||||
*/
|
*/
|
||||||
export const triggerSingleEventNotification = async (eventId, adminUserId) => {
|
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);
|
return sendSingleEventNotification(eventId, adminUserId);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import cron from 'node-cron';
|
import cron from 'node-cron';
|
||||||
import { db } from '../config/database.js';
|
import { db } from '../config/database.js';
|
||||||
import { auditLogs } from '../db/schema.js';
|
import { auditLogs } from '../db/schema.js';
|
||||||
import { lt, sql } from 'drizzle-orm';
|
import { lt } from 'drizzle-orm';
|
||||||
import { logger } from '../utils/logger.js';
|
import { logger } from '../utils/logger.js';
|
||||||
|
|
||||||
const RETENTION_DAYS = 7;
|
const RETENTION_DAYS = 7;
|
||||||
@@ -13,8 +13,6 @@ export const cleanupOldAuditLogs = async () => {
|
|||||||
const cutoffDate = new Date();
|
const cutoffDate = new Date();
|
||||||
cutoffDate.setDate(cutoffDate.getDate() - RETENTION_DAYS);
|
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 {
|
try {
|
||||||
const result = await db
|
const result = await db
|
||||||
.delete(auditLogs)
|
.delete(auditLogs)
|
||||||
@@ -24,14 +22,12 @@ export const cleanupOldAuditLogs = async () => {
|
|||||||
const deletedCount = result.length;
|
const deletedCount = result.length;
|
||||||
|
|
||||||
if (deletedCount > 0) {
|
if (deletedCount > 0) {
|
||||||
logger.info(`[Audit Cleanup] Úspešne zmazaných ${deletedCount} starých audit logov`);
|
logger.info(`Audit cleanup: deleted ${deletedCount} old logs`);
|
||||||
} else {
|
|
||||||
logger.info('[Audit Cleanup] Žiadne staré audit logy na zmazanie');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return deletedCount;
|
return deletedCount;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error('[Audit Cleanup] Chyba pri mazaní audit logov:', error);
|
logger.error('Audit cleanup failed', error);
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -39,19 +35,12 @@ export const cleanupOldAuditLogs = async () => {
|
|||||||
/**
|
/**
|
||||||
* Start the audit logs cleanup cron job
|
* Start the audit logs cleanup cron job
|
||||||
* Runs every day at midnight (00:00)
|
* Runs every day at midnight (00:00)
|
||||||
|
* @returns {{ name: string, schedule: string }}
|
||||||
*/
|
*/
|
||||||
export const startAuditCleanupCron = () => {
|
export const startAuditCleanupCron = () => {
|
||||||
// Schedule: '0 0 * * *' = every day at 00:00
|
|
||||||
cron.schedule('0 0 * * *', () => {
|
cron.schedule('0 0 * * *', () => {
|
||||||
logger.info('[Audit Cleanup] Cron job spustený');
|
cleanupOldAuditLogs().catch(() => {});
|
||||||
cleanupOldAuditLogs()
|
|
||||||
.then(() => {
|
|
||||||
logger.info('[Audit Cleanup] Cron job dokončený');
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error('[Audit Cleanup] Cron job zlyhal:', error);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
logger.info('[Audit Cleanup] Cron job naplánovaný - každý deň o 00:00');
|
return { name: 'Audit cleanup (00:00)', schedule: '00:00' };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,15 +6,18 @@ import { startAuditCleanupCron, cleanupOldAuditLogs } from './cleanupAuditLogs.j
|
|||||||
* Start all cron jobs
|
* Start all cron jobs
|
||||||
*/
|
*/
|
||||||
export const startAllCronJobs = () => {
|
export const startAllCronJobs = () => {
|
||||||
logger.info('=== Inicializujem cron jobs ===');
|
const jobs = [];
|
||||||
|
|
||||||
// Calendar event notifications
|
// Calendar event notifications
|
||||||
startCalendarNotificationCron();
|
const calendarJob = startCalendarNotificationCron();
|
||||||
|
jobs.push(calendarJob);
|
||||||
|
|
||||||
// Audit logs cleanup (daily at midnight)
|
// Audit logs cleanup
|
||||||
startAuditCleanupCron();
|
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
|
// Export individual functions for testing/manual triggers
|
||||||
|
|||||||
Reference in New Issue
Block a user