52 lines
1.8 KiB
JavaScript
52 lines
1.8 KiB
JavaScript
import { prisma } from '../prisma.js';
|
|
/**
|
|
* Clean up inactive boards that have not been touched/updated in the last 30 days.
|
|
* Cascades to delete all associated columns and tickets.
|
|
*/
|
|
export async function cleanupInactiveBoards(daysInactive = 30) {
|
|
const cutoffDate = new Date();
|
|
cutoffDate.setDate(cutoffDate.getDate() - daysInactive);
|
|
console.log(`🧹 [TTL Cleanup] Checking for boards inactive since ${cutoffDate.toISOString()}...`);
|
|
try {
|
|
const result = await prisma.board.deleteMany({
|
|
where: {
|
|
updatedAt: {
|
|
lt: cutoffDate,
|
|
},
|
|
},
|
|
});
|
|
console.log(`🧹 [TTL Cleanup] Removed ${result.count} inactive boards (> ${daysInactive} days old).`);
|
|
return result.count;
|
|
}
|
|
catch (error) {
|
|
console.error('❌ [TTL Cleanup Error]:', error);
|
|
return 0;
|
|
}
|
|
}
|
|
/**
|
|
* Schedule periodic TTL cleanup in the server process (runs every 24 hours).
|
|
*/
|
|
export function startTtlCleanupSchedule(intervalHours = 24) {
|
|
console.log(`⏰ [TTL Schedule] Inactive board cleaner registered (Interval: every ${intervalHours}h)`);
|
|
// Run once after 1 minute on startup
|
|
setTimeout(() => {
|
|
cleanupInactiveBoards().catch(console.error);
|
|
}, 60000);
|
|
// Then run every intervalHours
|
|
return setInterval(() => {
|
|
cleanupInactiveBoards().catch(console.error);
|
|
}, intervalHours * 60 * 60 * 1000);
|
|
}
|
|
// Standalone execution support: `tsx src/cron/cleanup.ts`
|
|
if (process.argv[1]?.includes('cleanup.ts') || process.argv[1]?.includes('cleanup.js')) {
|
|
cleanupInactiveBoards()
|
|
.then((count) => {
|
|
console.log(`Done. Cleaned ${count} boards.`);
|
|
process.exit(0);
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|
|
}
|