feat: add cleanup script and o2switch deployment docs
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
||||
import fs from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
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 { runCleanup } from '../src/cleanup.js';
|
||||
|
||||
let pool;
|
||||
let config;
|
||||
|
||||
beforeAll(async () => {
|
||||
config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-cleanup-')) };
|
||||
await ensureStorageDirs(config);
|
||||
pool = getPool(config);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await closePool();
|
||||
await fs.rm(config.storageDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await pool.query('DELETE FROM conversion_jobs');
|
||||
});
|
||||
|
||||
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');
|
||||
await createJob(pool, {
|
||||
id,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
});
|
||||
await markDone(pool, id, { outputPath: `${id}.webp`, outputMimeType: 'image/webp' });
|
||||
|
||||
const deletedCount = 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();
|
||||
});
|
||||
|
||||
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');
|
||||
await createJob(pool, {
|
||||
id,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
expiresAt: new Date(Date.now() - 1000),
|
||||
});
|
||||
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
|
||||
expect(deletedCount).toBe(1);
|
||||
expect(await getJobById(pool, id)).toBeNull();
|
||||
});
|
||||
|
||||
it('leaves non-expired jobs untouched', async () => {
|
||||
const id = '12121212-1212-4212-8212-121212121212';
|
||||
await createJob(pool, {
|
||||
id,
|
||||
family: 'image',
|
||||
sourceFormat: 'png',
|
||||
targetFormat: 'webp',
|
||||
originalFilename: 'photo.png',
|
||||
inputPath: `${id}.png`,
|
||||
inputMimeType: 'image/png',
|
||||
expiresAt: new Date(Date.now() + 3600 * 1000),
|
||||
});
|
||||
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
|
||||
expect(deletedCount).toBe(0);
|
||||
expect(await getJobById(pool, id)).not.toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user