feat: add queued image derivative lifecycle
This commit is contained in:
@@ -1,3 +1,90 @@
|
||||
export function createInMemoryImageDerivativeQueue() {
|
||||
const jobs = new Map();
|
||||
const pendingIds = [];
|
||||
let activeJobId = null;
|
||||
|
||||
return {
|
||||
enqueue(job) {
|
||||
if (!job || !job.id) {
|
||||
throw new Error('Image derivative job requires an id.');
|
||||
}
|
||||
|
||||
const persisted = {
|
||||
...job,
|
||||
status: job.status ?? 'PENDING',
|
||||
error: null,
|
||||
derivatives: job.derivatives ?? [],
|
||||
};
|
||||
|
||||
jobs.set(job.id, persisted);
|
||||
pendingIds.push(job.id);
|
||||
return persisted;
|
||||
},
|
||||
|
||||
claimNext() {
|
||||
const nextId = pendingIds.shift();
|
||||
if (!nextId) return null;
|
||||
|
||||
const current = jobs.get(nextId);
|
||||
if (!current) return null;
|
||||
|
||||
activeJobId = nextId;
|
||||
const claimed = {
|
||||
...current,
|
||||
status: 'PROCESSING',
|
||||
error: null,
|
||||
startedAt: new Date(),
|
||||
};
|
||||
|
||||
jobs.set(nextId, claimed);
|
||||
return claimed;
|
||||
},
|
||||
|
||||
getCurrentJob() {
|
||||
if (!activeJobId) return null;
|
||||
return jobs.get(activeJobId) ?? null;
|
||||
},
|
||||
|
||||
getJob(id) {
|
||||
return jobs.get(id) ?? null;
|
||||
},
|
||||
|
||||
updateJob(id, updates) {
|
||||
const current = jobs.get(id);
|
||||
if (!current) return null;
|
||||
|
||||
const next = {
|
||||
...current,
|
||||
...updates,
|
||||
status: updates.status ?? current.status,
|
||||
};
|
||||
|
||||
jobs.set(id, next);
|
||||
if (next.status === 'COMPLETED' || next.status === 'FAILED') {
|
||||
if (activeJobId === id) {
|
||||
activeJobId = null;
|
||||
}
|
||||
}
|
||||
return next;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function processNextImageDerivativeJob({ queue, loadSource, processor }) {
|
||||
if (!queue || typeof queue.claimNext !== 'function') {
|
||||
throw new TypeError('Image derivative queue is required.');
|
||||
}
|
||||
|
||||
const claimed = queue.getCurrentJob() ?? queue.claimNext();
|
||||
if (!claimed) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const result = await runImageDerivativeJob({ job: claimed, loadSource, processor });
|
||||
queue.updateJob(claimed.id, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function runImageDerivativeJob({ job, loadSource, processor }) {
|
||||
if (!job || job.type !== 'IMAGE_DERIVATIVES') {
|
||||
throw new Error('Unsupported image derivative job.');
|
||||
|
||||
Reference in New Issue
Block a user