Wire the app, worker, and cleanup entry points onto Prisma Client
Task 4 rewrote src/db.js and src/jobs/jobRepository.js to use Prisma instead of the hand-rolled mariadb pool. This updates every remaining call site (app.js, server.js, worker.js, cleanup.js) and the 5 test files that still referenced getPool/closePool/pool.query, so the app and full test suite compile and run against Prisma Client. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+4
-4
@@ -31,7 +31,7 @@ function registerAllConverters() {
|
||||
convertersRegistered = true;
|
||||
}
|
||||
|
||||
export function createApp(config, pool) {
|
||||
export function createApp(config, prisma) {
|
||||
registerAllConverters();
|
||||
|
||||
const app = express();
|
||||
@@ -131,7 +131,7 @@ export function createApp(config, pool) {
|
||||
}
|
||||
|
||||
const expiresAt = new Date(Date.now() + config.retentionHours * 3600 * 1000);
|
||||
await createJob(pool, {
|
||||
await createJob(prisma, {
|
||||
uuid,
|
||||
family: registryEntry.family,
|
||||
sourceFormat,
|
||||
@@ -151,7 +151,7 @@ export function createApp(config, pool) {
|
||||
});
|
||||
|
||||
app.get('/api/jobs/:id', async (req, res) => {
|
||||
const job = await getJobByUuid(pool, req.params.id);
|
||||
const job = await getJobByUuid(prisma, req.params.id);
|
||||
if (!job) {
|
||||
return res.status(404).json({ error: 'Job not found' });
|
||||
}
|
||||
@@ -173,7 +173,7 @@ export function createApp(config, pool) {
|
||||
}
|
||||
|
||||
app.get('/api/jobs/:id/download', async (req, res) => {
|
||||
const job = await getJobByUuid(pool, req.params.id);
|
||||
const job = await getJobByUuid(prisma, req.params.id);
|
||||
if (!job) {
|
||||
return res.status(404).json({ error: 'Job not found' });
|
||||
}
|
||||
|
||||
+7
-7
@@ -1,16 +1,16 @@
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { loadConfig } from './config.js';
|
||||
import { getPool, closePool } from './db.js';
|
||||
import { getPrismaClient, closePrismaClient } from './db.js';
|
||||
import { uploadPath, outputPath, deleteIfExists } from './storage.js';
|
||||
import { findExpiredJobs, markCleaned } from './jobs/jobRepository.js';
|
||||
|
||||
export async function runCleanup(pool, config) {
|
||||
const expiredJobs = await findExpiredJobs(pool);
|
||||
export async function runCleanup(prisma, config) {
|
||||
const expiredJobs = await findExpiredJobs(prisma);
|
||||
|
||||
for (const job of expiredJobs) {
|
||||
await deleteIfExists(uploadPath(config, job.uuid, job.sourceFormat));
|
||||
await deleteIfExists(outputPath(config, job.uuid, job.targetFormat));
|
||||
await markCleaned(pool, job.id);
|
||||
await markCleaned(prisma, job.id);
|
||||
}
|
||||
|
||||
return expiredJobs.length;
|
||||
@@ -18,10 +18,10 @@ export async function runCleanup(pool, config) {
|
||||
|
||||
async function main() {
|
||||
const config = loadConfig();
|
||||
const pool = getPool(config);
|
||||
const deletedCount = await runCleanup(pool, config);
|
||||
const prisma = getPrismaClient(config);
|
||||
const deletedCount = await runCleanup(prisma, config);
|
||||
console.log(`Cleanup: removed ${deletedCount} expired job(s).`);
|
||||
await closePool();
|
||||
await closePrismaClient();
|
||||
}
|
||||
|
||||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
|
||||
+3
-3
@@ -1,13 +1,13 @@
|
||||
import { loadConfig } from './config.js';
|
||||
import { getPool } from './db.js';
|
||||
import { getPrismaClient } from './db.js';
|
||||
import { ensureStorageDirs } from './storage.js';
|
||||
import { createApp } from './app.js';
|
||||
|
||||
async function main() {
|
||||
const config = loadConfig();
|
||||
await ensureStorageDirs(config);
|
||||
const pool = getPool(config);
|
||||
const app = createApp(config, pool);
|
||||
const prisma = getPrismaClient(config);
|
||||
const app = createApp(config, prisma);
|
||||
|
||||
app.listen(config.port, () => {
|
||||
console.log(`File converter API listening on port ${config.port}`);
|
||||
|
||||
+13
-13
@@ -1,7 +1,7 @@
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import fs from 'node:fs/promises';
|
||||
import { loadConfig } from './config.js';
|
||||
import { getPool } from './db.js';
|
||||
import { getPrismaClient } from './db.js';
|
||||
import { ensureStorageDirs, uploadPath, outputPath, deleteIfExists } from './storage.js';
|
||||
import { outputMimeType } from './mime.js';
|
||||
import { resolve as resolveConverter } from './converters/registry.js';
|
||||
@@ -20,8 +20,8 @@ function withTimeout(promise, ms) {
|
||||
return Promise.race([promise, timeout]).finally(() => clearTimeout(timeoutId));
|
||||
}
|
||||
|
||||
async function processJob(pool, config, job) {
|
||||
await markProcessing(pool, job.id);
|
||||
async function processJob(prisma, config, job) {
|
||||
await markProcessing(prisma, job.id);
|
||||
|
||||
const inputFilePath = uploadPath(config, job.uuid, job.sourceFormat);
|
||||
const outputFilePath = outputPath(config, job.uuid, job.targetFormat);
|
||||
@@ -37,7 +37,7 @@ async function processJob(pool, config, job) {
|
||||
const conversionDurationSeconds = (Date.now() - startedAt) / 1000;
|
||||
const { size: outputSizeBytes } = await fs.stat(outputFilePath);
|
||||
|
||||
await markDone(pool, job.id, {
|
||||
await markDone(prisma, job.id, {
|
||||
outputPath: `${job.uuid}.${job.targetFormat}`,
|
||||
outputMimeType: outputMimeType(job.targetFormat),
|
||||
outputSizeBytes,
|
||||
@@ -45,22 +45,22 @@ async function processJob(pool, config, job) {
|
||||
});
|
||||
} catch (error) {
|
||||
await deleteIfExists(outputFilePath);
|
||||
await markFailed(pool, job.id, {
|
||||
await markFailed(prisma, 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)));
|
||||
export async function processPendingJobs(prisma, config) {
|
||||
const jobs = await findPendingJobs(prisma, config.workerConcurrency);
|
||||
await Promise.all(jobs.map((job) => processJob(prisma, config, job)));
|
||||
return jobs.length;
|
||||
}
|
||||
|
||||
export function startWorker(pool, config) {
|
||||
export function startWorker(prisma, config) {
|
||||
const interval = setInterval(() => {
|
||||
processPendingJobs(pool, config).catch((error) => {
|
||||
processPendingJobs(prisma, config).catch((error) => {
|
||||
console.error('Error while processing pending jobs:', error);
|
||||
});
|
||||
}, config.workerPollIntervalMs);
|
||||
@@ -70,14 +70,14 @@ export function startWorker(pool, config) {
|
||||
async function main() {
|
||||
const config = loadConfig();
|
||||
await ensureStorageDirs(config);
|
||||
const pool = getPool(config);
|
||||
const prisma = getPrismaClient(config);
|
||||
|
||||
registerImageConverters();
|
||||
registerImageToPdfConverter();
|
||||
registerDocumentConverters();
|
||||
|
||||
startWorker(pool, config);
|
||||
startWorker(prisma, config);
|
||||
console.log(`Worker started, polling every ${config.workerPollIntervalMs}ms`);
|
||||
}
|
||||
|
||||
main();
|
||||
main();
|
||||
|
||||
Reference in New Issue
Block a user