Skip to content

Commit

Permalink
feat: make jobs restartable
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Jan 20, 2023
1 parent f00b77c commit 5b6f04a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/lib/storage/jobs/ConversionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -171,6 +171,7 @@ export default class ConversionJob {
});
return decks;
} catch (error) {
sendError(error);
await this.failed();
return undefined;
}
Expand Down Expand Up @@ -208,7 +209,6 @@ export default class ConversionJob {
key,
size_mb: size,
});
await this.completed();
return { size, key, apkg };
};
}
5 changes: 1 addition & 4 deletions src/lib/storage/jobs/helpers/getAllStartedJobs.js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ export const getAllMyJobs = async (
db: Knex,
owner: string
): Promise<Array<Job>> => {
return db('jobs')
.where({ owner })
.andWhereNot({ status: 'completed' })
.returning(['*']);
return db('jobs').where({ owner }).returning(['*']);
};
5 changes: 3 additions & 2 deletions src/lib/storage/jobs/helpers/performConversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down Expand Up @@ -70,8 +70,9 @@ export default async function performConversion({
id,
apkg,
});
await job.completed();
} catch (error) {
await job.failed();
sendError(error);
await job.failed();
}
}
8 changes: 4 additions & 4 deletions src/routes/upload/deleteJob.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Request, Response } from 'express';
import ConversionJob from '../../lib/storage/jobs/ConversionJob';
import DB from '../../lib/storage/db';
import { sendError } from '../../lib/error/sendError';

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();
Expand Down
4 changes: 2 additions & 2 deletions src/routes/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
12 changes: 6 additions & 6 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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');
Expand Down

0 comments on commit 5b6f04a

Please sign in to comment.