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:
+63
-27
@@ -5,7 +5,7 @@ import os from 'node:os';
|
||||
import { getPool, closePool } from '../src/db.js';
|
||||
import { loadConfig } from '../src/config.js';
|
||||
import { ensureStorageDirs, uploadPath, outputPath } from '../src/storage.js';
|
||||
import { createJob, markDone, getJobById } from '../src/jobs/jobRepository.js';
|
||||
import { createJob, markDone, getJobByUuid } from '../src/jobs/jobRepository.js';
|
||||
import { runCleanup } from '../src/cleanup.js';
|
||||
|
||||
let pool;
|
||||
@@ -27,66 +27,102 @@ beforeEach(async () => {
|
||||
});
|
||||
|
||||
describe('runCleanup', () => {
|
||||
it('deletes an expired done job, its input file, and its output file', async () => {
|
||||
const id = 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee';
|
||||
await fs.writeFile(uploadPath(config, id, 'png'), 'input bytes');
|
||||
await fs.writeFile(outputPath(config, id, 'webp'), 'output bytes');
|
||||
it('marks an expired done job cleaned and removes its input and output files, keeping the row', async () => {
|
||||
const uuid = 'eeeeeeee-eeee-4eee-8eee-eeeeeeeeeeee';
|
||||
await fs.writeFile(uploadPath(config, uuid, 'png'), 'input bytes');
|
||||
await fs.writeFile(outputPath(config, uuid, 'webp'), 'output bytes');
|
||||
await createJob(pool, {
|
||||
id,
|
||||
uuid,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputPath: `${uuid}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
inputSizeBytes: 11,
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
});
|
||||
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: 12,
|
||||
conversionDurationSeconds: 0.5,
|
||||
});
|
||||
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
const cleanedCount = await runCleanup(pool, config);
|
||||
|
||||
expect(deletedCount).toBe(1);
|
||||
expect(await getJobById(pool, id)).toBeNull();
|
||||
await expect(fs.stat(uploadPath(config, id, 'png'))).rejects.toThrow();
|
||||
await expect(fs.stat(outputPath(config, id, 'webp'))).rejects.toThrow();
|
||||
expect(cleanedCount).toBe(1);
|
||||
const job = await getJobByUuid(pool, uuid);
|
||||
expect(job).not.toBeNull();
|
||||
expect(job.cleanedAt).not.toBeNull();
|
||||
await expect(fs.stat(uploadPath(config, uuid, 'png'))).rejects.toThrow();
|
||||
await expect(fs.stat(outputPath(config, uuid, 'webp'))).rejects.toThrow();
|
||||
});
|
||||
|
||||
it('deletes an expired pending job (with no output file) without throwing', async () => {
|
||||
const id = 'ffffffff-ffff-4fff-8fff-ffffffffffff';
|
||||
await fs.writeFile(uploadPath(config, id, 'png'), 'input bytes');
|
||||
it('marks an expired pending job (with no output file) cleaned without throwing', async () => {
|
||||
const uuid = 'ffffffff-ffff-4fff-8fff-ffffffffffff';
|
||||
await fs.writeFile(uploadPath(config, uuid, 'png'), 'input bytes');
|
||||
await createJob(pool, {
|
||||
id,
|
||||
uuid,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputPath: `${uuid}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
inputSizeBytes: 11,
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
});
|
||||
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
const cleanedCount = await runCleanup(pool, config);
|
||||
|
||||
expect(deletedCount).toBe(1);
|
||||
expect(await getJobById(pool, id)).toBeNull();
|
||||
expect(cleanedCount).toBe(1);
|
||||
const job = await getJobByUuid(pool, uuid);
|
||||
expect(job).not.toBeNull();
|
||||
expect(job.cleanedAt).not.toBeNull();
|
||||
});
|
||||
|
||||
it('leaves non-expired jobs untouched', async () => {
|
||||
const id = '12121212-1212-4212-8212-121212121212';
|
||||
const uuid = '12121212-1212-4212-8212-121212121212';
|
||||
await createJob(pool, {
|
||||
id,
|
||||
uuid,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputPath: `${uuid}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
inputSizeBytes: 11,
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||
});
|
||||
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
const cleanedCount = await runCleanup(pool, config);
|
||||
|
||||
expect(deletedCount).toBe(0);
|
||||
expect(await getJobById(pool, id)).not.toBeNull();
|
||||
expect(cleanedCount).toBe(0);
|
||||
const job = await getJobByUuid(pool, uuid);
|
||||
expect(job).not.toBeNull();
|
||||
expect(job.cleanedAt).toBeNull();
|
||||
});
|
||||
|
||||
it('does not re-process an already cleaned expired job', async () => {
|
||||
const uuid = '13131313-1313-4313-8313-131313131313';
|
||||
await createJob(pool, {
|
||||
uuid,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${uuid}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
inputSizeBytes: 11,
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
});
|
||||
|
||||
const firstRun = await runCleanup(pool, config);
|
||||
const secondRun = await runCleanup(pool, config);
|
||||
|
||||
expect(firstRun).toBe(1);
|
||||
expect(secondRun).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user