From ad49eedebc7158d88361118ae32b42f08e454071 Mon Sep 17 00:00:00 2001 From: tdurieux Date: Thu, 23 Sep 2021 18:45:39 +0200 Subject: [PATCH] feat: remove & retry queue jobs --- public/partials/admin/queues.htm | 28 +++++++++++------ public/script/admin.js | 52 ++++++++++++++++++++------------ src/routes/admin.ts | 41 +++++++++++++++++++++++-- 3 files changed, 88 insertions(+), 33 deletions(-) diff --git a/public/partials/admin/queues.htm b/public/partials/admin/queues.htm index 3006c4b..08f1c04 100644 --- a/public/partials/admin/queues.htm +++ b/public/partials/admin/queues.htm @@ -6,9 +6,7 @@ class="col-12 d-flex px-0 py-3 border-bottom color-border-secondary" ng-repeat="job in downloadJobs as filteredDownloadJobs" > -
- {{job}} -
+
{{job}}
@@ -47,9 +51,7 @@ class="col-12 d-flex px-0 py-3 border-bottom color-border-secondary" ng-repeat="job in removeJobs as filteredRemoveJobs" > -
- {{job}} -
+
{{job}}
diff --git a/public/script/admin.js b/public/script/admin.js index 73e6a9f..b53ede6 100644 --- a/public/script/admin.js +++ b/public/script/admin.js @@ -126,7 +126,7 @@ angular page: 1, limit: 25, sort: "name", - search: "" + search: "", }; function getConferences() { @@ -154,7 +154,8 @@ angular true ); }, - ]).controller("queuesAdminController", [ + ]) + .controller("queuesAdminController", [ "$scope", "$http", "$location", @@ -170,21 +171,12 @@ angular $scope.downloadJobs = []; $scope.removeJobs = []; - $scope.total = -1; - $scope.totalPage = 0; - $scope.query = { - page: 1, - limit: 25, - sort: "name", - search: "" - }; function getQueues() { $http.get("/api/admin/queues", { params: $scope.query }).then( (res) => { $scope.downloadJobs = res.data.downloadQueue; $scope.removeJobs = res.data.removeQueue; - $scope.$apply(); }, (err) => { console.error(err); @@ -193,14 +185,34 @@ angular } getQueues(); - let timeClear = null; - $scope.$watch( - "query", - () => { - clearTimeout(timeClear); - timeClear = setTimeout(getQueues, 500); - }, - true - ); + $scope.removeJob = function (queue, job) { + $http + .delete(`/api/admin/queue/${queue}/${job.id}`, { + params: $scope.query, + }) + .then( + (res) => { + getQueues(); + }, + (err) => { + console.error(err); + } + ); + }; + + $scope.retryJob = function (queue, job) { + $http + .post(`/api/admin/queue/${queue}/${job.id}`, { + params: $scope.query, + }) + .then( + (res) => { + getQueues(); + }, + (err) => { + console.error(err); + } + ); + }; }, ]); diff --git a/src/routes/admin.ts b/src/routes/admin.ts index 4e09ad7..e1ac145 100644 --- a/src/routes/admin.ts +++ b/src/routes/admin.ts @@ -1,10 +1,9 @@ +import { Queue } from "bull"; import * as express from "express"; import AnonymizedRepositoryModel from "../database/anonymizedRepositories/anonymizedRepositories.model"; import ConferenceModel from "../database/conference/conferences.model"; -import RepositoryModel from "../database/repositories/repositories.model"; import UserModel from "../database/users/users.model"; import { downloadQueue, removeQueue } from "../queue"; -import Repository from "../Repository"; import { ensureAuthenticated } from "./connection"; import { handleError, getUser, isOwnerOrAdmin } from "./route-utils"; @@ -29,6 +28,40 @@ router.use( } ); +router.post("/queue/:name/:repo_id", async (req, res) => { + let queue: Queue; + if (req.params.name == "download") { + queue = downloadQueue; + } else if (req.params.name == "remove") { + queue = removeQueue; + } else { + return res.status(404).json({ error: "queue_not_found" }); + } + const job = await queue.getJob(req.params.repo_id); + if (!job) { + return res.status(404).json({ error: "job_not_found" }); + } + job.retry(); + res.send("ok"); +}); + +router.delete("/queue/:name/:repo_id", async (req, res) => { + let queue: Queue; + if (req.params.name == "download") { + queue = downloadQueue; + } else if (req.params.name == "remove") { + queue = removeQueue; + } else { + return res.status(404).json({ error: "queue_not_found" }); + } + const job = await queue.getJob(req.params.repo_id); + if (!job) { + return res.status(404).json({ error: "job_not_found" }); + } + await job.remove(); + res.send("ok"); +}); + router.get("/queues", async (req, res) => { const out = await Promise.all([ downloadQueue.getJobs([ @@ -94,7 +127,9 @@ router.get("/repos", async (req, res) => { res.json({ query: { $and: query }, page, - total: await AnonymizedRepositoryModel.find({ $and: query }).estimatedDocumentCount(), + total: await AnonymizedRepositoryModel.find({ + $and: query, + }).estimatedDocumentCount(), sort, results: await AnonymizedRepositoryModel.find({ $and: query }) .sort(sort)