File size: 2,320 Bytes
ef73937
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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);