/ opencodehub / src / lib / queue-worker.ts
swadhinbiswas feat: OpenCodeHub v1.1.0 - Enterprise Features & Storage Adapters · 2/15/2026
719a1cb
223 lines · 9.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { getDatabase, schema } from "@/db";
import { eq, and, asc } from "drizzle-orm";
import { logger } from "@/lib/logger";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { mergeBranch, createSpeculativeBranch } from "@/lib/git";
import { acquireRepo, releaseRepo } from "@/lib/git-storage";

export class QueueWorker {
    private isProcessing = false;

    async processQueue(repositoryId: string) {
        if (this.isProcessing) return;
        this.isProcessing = true;

        const db = getDatabase() as NodePgDatabase<typeof schema>;

        try {
            // 1. Check if anything is currently running
            const runningItem = await db.query.mergeQueueItems.findFirst({
                where: and(
                    eq(schema.mergeQueueItems.repositoryId, repositoryId),
                    eq(schema.mergeQueueItems.status, "running")
                ),
            });

            if (runningItem) {
                // In a real system, we'd check if the CI job is still active.
                // For MVP, we'll assume the previous run crashed if it's been running too long (>10m)
                const runtime = Date.now() - (runningItem.startedAt?.getTime() || 0);
                if (runtime > 10 * 60 * 1000) {
                    logger.warn(`Queue item ${runningItem.id} timed out. Marking failed.`);
                    await db.update(schema.mergeQueueItems)
                        .set({ status: "failed", completedAt: new Date() })
                        .where(eq(schema.mergeQueueItems.id, runningItem.id));
                    // Continue to next item
                } else {
                    return;
                }
            }

            // 2. Pick next item
            const nextItem = await db.query.mergeQueueItems.findFirst({
                where: and(
                    eq(schema.mergeQueueItems.repositoryId, repositoryId),
                    eq(schema.mergeQueueItems.status, "queued")
                ),
                orderBy: [asc(schema.mergeQueueItems.queuedAt)],
                with: {
                    repository: {
                        with: { owner: true }
                    },
                    pullRequest: true
                }
            });

            if (!nextItem) {
                return;
            }

            // 3. Start processing
            await db.update(schema.mergeQueueItems)
                .set({
                    status: "running",
                    startedAt: new Date(),
                    attemptCount: (nextItem.attemptCount || 0) + 1,
                    lastAttemptAt: new Date()
                })
                .where(eq(schema.mergeQueueItems.id, nextItem.id));

            logger.info(`Starting merge for PR ${nextItem.pullRequestId} (${nextItem.repository.owner.username}/${nextItem.repository.name})`);

            // 3b. Trigger Speculative Builds for upcoming PRs
            // We do this concurrently while the main PR is entering its build phase
            this.triggerSpeculativeBuilds(repositoryId, nextItem, db).catch(err => {
                logger.error("Failed to trigger speculative builds", err);
            });

            // 4. Execution
            // A. Acquire Repo (Local or Cloud)
            const repoPath = await acquireRepo(nextItem.repository.owner.username, nextItem.repository.name);

            // B. Simulate CI - In a real world, we'd trigger a workflow here and exit, waiting for a webhook callback.
            // For MVP "Flesh out", we simulate 'running tests' on the merge result.

            // Attempt Merge Locally first to check conflicts
            const result = await mergeBranch(repoPath, nextItem.pullRequest.baseBranch, nextItem.pullRequest.headBranch);

            // B. Simulate CI / Check Speculative Result
            if (nextItem.executionBranch) {
                logger.info(`Speculative hit for PR ${nextItem.pullRequestId} using branch ${nextItem.executionBranch}. Skipping build wait.`);
                // In a real system, we would verify the CI status of executionBranch here.
                // Since we created it successfully in triggerSpeculativeBuilds, we assume it passed.
            } else {
                // Simulate Build Time for non-speculative runs
                logger.info(`Running CI simulation for PR ${nextItem.pullRequestId}...`);
                await new Promise(r => setTimeout(r, 5000));
            }

            if (result.success) {
                // Success!
                // 1. Update PR Status
                const now = new Date();
                await db.update(schema.pullRequests)
                    .set({
                        state: "merged",
                        isMerged: true,
                        mergedAt: now,
                        // mergedById: system user?
                        updatedAt: now,
                    })
                    .where(eq(schema.pullRequests.id, nextItem.pullRequestId));

                // 2. Update Queue Item
                await db.update(schema.mergeQueueItems)
                    .set({ status: "merged", completedAt: now })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));

                // 3. Sync Logic (Release Repo with modified=true)
                // Since mergeBranch modified the repo (update-ref), we must push back.
                await releaseRepo(nextItem.repository.owner.username, nextItem.repository.name, true);

                logger.info(`PR ${nextItem.pullRequestId} merged successfully via queue.`);
            } else {
                // Failure (Conflict)
                await db.update(schema.mergeQueueItems)
                    .set({ status: "failed", completedAt: new Date() })
                    .where(eq(schema.mergeQueueItems.id, nextItem.id));

                // Release without sync (no changes to base, strict speaking merge-tree doesn't touch base if fail? 
                // Actually mergeBranch implementation might have failed before update-ref or during conflict check.
                await releaseRepo(nextItem.repository.owner.username, nextItem.repository.name, false);

                logger.warn(`PR ${nextItem.pullRequestId} failed to merge: ${result.message}`);
            }

        } catch (error) {
            logger.error("Error processing merge queue", error);
            // Try to release repo if stuck?
            // We don't have scope here easily, but acquireRepo is safe to re-acquire (cleans up).
        } finally {
            this.isProcessing = false;
            // Trigger next immediately?
            // this.processQueue(repositoryId);
        }
    }

    /**
     * Look ahead in the queue and trigger speculative builds
     */
    private async triggerSpeculativeBuilds(
        repositoryId: string,
        currentItem: any,
        db: NodePgDatabase<typeof schema>
    ) {
        // Fetch next 2 items
        const lookaheadItems = await db.query.mergeQueueItems.findMany({
            where: and(
                eq(schema.mergeQueueItems.repositoryId, repositoryId),
                eq(schema.mergeQueueItems.status, "queued")
            ),
            orderBy: [asc(schema.mergeQueueItems.queuedAt)],
            limit: 2,
        });

        if (lookaheadItems.length === 0) return;

        logger.info(`Found ${lookaheadItems.length} items for speculative execution`);

        const repo = currentItem.repository;
        const repoPath = await acquireRepo(repo.owner.username, repo.name); // Re-acquire safe? Yes

        try {
            // Build the chain of PRs: [Current, Next 1, Next 2]
            const prChain = [
                { headBranch: currentItem.pullRequest.headBranch, number: currentItem.pullRequest.number }
            ];

            for (const item of lookaheadItems) {
                // If already has an execution branch, skip (assume already handling)
                if (item.executionBranch) continue;

                // Fetch PR details
                const pr = await db.query.pullRequests.findFirst({
                    where: eq(schema.pullRequests.id, item.pullRequestId)
                });

                if (!pr) continue;

                prChain.push({ headBranch: pr.headBranch, number: pr.number });

                logger.info(`Creating speculative branch for PR #${pr.number} (Chain: ${prChain.map(p => p.number).join("+")})`);

                const result = await createSpeculativeBranch(
                    repoPath,
                    pr.baseBranch, // Assuming all target same base
                    [...prChain] // Copy array
                );

                if (result.success) {
                    await db.update(schema.mergeQueueItems)
                        .set({
                            executionBranch: result.branchName,
                            // We don't set status to 'running' to keep them in 'queued' for the main worker loop,
                            // but having executionBranch implies speculative CI is running.
                        })
                        .where(eq(schema.mergeQueueItems.id, item.id));

                    logger.info(`Speculative branch created: ${result.branchName}`);
                } else {
                    logger.warn(`Failed to create speculative branch: ${result.message}`);
                }
            }
        } finally {
            // We don't release here because the main loop holds the lock? 
            // acquireRepo is usually re-entrant for same process/path or handles locking. 
            // Checks if git-storage supports concurrent access. 
            // Assuming it does simply resolve path.
        }
    }
}

export const queueWorker = new QueueWorker();