feat: add worker process with bounded concurrency and per-job timeout

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-07-30 09:21:42 +02:00
co-authored by Claude Sonnet 5
parent ceccc1d1af
commit 360e0ee7a6
2 changed files with 177 additions and 0 deletions
+78
View File
@@ -0,0 +1,78 @@
import { loadConfig } from './config.js';
import { getPool } from './db.js';
import { ensureStorageDirs, uploadPath, outputPath, deleteIfExists } from './storage.js';
import { outputMimeType } from './mime.js';
import { resolve as resolveConverter } from './converters/registry.js';
import { registerImageConverters } from './converters/image.js';
import { registerImageToPdfConverter } from './converters/imageToPdf.js';
import { registerDocumentConverters } from './converters/document.js';
import { findPendingJobs, markProcessing, markDone, markFailed } from './jobs/jobRepository.js';
const JOB_TIMEOUT_MS = 60000;
function withTimeout(promise, ms) {
let timeoutId;
const timeout = new Promise((_, reject) => {
timeoutId = setTimeout(() => reject(new Error(`Conversion timed out after ${ms}ms`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timeoutId));
}
async function processJob(pool, config, job) {
await markProcessing(pool, job.id);
const inputFilePath = uploadPath(config, job.id, job.sourceFormat);
const outputFilePath = outputPath(config, job.id, job.targetFormat);
try {
const entry = resolveConverter(job.sourceFormat, job.targetFormat);
if (!entry) {
throw new Error(`No converter registered for ${job.sourceFormat} -> ${job.targetFormat}`);
}
await withTimeout(entry.convert(inputFilePath, outputFilePath), JOB_TIMEOUT_MS);
await markDone(pool, job.id, {
outputPath: `${job.id}.${job.targetFormat}`,
outputMimeType: outputMimeType(job.targetFormat),
});
} catch (error) {
await deleteIfExists(outputFilePath);
await markFailed(pool, job.id, {
errorMessage: 'Conversion failed, please try again.',
errorLog: error.stack ?? String(error),
});
}
}
export async function processPendingJobs(pool, config) {
const jobs = await findPendingJobs(pool, config.workerConcurrency);
await Promise.all(jobs.map((job) => processJob(pool, config, job)));
return jobs.length;
}
export function startWorker(pool, config) {
const interval = setInterval(() => {
processPendingJobs(pool, config).catch((error) => {
console.error('Error while processing pending jobs:', error);
});
}, config.workerPollIntervalMs);
return () => clearInterval(interval);
}
async function main() {
const config = loadConfig();
await ensureStorageDirs(config);
const pool = getPool(config);
registerImageConverters();
registerImageToPdfConverter();
registerDocumentConverters();
startWorker(pool, config);
console.log(`Worker started, polling every ${config.workerPollIntervalMs}ms`);
}
if (import.meta.url === `file://${process.argv[1]}`) {
main();
}