34 lines
754 B
TypeScript
34 lines
754 B
TypeScript
import { rm } from "node:fs/promises";
|
|
|
|
type Job = {
|
|
dir: string;
|
|
zipPath: string;
|
|
createdAt: number;
|
|
};
|
|
|
|
const JOB_TTL_MS = 15 * 60 * 1000;
|
|
const jobs = new Map<string, Job>();
|
|
|
|
function sweepExpiredJobs(): void {
|
|
const cutoff = Date.now() - JOB_TTL_MS;
|
|
for (const [id, job] of jobs) {
|
|
if (job.createdAt > cutoff) continue;
|
|
jobs.delete(id);
|
|
void rm(job.dir, { recursive: true, force: true });
|
|
}
|
|
}
|
|
|
|
export function saveJob(dir: string, zipPath: string): string {
|
|
sweepExpiredJobs();
|
|
const id = crypto.randomUUID();
|
|
jobs.set(id, { dir, zipPath, createdAt: Date.now() });
|
|
return id;
|
|
}
|
|
|
|
export function takeJob(id: string): Job | null {
|
|
const job = jobs.get(id);
|
|
if (!job) return null;
|
|
jobs.delete(id);
|
|
return job;
|
|
}
|