/ opencodehub / scripts / worker.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
78 lines · 2.4 KB
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 { getDatabase, schema } from "@/db";
import { queueWorker } from "@/lib/queue-worker";
import { syncAllMirrors } from "@/lib/mirror-sync";
import { cleanupAllRepos } from "@/lib/cron/cleanup-branches";
import { logger } from "@/lib/logger";
import { eq, and, sql } from "drizzle-orm";

const WORKER_INTERVAL = 5000; // 5 seconds for queue polling
const MIRROR_SYNC_INTERVAL = 60 * 1000; // 1 minute
const CLEANUP_INTERVAL = 60 * 60 * 1000; // 1 hour

async function runQueueProcessor() {
    const db = getDatabase();

    try {
        // Find repositories with queued items
        // We group by repositoryId to process per-repo queues
        // Actually QueueWorker handles locking per repo?
        // Let's just find distinct repositoryIds that have queued items.
        // Drizzle distinct on unsupported? Let's Fetch all pending items and get unique repoIds.

        const pendingItems = await db.query.mergeQueueItems.findMany({
            where: eq(schema.mergeQueueItems.status, "queued"),
            columns: { repositoryId: true },
            // limit: 100 ??
        });

        const repoIds = [...new Set(pendingItems.map(item => item.repositoryId))];

        if (repoIds.length > 0) {
            logger.info({ repoCount: repoIds.length }, "Processing merge queues");

            // Process concurrently? Or sequentially to avoid load?
            // Concurrently is fine as they are independent locks.
            await Promise.all(repoIds.map(repoId => queueWorker.processQueue(repoId)));
        }

    } catch (error) {
        logger.error({ err: error }, "Error in queue processor loop");
    }
}

async function startWorker() {
    logger.info("Starting OpenCodeHub Background Worker...");

    // 1. Merge Queue Loop
    setInterval(runQueueProcessor, WORKER_INTERVAL);
    // Initial run
    runQueueProcessor();

    // 2. Mirror Sync Loop
    setInterval(async () => {
        try {
            await syncAllMirrors();
        } catch (e) {
            logger.error({ err: e }, "Mirror sync failed");
        }
    }, MIRROR_SYNC_INTERVAL);

    // 3. Cleanup Loop
    setInterval(async () => {
        try {
            await cleanupAllRepos();
        } catch (e) {
            logger.error({ err: e }, "Cleanup failed");
        }
    }, CLEANUP_INTERVAL);

    // Keep process alive
    process.on('SIGINT', () => {
        logger.info("Worker shutting down...");
        process.exit(0);
    });
}

startWorker().catch(console.error);