feat: add image derivative worker lifecycle
Add worker-owned image derivative job execution with explicit PROCESSING, COMPLETED, and FAILED outcomes, plus tests and documentation. Verification: npm test passed 70 tests, worker tests passed 2 tests, and integration passed 14 tests. No build script is configured.
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
export async function runImageDerivativeJob({ job, loadSource, processor }) {
|
||||
if (!job || job.type !== 'IMAGE_DERIVATIVES') {
|
||||
throw new Error('Unsupported image derivative job.');
|
||||
}
|
||||
|
||||
const processingJob = { ...job, status: 'PROCESSING', error: null };
|
||||
|
||||
try {
|
||||
const source = await loadSource(job.sourceKey);
|
||||
const derivatives = await processor.process(source);
|
||||
|
||||
return {
|
||||
...processingJob,
|
||||
status: 'COMPLETED',
|
||||
derivatives,
|
||||
completedAt: new Date(),
|
||||
};
|
||||
} catch (error) {
|
||||
return {
|
||||
...processingJob,
|
||||
status: 'FAILED',
|
||||
error: error instanceof Error ? error.message : 'Image derivative processing failed.',
|
||||
failedAt: new Date(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { runImageDerivativeJob } from '../src/image-derivative-worker.js';
|
||||
|
||||
test('completes an image derivative job after loading and processing the source', async () => {
|
||||
const calls = [];
|
||||
const result = await runImageDerivativeJob({
|
||||
job: { id: 'job-1', type: 'IMAGE_DERIVATIVES', sourceKey: 'source.jpg', status: 'PENDING' },
|
||||
async loadSource(sourceKey) {
|
||||
calls.push(`load:${sourceKey}`);
|
||||
return { sourceKey, data: Buffer.from('image') };
|
||||
},
|
||||
processor: {
|
||||
async process(source) {
|
||||
calls.push(`process:${source.sourceKey}`);
|
||||
return [{ name: 'thumbnail', key: 'source-thumbnail.webp' }];
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
assert.equal(result.status, 'COMPLETED');
|
||||
assert.deepEqual(result.derivatives, [{ name: 'thumbnail', key: 'source-thumbnail.webp' }]);
|
||||
assert.deepEqual(calls, ['load:source.jpg', 'process:source.jpg']);
|
||||
assert.ok(result.completedAt instanceof Date);
|
||||
});
|
||||
|
||||
test('records a failed image derivative job without throwing worker errors', async () => {
|
||||
const result = await runImageDerivativeJob({
|
||||
job: { id: 'job-2', type: 'IMAGE_DERIVATIVES', sourceKey: 'missing.jpg', status: 'PENDING' },
|
||||
async loadSource() {
|
||||
throw new Error('source unavailable');
|
||||
},
|
||||
processor: { async process() { return []; } },
|
||||
});
|
||||
|
||||
assert.equal(result.status, 'FAILED');
|
||||
assert.equal(result.error, 'source unavailable');
|
||||
assert.ok(result.failedAt instanceof Date);
|
||||
});
|
||||
Reference in New Issue
Block a user