-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis-queue.ts
More file actions
281 lines (248 loc) · 7.72 KB
/
Copy pathanalysis-queue.ts
File metadata and controls
281 lines (248 loc) · 7.72 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* In-process analysis job queue with idempotency.
* Shape is Redis/BullMQ-replaceable without rewriting callers.
*
* Durability:
* - Default: in-memory only (single Probot instance).
* - Optional: set SPECSYNC_JOB_MARKERS_DIR to a shared filesystem path so
* multiple instances on the same volume can skip already-completed keys.
* Markers are local process state — they are NOT written into the target repo.
*/
import * as fs from "fs";
import * as path from "path";
import type { AnalysisJobPayload } from "./types";
export type JobStatus = "pending" | "running" | "completed" | "superseded" | "failed";
export interface AnalysisJob {
id: string;
/** Idempotency key: pr:{owner}/{repo}/{number}:{headSha} */
key: string;
payload: AnalysisJobPayload;
status: JobStatus;
enqueuedAt: number;
startedAt?: number;
finishedAt?: number;
error?: string;
}
export interface EnqueueResult {
enqueued: boolean;
jobId?: string;
key: string;
reason?: "duplicate" | "superseded_prior" | "in_flight" | "marker_completed";
}
export type AnalysisJobRunner = (job: AnalysisJob) => Promise<void>;
export function buildAnalysisJobKey(payload: AnalysisJobPayload): string {
return `pr:${payload.owner}/${payload.repo}/${payload.pr}:${payload.headSha}`;
}
function prScopeKey(payload: AnalysisJobPayload): string {
return `pr:${payload.owner}/${payload.repo}/${payload.pr}`;
}
/** Sanitize idempotency key for use as a filename. */
export function markerFileName(key: string): string {
return key.replace(/[^a-zA-Z0-9._-]+/g, "_") + ".json";
}
export class AnalysisJobQueue {
private jobs = new Map<string, AnalysisJob>();
private byPr = new Map<string, Set<string>>();
private running = 0;
private readonly maxConcurrent: number;
private readonly markersDir: string | null;
constructor(options?: { maxConcurrent?: number; markersDir?: string | null }) {
this.maxConcurrent = options?.maxConcurrent ?? 2;
const fromEnv = process.env.SPECSYNC_JOB_MARKERS_DIR;
this.markersDir =
options?.markersDir !== undefined
? options.markersDir
: fromEnv && fromEnv.trim()
? fromEnv.trim()
: null;
}
/**
* Enqueue analysis for a PR head SHA.
* Skips if the same key is completed or in-flight (memory and optional marker file).
* On a new SHA for the same PR, supersedes older pending/running jobs.
*/
enqueue(payload: AnalysisJobPayload, runner: AnalysisJobRunner): EnqueueResult {
const key = buildAnalysisJobKey(payload);
const existing = this.jobs.get(key);
if (existing && (existing.status === "completed" || existing.status === "running")) {
return {
enqueued: false,
jobId: existing.id,
key,
reason: existing.status === "running" ? "in_flight" : "duplicate",
};
}
if (existing && existing.status === "pending") {
return { enqueued: false, jobId: existing.id, key, reason: "in_flight" };
}
if (this.readMarkerStatus(key) === "completed") {
return { enqueued: false, key, reason: "marker_completed" };
}
const superseded = this.supersedeOlderShas(payload);
const job: AnalysisJob = {
id: `${key}:${Date.now()}`,
key,
payload,
status: "pending",
enqueuedAt: Date.now(),
};
this.jobs.set(key, job);
const scope = prScopeKey(payload);
if (!this.byPr.has(scope)) {
this.byPr.set(scope, new Set());
}
this.byPr.get(scope)!.add(key);
this.writeMarker(job, "pending");
this.schedule(job, runner);
return {
enqueued: true,
jobId: job.id,
key,
reason: superseded > 0 ? "superseded_prior" : undefined,
};
}
/** Cancel/supersede jobs for the same PR with a different head SHA. */
supersedeOlderShas(payload: AnalysisJobPayload): number {
const scope = prScopeKey(payload);
const keys = this.byPr.get(scope);
if (!keys) {
return 0;
}
let count = 0;
const currentKey = buildAnalysisJobKey(payload);
for (const key of keys) {
if (key === currentKey) {
continue;
}
const job = this.jobs.get(key);
if (!job) {
continue;
}
if (job.status === "pending" || job.status === "running") {
job.status = "superseded";
job.finishedAt = Date.now();
this.writeMarker(job, "superseded");
count += 1;
}
}
return count;
}
getJob(key: string): AnalysisJob | undefined {
return this.jobs.get(key);
}
/** Test helper — clears idempotency state between suites. */
clearForTests(): void {
this.jobs.clear();
this.byPr.clear();
this.running = 0;
}
/** Test/observability helper. */
getStats(): { total: number; running: number; byStatus: Record<JobStatus, number> } {
const byStatus: Record<JobStatus, number> = {
pending: 0,
running: 0,
completed: 0,
superseded: 0,
failed: 0,
};
for (const job of this.jobs.values()) {
byStatus[job.status] += 1;
}
return { total: this.jobs.size, running: this.running, byStatus };
}
private schedule(job: AnalysisJob, runner: AnalysisJobRunner): void {
setImmediate(() => {
void this.runWhenSlot(job, runner);
});
}
private async runWhenSlot(job: AnalysisJob, runner: AnalysisJobRunner): Promise<void> {
while (this.running >= this.maxConcurrent) {
await sleep(25);
if (job.status === "superseded") {
return;
}
}
if (job.status === "superseded") {
return;
}
this.running += 1;
job.status = "running";
job.startedAt = Date.now();
this.writeMarker(job, "running");
try {
await runner(job);
// Status may have been flipped to superseded concurrently while running.
if ((job.status as JobStatus) !== "superseded") {
job.status = "completed";
this.writeMarker(job, "completed");
}
} catch (error: unknown) {
if ((job.status as JobStatus) !== "superseded") {
job.status = "failed";
job.error = error instanceof Error ? error.message : String(error);
this.writeMarker(job, "failed");
}
throw error;
} finally {
job.finishedAt = Date.now();
this.running = Math.max(0, this.running - 1);
}
}
private markerPath(key: string): string | null {
if (!this.markersDir) {
return null;
}
return path.join(this.markersDir, markerFileName(key));
}
private readMarkerStatus(key: string): JobStatus | null {
const file = this.markerPath(key);
if (!file) {
return null;
}
try {
if (!fs.existsSync(file)) {
return null;
}
const raw = JSON.parse(fs.readFileSync(file, "utf8")) as { status?: JobStatus };
return raw.status ?? null;
} catch {
return null;
}
}
private writeMarker(job: AnalysisJob, status: JobStatus): void {
const file = this.markerPath(job.key);
if (!file) {
return;
}
try {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(
file,
JSON.stringify(
{
key: job.key,
jobId: job.id,
status,
payload: job.payload,
updatedAt: new Date().toISOString(),
error: job.error,
},
null,
2
),
"utf8"
);
} catch (error: unknown) {
// Markers are best-effort; never block analysis on FS errors.
console.warn(
"[SpecSync] failed to write job marker:",
error instanceof Error ? error.message : String(error)
);
}
}
}
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/** Shared singleton for the Probot process. */
export const analysisJobQueue = new AnalysisJobQueue();