Chat-With-AI / workers /updateWorker.ts
NathMen12's picture
Upload 56 files
ef73937 verified
Raw
History Blame Contribute Delete
2.32 kB
import { channelRepository } from '@core/storage/repositories/channels.js';
import { scrapeQueueRepository } from '@core/storage/repositories/scrapeQueue.js';
import { logRepository } from '@core/storage/repositories/logs.js';
import { config } from '@config/index.js';
import { sleep } from '@core/utils/helpers.js';
async function runUpdateWorker() {
console.log('🔄 Starting update worker (24h cycle)...');
await Promise.all([
channelRepository.initialize(),
scrapeQueueRepository.initialize(),
logRepository.initialize(),
]);
let running = true;
process.on('SIGINT', () => { running = false; });
process.on('SIGTERM', () => { running = false; });
// Run immediately on start
await updateAllChannels();
// Then run every 24 hours
while (running) {
await sleep(24 * 60 * 60 * 1000); // 24 hours
if (running) {
await updateAllChannels();
}
}
console.log('🛑 Update worker stopped');
}
async function updateAllChannels() {
console.log('📡 Starting daily channel update...');
try {
const channels = await channelRepository.findByStatus('published', { limit: 10000 });
const publishedChannels = channels.filter(c => !c.isBanned);
console.log(`Found ${publishedChannels.length} published channels to update`);
let queued = 0;
let errors = 0;
for (const channel of publishedChannels) {
try {
// Re-queue for scraping update
await scrapeQueueRepository.requeueForUpdate(channel.id, channel.inviteLink, channel.ownerId);
queued++;
// Small delay to avoid overwhelming the queue
await sleep(100);
} catch (error) {
errors++;
console.error(`Failed to queue update for ${channel.id}:`, error);
}
}
await logRepository.log({
level: 'info',
message: 'Daily channel update queued',
context: { totalChannels: publishedChannels.length, queued, errors },
});
console.log(`✅ Update queued: ${queued} channels, ${errors} errors`);
} catch (error) {
console.error('Update worker error:', error);
await logRepository.log({
level: 'error',
message: 'Update worker failed',
error: { name: 'Error', message: String(error), stack: String(error) },
});
}
}
runUpdateWorker().catch(console.error);