feat: give ebook conversions their own worker timeout and register the converter

This commit is contained in:
2026-07-31 13:24:49 +02:00
parent f796c0bd97
commit 534630ab4e
2 changed files with 46 additions and 3 deletions
+41 -1
View File
@@ -1,5 +1,6 @@
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
import fs from 'node:fs/promises';
import { writeFileSync } from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import sharp from 'sharp';
@@ -9,14 +10,19 @@ import { ensureStorageDirs, uploadPath, outputPath } from '../src/storage.js';
import { createJob, getJobByUuid, getJobErrorLog } from '../src/jobs/jobRepository.js';
import { registerImageConverters } from '../src/converters/image.js';
import { registerIcoConverter } from '../src/converters/ico.js';
import { registerEbookConverter } from '../src/converters/ebook.js';
import { processPendingJobs } from '../src/worker.js';
const { execFileMock } = vi.hoisted(() => ({ execFileMock: vi.fn() }));
vi.mock('node:child_process', () => ({ execFile: execFileMock }));
let prisma;
let config;
beforeAll(async () => {
registerImageConverters();
registerIcoConverter();
registerEbookConverter();
config = { ...loadConfig(), storageDir: await fs.mkdtemp(path.join(os.tmpdir(), 'converter-worker-')) };
await ensureStorageDirs(config);
prisma = getPrismaClient(config);
@@ -187,4 +193,38 @@ describe('processPendingJobs', () => {
const [image] = await decodeIco(await fs.readFile(outputFilePath), 'image/png');
expect(image.width).toBe(32);
});
it('converts a pending ebook job to done using config.ebookJobTimeoutMs, not the default 60s timeout', async () => {
execFileMock.mockReset();
execFileMock.mockImplementation((file, args, options, callback) => {
writeFileSync(args[1], 'fake converted ebook output');
callback(null, '', '');
});
const uuid = '11111111-1111-4111-8111-111111111111';
const inputFilePath = uploadPath(config, uuid, 'epub');
await fs.writeFile(inputFilePath, 'fake epub content');
await createJob(prisma, {
uuid,
family: 'ebook',
sourceFormat: 'epub',
targetFormat: 'pdf',
originalFilename: 'book.epub',
inputPath: `${uuid}.epub`,
inputMimeType: 'application/epub+zip',
inputSizeBytes: 18,
expiresAt: new Date(Date.now() + 3600 * 1000),
});
await processPendingJobs(prisma, config);
const job = await getJobByUuid(prisma, uuid);
expect(job.status).toBe('done');
const [file, args, options] = execFileMock.mock.calls[0];
expect(file).toBe('ebook-convert');
expect(args).toEqual([inputFilePath, outputPath(config, uuid, 'pdf')]);
expect(options).toEqual({ timeout: config.ebookJobTimeoutMs });
expect(config.ebookJobTimeoutMs).not.toBe(60000);
});
});