forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob-executor.ts
More file actions
362 lines (311 loc) · 10.1 KB
/
job-executor.ts
File metadata and controls
362 lines (311 loc) · 10.1 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import {
getMediaJob,
getMediaJobItems,
getPendingJobItems,
updateMediaJobStatus,
updateMediaJobItem,
updateMediaJobCounters,
cancelPendingJobItems,
} from '@/lib/db';
import { generateSingleImage } from '@/lib/image-generator';
import type { BatchConfig, JobProgressEvent, MediaJobItem } from '@/types';
// ==========================================
// globalThis Singleton (survives hot reload)
// ==========================================
interface RunningJob {
jobId: string;
abortController: AbortController;
progressListeners: Set<(event: JobProgressEvent) => void>;
config: BatchConfig;
activeCount: number;
isRunning: boolean;
}
const GLOBAL_KEY = '__mediaJobExecutor__' as const;
function getRunningJobs(): Map<string, RunningJob> {
if (!(globalThis as Record<string, unknown>)[GLOBAL_KEY]) {
(globalThis as Record<string, unknown>)[GLOBAL_KEY] = new Map<string, RunningJob>();
}
return (globalThis as Record<string, unknown>)[GLOBAL_KEY] as Map<string, RunningJob>;
}
const DEFAULT_CONFIG: BatchConfig = {
concurrency: 2,
maxRetries: 2,
retryDelayMs: 2000,
};
// ==========================================
// Progress Listener Management
// ==========================================
export function addProgressListener(jobId: string, listener: (event: JobProgressEvent) => void): () => void {
const running = getRunningJobs().get(jobId);
if (running) {
running.progressListeners.add(listener);
}
// Return cleanup function
return () => {
const r = getRunningJobs().get(jobId);
if (r) {
r.progressListeners.delete(listener);
}
};
}
function emitProgress(jobId: string, event: JobProgressEvent): void {
const running = getRunningJobs().get(jobId);
if (!running) return;
for (const listener of running.progressListeners) {
try {
listener(event);
} catch (err) {
console.warn('[job-executor] Progress listener error:', err);
}
}
}
function buildProgressSnapshot(jobId: string): JobProgressEvent['progress'] {
const items = getMediaJobItems(jobId);
return {
total: items.length,
completed: items.filter(i => i.status === 'completed').length,
failed: items.filter(i => i.status === 'failed').length,
processing: items.filter(i => i.status === 'processing').length,
};
}
// ==========================================
// Core Executor
// ==========================================
/**
* Start executing a job. Transitions job to 'running' and processes items with concurrency control.
*/
export async function startJob(jobId: string): Promise<void> {
const job = getMediaJob(jobId);
if (!job) throw new Error(`Job ${jobId} not found`);
if (job.status !== 'planned' && job.status !== 'paused') {
throw new Error(`Job ${jobId} cannot be started from status "${job.status}"`);
}
// Parse batch config
let config: BatchConfig;
try {
config = { ...DEFAULT_CONFIG, ...JSON.parse(job.batch_config) };
} catch {
config = { ...DEFAULT_CONFIG };
}
const abortController = new AbortController();
const runningJob: RunningJob = {
jobId,
abortController,
progressListeners: new Set(),
config,
activeCount: 0,
isRunning: true,
};
// Preserve existing listeners if resuming
const existing = getRunningJobs().get(jobId);
if (existing) {
for (const listener of existing.progressListeners) {
runningJob.progressListeners.add(listener);
}
}
getRunningJobs().set(jobId, runningJob);
updateMediaJobStatus(jobId, 'running');
// Execute the queue
try {
await executeQueue(runningJob);
} catch (err) {
console.error(`[job-executor] Job ${jobId} queue error:`, err);
}
}
async function executeQueue(runningJob: RunningJob): Promise<void> {
const { jobId, config, abortController } = runningJob;
while (runningJob.isRunning) {
// Get items that need processing
const pending = getPendingJobItems(jobId, config.maxRetries);
if (pending.length === 0 && runningJob.activeCount === 0) {
break; // All done
}
if (pending.length === 0) {
// Wait for active items to complete
await new Promise(resolve => setTimeout(resolve, 500));
continue;
}
// Fill up to concurrency limit
while (runningJob.isRunning && runningJob.activeCount < config.concurrency) {
const nextItems = getPendingJobItems(jobId, config.maxRetries);
const nextItem = nextItems.find(i => i.status === 'pending' || i.status === 'failed');
if (!nextItem) break;
runningJob.activeCount++;
processItem(runningJob, nextItem).finally(() => {
runningJob.activeCount--;
});
}
// Wait a bit before checking again
await new Promise(resolve => setTimeout(resolve, 300));
}
// Finalize the job
finalizeJob(jobId);
}
async function processItem(runningJob: RunningJob, item: MediaJobItem): Promise<void> {
const { jobId, abortController, config } = runningJob;
const now = new Date().toISOString();
// If this is a retry, wait with exponential backoff
if (item.retry_count > 0) {
const delay = config.retryDelayMs * Math.pow(3, item.retry_count - 1);
await new Promise(resolve => setTimeout(resolve, delay));
// Check if still running after delay
if (!runningJob.isRunning || abortController.signal.aborted) return;
}
// Mark as processing
updateMediaJobItem(item.id, { status: 'processing' });
emitProgress(jobId, {
type: 'item_started',
jobId,
itemId: item.id,
itemIdx: item.idx,
progress: buildProgressSnapshot(jobId),
timestamp: now,
});
try {
const result = await generateSingleImage({
prompt: item.prompt,
aspectRatio: item.aspect_ratio,
imageSize: item.image_size,
model: item.model || undefined,
sessionId: getMediaJob(jobId)?.session_id || undefined,
abortSignal: abortController.signal,
});
// Success
updateMediaJobItem(item.id, {
status: 'completed',
resultMediaGenerationId: result.mediaGenerationId,
error: null,
});
updateMediaJobCounters(jobId);
emitProgress(jobId, {
type: 'item_completed',
jobId,
itemId: item.id,
itemIdx: item.idx,
progress: buildProgressSnapshot(jobId),
mediaGenerationId: result.mediaGenerationId,
timestamp: new Date().toISOString(),
});
} catch (err) {
const errorMessage = err instanceof Error ? err.message : 'Unknown error';
const statusCode = extractStatusCode(err);
const isNonRetryable = statusCode !== undefined && [400, 401, 403].includes(statusCode);
const retriesExhausted = item.retry_count + 1 >= config.maxRetries;
if (isNonRetryable || retriesExhausted) {
// Mark as failed permanently
updateMediaJobItem(item.id, {
status: 'failed',
retryCount: item.retry_count + 1,
error: errorMessage,
});
updateMediaJobCounters(jobId);
emitProgress(jobId, {
type: 'item_failed',
jobId,
itemId: item.id,
itemIdx: item.idx,
progress: buildProgressSnapshot(jobId),
error: errorMessage,
retryCount: item.retry_count + 1,
timestamp: new Date().toISOString(),
});
} else {
// Mark for retry
updateMediaJobItem(item.id, {
status: 'failed',
retryCount: item.retry_count + 1,
error: errorMessage,
});
emitProgress(jobId, {
type: 'item_retry',
jobId,
itemId: item.id,
itemIdx: item.idx,
progress: buildProgressSnapshot(jobId),
error: errorMessage,
retryCount: item.retry_count + 1,
timestamp: new Date().toISOString(),
});
}
}
}
function finalizeJob(jobId: string): void {
const job = getMediaJob(jobId);
if (!job) return;
// Only finalize if the job was still running (not paused/cancelled externally)
if (job.status !== 'running') return;
updateMediaJobCounters(jobId);
const updatedJob = getMediaJob(jobId);
if (!updatedJob) return;
const items = getMediaJobItems(jobId);
const allDone = items.every(i => i.status === 'completed' || i.status === 'failed' || i.status === 'cancelled');
if (allDone) {
const hasFailures = items.some(i => i.status === 'failed');
const finalStatus = hasFailures && updatedJob.completed_items === 0 ? 'failed' : 'completed';
updateMediaJobStatus(jobId, finalStatus);
emitProgress(jobId, {
type: 'job_completed',
jobId,
progress: buildProgressSnapshot(jobId),
timestamp: new Date().toISOString(),
});
}
// Cleanup
getRunningJobs().delete(jobId);
}
// ==========================================
// Control Operations
// ==========================================
/**
* Pause a running job. Current items finish but no new items start.
*/
export function pauseJob(jobId: string): void {
const running = getRunningJobs().get(jobId);
if (!running) throw new Error(`Job ${jobId} is not running`);
running.isRunning = false;
updateMediaJobStatus(jobId, 'paused');
emitProgress(jobId, {
type: 'job_paused',
jobId,
progress: buildProgressSnapshot(jobId),
timestamp: new Date().toISOString(),
});
}
/**
* Cancel a running or paused job.
*/
export function cancelJob(jobId: string): void {
const running = getRunningJobs().get(jobId);
if (running) {
running.isRunning = false;
running.abortController.abort();
}
cancelPendingJobItems(jobId);
updateMediaJobStatus(jobId, 'cancelled');
emitProgress(jobId, {
type: 'job_cancelled',
jobId,
progress: buildProgressSnapshot(jobId),
timestamp: new Date().toISOString(),
});
getRunningJobs().delete(jobId);
}
/**
* Check if a job is currently running in memory.
*/
export function isJobRunning(jobId: string): boolean {
return getRunningJobs().has(jobId);
}
// ==========================================
// Helpers
// ==========================================
function extractStatusCode(err: unknown): number | undefined {
if (err && typeof err === 'object' && 'status' in err) {
return (err as { status: number }).status;
}
if (err instanceof Error && err.message) {
const match = err.message.match(/\b(4\d{2}|5\d{2})\b/);
if (match) return parseInt(match[1]);
}
return undefined;
}