feat: add auto-increment id, size/duration tracking, and soft cleanup to conversion_jobs
Rename the old CHAR(36) id to uuid (still used for public URLs and file naming) and add a real auto-increment id as the primary key. Track input/output file size and conversion duration per job. Cleanup no longer deletes rows; it marks cleaned_at and skips already-cleaned expired jobs on later runs. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+23
-15
@@ -6,7 +6,7 @@ import os from 'node:os';
|
||||
import { createApp } from '../../src/app.js';
|
||||
import { getPool, closePool } from '../../src/db.js';
|
||||
import { loadConfig } from '../../src/config.js';
|
||||
import { createJob, markDone } from '../../src/jobs/jobRepository.js';
|
||||
import { createJob, getJobByUuid, markDone } from '../../src/jobs/jobRepository.js';
|
||||
import { ensureStorageDirs, outputPath } from '../../src/storage.js';
|
||||
|
||||
let app;
|
||||
@@ -29,27 +29,29 @@ beforeEach(async () => {
|
||||
await pool.query('DELETE FROM conversion_jobs');
|
||||
});
|
||||
|
||||
function baseJob(id) {
|
||||
function baseJob(uuid) {
|
||||
return {
|
||||
id,
|
||||
uuid,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'holiday photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputPath: `${uuid}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
inputSizeBytes: 11,
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||
};
|
||||
}
|
||||
|
||||
describe('GET /api/jobs/:id', () => {
|
||||
it('returns job status without the error log field', async () => {
|
||||
const id = '66666666-6666-4666-8666-666666666666';
|
||||
await createJob(pool, baseJob(id));
|
||||
const uuid = '66666666-6666-4666-8666-666666666666';
|
||||
await createJob(pool, baseJob(uuid));
|
||||
|
||||
const response = await request(app).get(`/api/jobs/${id}`);
|
||||
const response = await request(app).get(`/api/jobs/${uuid}`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.body.id).toBe(uuid);
|
||||
expect(response.body.status).toBe('pending');
|
||||
expect(response.body.originalFilename).toBe('holiday photo.png');
|
||||
expect(response.body.errorLog).toBeUndefined();
|
||||
@@ -63,13 +65,19 @@ describe('GET /api/jobs/:id', () => {
|
||||
|
||||
describe('GET /api/jobs/:id/download', () => {
|
||||
it('streams the converted file with correct headers once done', async () => {
|
||||
const id = '77777777-7777-4777-8777-777777777777';
|
||||
await createJob(pool, baseJob(id));
|
||||
const filePath = outputPath(config, id, 'webp');
|
||||
const uuid = '77777777-7777-4777-8777-777777777777';
|
||||
await createJob(pool, baseJob(uuid));
|
||||
const filePath = outputPath(config, uuid, 'webp');
|
||||
await fs.writeFile(filePath, Buffer.from('fake webp bytes'));
|
||||
await markDone(pool, id, { outputPath: `${id}.webp`, outputMimeType: 'image/webp' });
|
||||
const created = await getJobByUuid(pool, uuid);
|
||||
await markDone(pool, created.id, {
|
||||
outputPath: `${uuid}.webp`,
|
||||
outputMimeType: 'image/webp',
|
||||
outputSizeBytes: 16,
|
||||
conversionDurationSeconds: 0.2,
|
||||
});
|
||||
|
||||
const response = await request(app).get(`/api/jobs/${id}/download`);
|
||||
const response = await request(app).get(`/api/jobs/${uuid}/download`);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(response.headers['content-type']).toBe('image/webp');
|
||||
@@ -78,10 +86,10 @@ describe('GET /api/jobs/:id/download', () => {
|
||||
});
|
||||
|
||||
it('returns 409 when the job is not done yet', async () => {
|
||||
const id = '88888888-8888-4888-8888-888888888888';
|
||||
await createJob(pool, baseJob(id));
|
||||
const uuid = '88888888-8888-4888-8888-888888888888';
|
||||
await createJob(pool, baseJob(uuid));
|
||||
|
||||
const response = await request(app).get(`/api/jobs/${id}/download`);
|
||||
const response = await request(app).get(`/api/jobs/${uuid}/download`);
|
||||
|
||||
expect(response.status).toBe(409);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user