Spaces:
Sleeping
Sleeping
File size: 4,682 Bytes
641b62c | 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 | /**
* ExecutionQueue.ts — FIFO task queue with retry, cancel, and timeout
*
* Manages all code execution tasks — prevents race conditions and
* ensures stable ordering for Judge0 and worker-based runs.
*/
export type TaskStatus = "pending" | "running" | "success" | "error" | "timeout" | "cancelled";
export interface ExecutionTask<T = unknown> {
id: string;
status: TaskStatus;
result?: T;
error?: string;
startedAt?: number;
finishedAt?: number;
attempts: number;
}
export interface QueueOptions {
maxRetries?: number;
retryDelay?: number;
defaultTimeout?: number;
concurrency?: number;
}
type TaskRunner<T> = (signal: AbortSignal) => Promise<T>;
interface InternalTask<T> {
id: string;
runner: TaskRunner<T>;
resolve: (val: T) => void;
reject: (err: unknown) => void;
timeout: number;
retries: number;
retryDelay: number;
attempts: number;
abortCtrl?: AbortController;
}
export class ExecutionQueue {
private queue: InternalTask<unknown>[] = [];
private running = 0;
private readonly concurrency: number;
private readonly defaultTimeout: number;
private readonly defaultRetries: number;
private readonly defaultRetryDelay: number;
private readonly tasks = new Map<string, ExecutionTask>();
constructor(opts: QueueOptions = {}) {
this.concurrency = opts.concurrency ?? 2;
this.defaultTimeout = opts.defaultTimeout ?? 30_000;
this.defaultRetries = opts.maxRetries ?? 2;
this.defaultRetryDelay = opts.retryDelay ?? 1000;
}
enqueue<T>(
id: string,
runner: TaskRunner<T>,
opts: { timeout?: number; retries?: number; retryDelay?: number } = {},
): Promise<T> {
return new Promise<T>((resolve, reject) => {
const task: InternalTask<T> = {
id,
runner,
resolve: resolve as (v: unknown) => void,
reject,
timeout: opts.timeout ?? this.defaultTimeout,
retries: opts.retries ?? this.defaultRetries,
retryDelay: opts.retryDelay ?? this.defaultRetryDelay,
attempts: 0,
};
this.tasks.set(id, { id, status: "pending", attempts: 0 });
this.queue.push(task as InternalTask<unknown>);
this.drain();
});
}
cancel(id: string): boolean {
const inQueue = this.queue.findIndex(t => t.id === id);
if (inQueue !== -1) {
const [task] = this.queue.splice(inQueue, 1);
const record = this.tasks.get(id);
if (record) record.status = "cancelled";
task.reject(new Error("Esecuzione annullata"));
return true;
}
const task = this.queue.find(t => t.id === id);
if (task?.abortCtrl) {
task.abortCtrl.abort();
return true;
}
return false;
}
getStatus(id: string): ExecutionTask | undefined {
return this.tasks.get(id);
}
private drain() {
while (this.running < this.concurrency && this.queue.length > 0) {
const task = this.queue.shift()!;
this.runTask(task);
}
}
private async runTask(task: InternalTask<unknown>) {
this.running++;
const record = this.tasks.get(task.id)!;
record.status = "running";
record.startedAt = Date.now();
while (task.attempts <= task.retries) {
task.attempts++;
record.attempts = task.attempts;
const ctrl = new AbortController();
task.abortCtrl = ctrl;
const timer = setTimeout(() => ctrl.abort(), task.timeout);
try {
const result = await task.runner(ctrl.signal);
clearTimeout(timer);
record.status = "success";
record.result = result;
record.finishedAt = Date.now();
task.resolve(result);
break;
} catch (err) {
clearTimeout(timer);
if (ctrl.signal.aborted && ctrl.signal.reason === undefined) {
record.status = "timeout";
record.error = "Timeout esecuzione";
record.finishedAt = Date.now();
task.reject(new Error("Timeout esecuzione"));
break;
}
const isAborted = err instanceof Error && err.name === "AbortError";
if (isAborted) {
record.status = "cancelled";
record.finishedAt = Date.now();
task.reject(err);
break;
}
if (task.attempts > task.retries) {
record.status = "error";
record.error = err instanceof Error ? err.message : String(err);
record.finishedAt = Date.now();
task.reject(err);
break;
}
await new Promise(r => setTimeout(r, task.retryDelay * task.attempts));
}
}
this.running--;
this.drain();
}
}
export const executionQueue = new ExecutionQueue({ concurrency: 2, maxRetries: 2 });
|