Management API and tools
This commit is contained in:
parent
f698249dd8
commit
9b81a6cc8b
15 changed files with 222 additions and 149 deletions
|
|
@ -1,3 +1,7 @@
|
|||
export SECRET=krRkGF2rthrT3DScCwXY4Jc_z9ldSQFw32OJt1iKFh0=
|
||||
export MONGO_URI=localhost
|
||||
export MONGO_PORT=27017
|
||||
# export SECRET=krRkGF2rthrT3DScCwXY4Jc_z9ldSQFw32OJt1iKFh0=
|
||||
# export MONGO_URI=localhost
|
||||
# export MONGO_PORT=27017
|
||||
export SECRET=9nRJVpGklniRuLRwJCZH-GPvY-7tmhBcE2kaWWND71U=
|
||||
export MONGO_URI=oitsjustjose.com
|
||||
export MONGO_PORT=27017
|
||||
export MGMT_KEY=dv4idJXGpbrfXoF*tY.Xk*uTbZFo-gkJH3X3aoePFnQpKmBKNaoY.GPxNhaC.oR7cHN_wXJzJBa_Ea_29K9-iyq8WFbx7ua3r_rm3k37FezQ*cc*YAZw64p6Nqrh!h2Ncoir_Ae*7p!7aCciBaaVaaEFiBaxz8eg6FF6UsHxTLzXda9eo2BBmWChJz9c-ZBaF9VEyUDR
|
||||
|
|
@ -3,6 +3,10 @@ import cors from "cors";
|
|||
import express from "express";
|
||||
import GetJob from "./app/controllers/job/get";
|
||||
import PutJob from "./app/controllers/job/put";
|
||||
import DequeueJob from "./app/controllers/mgmt/dequeue";
|
||||
import ListResumableJobs from "./app/controllers/mgmt/resumable";
|
||||
import UpdateJob from "./app/controllers/mgmt/update";
|
||||
import RequiresKey from "./app/middleware/requiresKey";
|
||||
|
||||
const app = express();
|
||||
|
||||
|
|
@ -10,7 +14,11 @@ app.use(json({ limit: "50mb" }));
|
|||
app.use(urlencoded({ extended: true }));
|
||||
app.use(cors());
|
||||
|
||||
app.post("/api/v1/jobs/:jobId", GetJob);
|
||||
app.get("/api/v1/jobs/:jobId", GetJob);
|
||||
app.put("/api/v1/jobs", PutJob);
|
||||
|
||||
app.get("/api/v1/mgmt/dequeue", RequiresKey(), DequeueJob);
|
||||
app.get("/api/v1/mgmt/resumable", RequiresKey(), ListResumableJobs);
|
||||
app.patch("/api/v1/mgmt/update/:jobId", RequiresKey(), UpdateJob);
|
||||
|
||||
export default app;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import Status from "../../status";
|
|||
export default async (req: Request, res: Response) => {
|
||||
try {
|
||||
if (!req.params.jobId) {
|
||||
return res.status(403).send("URL Param for Job ID is required");
|
||||
return res.status(400).send("URL Param for Job ID is required");
|
||||
}
|
||||
|
||||
const job = await Jobs.findOne({ jobId: req.params.jobId });
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { Fernet } from "fernet-nodejs";
|
|||
|
||||
const getNextQueuePlace = async () => {
|
||||
const allActiveJobs = await Job.find({ status: Status.ENQUEUED }).sort({
|
||||
queue_place: "desc",
|
||||
queuePlace: "desc",
|
||||
});
|
||||
if (allActiveJobs.length) {
|
||||
return allActiveJobs[0].queuePlace + 1;
|
||||
|
|
|
|||
16
backend/src/app/controllers/mgmt/dequeue.ts
Normal file
16
backend/src/app/controllers/mgmt/dequeue.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/* Gets the next job in the queue */
|
||||
import { Request, Response } from "express";
|
||||
import Jobs from "../../models/Job";
|
||||
import Status from "../../status";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
const [job] = await Jobs.find({ status: Status.ENQUEUED })
|
||||
.sort({ queuePlace: "asc" })
|
||||
.limit(1);
|
||||
|
||||
if (job) {
|
||||
return res.status(200).json(job);
|
||||
}
|
||||
|
||||
return res.status(404).send();
|
||||
};
|
||||
12
backend/src/app/controllers/mgmt/resumable.ts
Normal file
12
backend/src/app/controllers/mgmt/resumable.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* Gets any jobs that were Processing when the processing node restarted */
|
||||
import { Request, Response } from "express";
|
||||
import Jobs from "../../models/Job";
|
||||
import Status from "../../status";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
const jobs = await Jobs.find({ status: Status.PROCESSING }).sort({
|
||||
queuePlace: "asc",
|
||||
});
|
||||
|
||||
return res.status(200).json(jobs);
|
||||
};
|
||||
23
backend/src/app/controllers/mgmt/update.ts
Normal file
23
backend/src/app/controllers/mgmt/update.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* Updates a job with a new status or logmsg */
|
||||
|
||||
import { Request, Response } from "express";
|
||||
import Jobs from "../../models/Job";
|
||||
|
||||
export default async (req: Request, res: Response) => {
|
||||
if (!req.params.jobId) {
|
||||
return res.status(400).send("Field JobId is required in the body");
|
||||
}
|
||||
|
||||
const job = await Jobs.findOne({ jobId: req.params.jobId });
|
||||
if (!job) {
|
||||
return res
|
||||
.status(400)
|
||||
.send(`Job with ID ${req.params.jobId} was not found`);
|
||||
}
|
||||
|
||||
req.body.status && (job.status = req.body.status);
|
||||
req.body.log && (job.logs = `${req.body.log}\n${job.logs}`);
|
||||
await job.save();
|
||||
|
||||
return res.status(200).send();
|
||||
};
|
||||
10
backend/src/app/middleware/requiresKey.ts
Normal file
10
backend/src/app/middleware/requiresKey.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import { NextFunction, Request, Response } from "express";
|
||||
|
||||
export default () => {
|
||||
return (req: Request, res: Response, next: NextFunction) => {
|
||||
if (!req.query.mgmtKey || req.query.mgmtKey != process.env.MGMT_KEY) {
|
||||
return res.status(403).send(`Key is not authorized`);
|
||||
}
|
||||
return next();
|
||||
};
|
||||
};
|
||||
Loading…
Reference in a new issue