EventBuzz_Chatbot/workers/purge.js

77 lines
2.2 KiB
JavaScript

const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '../.env') });
require('dotenv').config({ path: path.join(__dirname, '../../backend/.env') });
const { Worker, Queue } = require('bullmq');
const { connectDB } = require('../src/config/database');
const { getRedisConnection, QUEUE_PREFIX, PURGE_QUEUE } = require('../src/config/queue');
async function executeDataPurge() {
const Event = require('../../backend/src/models/Event');
const ConciergeBot = require('../../backend/src/models/ConciergeBot');
const KnowledgeSource = require('../../backend/src/models/KnowledgeSource');
const ContextChunk = require('../../backend/src/models/ContextChunk');
const ChatSubscriber = require('../../backend/src/models/ChatSubscriber');
const thresholdDate = new Date();
thresholdDate.setDate(thresholdDate.getDate() - 7);
const expiredEvents = await Event.find({
endDate: { $lt: thresholdDate },
}).select('_id');
const eventIds = expiredEvents.map((e) => e._id);
if (!eventIds.length) return { purged: 0 };
const bots = await ConciergeBot.find({
event: { $in: eventIds },
status: { $ne: 'ARCHIVED' },
});
for (const bot of bots) {
const eventId = bot.event;
await ContextChunk.deleteMany({ eventId });
await KnowledgeSource.deleteMany({ eventId });
await ChatSubscriber.deleteMany({ eventId });
bot.status = 'ARCHIVED';
bot.lastPurgedAt = new Date();
await bot.save();
console.log(`[purge] archived bot for event ${eventId}`);
}
return { purged: bots.length };
}
async function main() {
await connectDB();
const connection = getRedisConnection();
const queue = new Queue(PURGE_QUEUE, { connection, prefix: QUEUE_PREFIX });
await queue.add(
'daily-purge',
{},
{
repeat: { pattern: '0 2 * * *', tz: 'UTC' },
jobId: 'concierge-daily-purge',
},
);
const worker = new Worker(
PURGE_QUEUE,
async () => executeDataPurge(),
{ connection, prefix: QUEUE_PREFIX },
);
worker.on('completed', (job, result) => {
console.log('[purge] completed', job.id, result);
});
console.log('[purge] worker started (daily 02:00 UTC)');
}
main().catch((e) => {
console.error(e);
process.exit(1);
});