diff --git a/src/lib/storage/jobs/ConversionJob.ts b/src/lib/storage/jobs/ConversionJob.ts index b43be5e58..eef6e826e 100644 --- a/src/lib/storage/jobs/ConversionJob.ts +++ b/src/lib/storage/jobs/ConversionJob.ts @@ -60,8 +60,8 @@ export default class ConversionJob { this.raw = record as Job; } - isStarted() { - return this.raw?.status === 'started'; + canStart() { + return this.raw?.status === 'failed' || this.raw?.status === 'started'; } async setStatus(status: JobStatus) { @@ -171,6 +171,7 @@ export default class ConversionJob { }); return decks; } catch (error) { + sendError(error); await this.failed(); return undefined; } @@ -208,7 +209,6 @@ export default class ConversionJob { key, size_mb: size, }); - await this.completed(); return { size, key, apkg }; }; } diff --git a/src/lib/storage/jobs/helpers/getAllStartedJobs.js.ts b/src/lib/storage/jobs/helpers/getAllStartedJobs.js.ts index 73c880dbc..b885e8d27 100644 --- a/src/lib/storage/jobs/helpers/getAllStartedJobs.js.ts +++ b/src/lib/storage/jobs/helpers/getAllStartedJobs.js.ts @@ -5,8 +5,5 @@ export const getAllMyJobs = async ( db: Knex, owner: string ): Promise> => { - return db('jobs') - .where({ owner }) - .andWhereNot({ status: 'completed' }) - .returning(['*']); + return db('jobs').where({ owner }).returning(['*']); }; diff --git a/src/lib/storage/jobs/helpers/performConversion.ts b/src/lib/storage/jobs/helpers/performConversion.ts index 87bcc25dc..cd78849ef 100644 --- a/src/lib/storage/jobs/helpers/performConversion.ts +++ b/src/lib/storage/jobs/helpers/performConversion.ts @@ -29,7 +29,7 @@ export default async function performConversion({ const job = new ConversionJob(DB); await job.load(id, owner, title); try { - if (!job.isStarted()) { + if (!job.canStart()) { console.log(`job ${id} was not started`); return res ? res.status(405).send({ message: 'Job is already active' }) @@ -70,8 +70,9 @@ export default async function performConversion({ id, apkg, }); + await job.completed(); } catch (error) { - await job.failed(); sendError(error); + await job.failed(); } } diff --git a/src/routes/upload/deleteJob.ts b/src/routes/upload/deleteJob.ts index 727f089b3..015b23d56 100644 --- a/src/routes/upload/deleteJob.ts +++ b/src/routes/upload/deleteJob.ts @@ -1,5 +1,4 @@ import { Request, Response } from 'express'; -import ConversionJob from '../../lib/storage/jobs/ConversionJob'; import DB from '../../lib/storage/db'; import { sendError } from '../../lib/error/sendError'; @@ -7,9 +6,10 @@ export default async function deleteJob(req: Request, res: Response) { console.log('delete job', req.params.id); try { const id = req.params.id; - const c = new ConversionJob(DB); - await c.load(id, res.locals.owner); - await c.completed(); + await DB('jobs').delete().where({ + id: id, + owner: res.locals.owner, + }); res.status(200).send(); } catch (err) { res.status(500).send(); diff --git a/src/routes/upload/index.ts b/src/routes/upload/index.ts index 4835c8f9e..79b4687ea 100644 --- a/src/routes/upload/index.ts +++ b/src/routes/upload/index.ts @@ -28,8 +28,8 @@ router.post('/file', RequireAllowedOrigin, async (req, res) => { }); router.get('/mine', RequireAuthentication, getUploads); -router.get('/active', RequireAuthentication, getAllJobs); -router.delete('/active/:id', RequireAuthentication, deleteJob); +router.get('/jobs', RequireAuthentication, getAllJobs); +router.delete('/jobs/:id', RequireAuthentication, deleteJob); router.delete('/mine/:key', RequireAuthentication, deleteUpload); export default router; diff --git a/src/server.ts b/src/server.ts index 453701967..17bd85da5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -5,11 +5,6 @@ import morgan from 'morgan'; import express, { RequestHandler } from 'express'; import cookieParser from 'cookie-parser'; import * as dotenv from 'dotenv'; -const localEnvFile = path.join(__dirname, '/../.env'); -if (existsSync(localEnvFile)) { - dotenv.config({ path: localEnvFile }); -} - import { ALLOWED_ORIGINS, BUILD_DIR, INDEX_FILE } from './lib/constants'; import ErrorHandler from './lib/misc/ErrorHandler'; @@ -32,9 +27,14 @@ import CrashReporter from './lib/CrashReporter'; import { ScheduleCleanup } from './lib/storage/jobs/JobHandler'; import RequireAuthentication from './middleware/RequireAuthentication'; import { Knex } from 'knex'; +import { sendError } from './lib/error/sendError'; + +const localEnvFile = path.join(__dirname, '/../.env'); +if (existsSync(localEnvFile)) { + dotenv.config({ path: localEnvFile }); +} import MigratorConfig = Knex.MigratorConfig; -import { sendError } from './lib/error/sendError'; function serve() { const templateDir = path.join(__dirname, 'templates');