From 2c29ad4f3b736912ae769a288c4795dce40e388d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 27 Feb 2018 16:57:53 +0100 Subject: [PATCH] Fix job panel sorting in administration --- server/controllers/api/jobs.ts | 2 +- server/lib/job-queue/job-queue.ts | 30 ++++++++++++++++++++++-------- server/lib/redis.ts | 12 ++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/server/controllers/api/jobs.ts b/server/controllers/api/jobs.ts index 132d110ad..aa58a9144 100644 --- a/server/controllers/api/jobs.ts +++ b/server/controllers/api/jobs.ts @@ -36,7 +36,7 @@ export { // --------------------------------------------------------------------------- async function listJobs (req: express.Request, res: express.Response, next: express.NextFunction) { - const sort = req.query.sort === 'createdAt' ? 'asc' : 'desc' + const sort = req.query.sort === 'createdAt' ? 'ASC' : 'DESC' const jobs = await JobQueue.Instance.listForApi(req.params.state, req.query.start, req.query.count, sort) const total = await JobQueue.Instance.count(req.params.state) diff --git a/server/lib/job-queue/job-queue.ts b/server/lib/job-queue/job-queue.ts index fb3cc8f66..b0ccbd59c 100644 --- a/server/lib/job-queue/job-queue.ts +++ b/server/lib/job-queue/job-queue.ts @@ -2,6 +2,7 @@ import * as kue from 'kue' import { JobType, JobState } from '../../../shared/models' import { logger } from '../../helpers/logger' import { CONFIG, JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY } from '../../initializers' +import { Redis } from '../redis' import { ActivitypubHttpBroadcastPayload, processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast' import { ActivitypubHttpFetcherPayload, processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher' import { ActivitypubHttpUnicastPayload, processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast' @@ -29,6 +30,7 @@ class JobQueue { private jobQueue: kue.Queue private initialized = false + private jobRedisPrefix: string private constructor () {} @@ -37,8 +39,10 @@ class JobQueue { if (this.initialized === true) return this.initialized = true + this.jobRedisPrefix = 'q-' + CONFIG.WEBSERVER.HOST + this.jobQueue = kue.createQueue({ - prefix: 'q-' + CONFIG.WEBSERVER.HOST, + prefix: this.jobRedisPrefix, redis: { host: CONFIG.REDIS.HOSTNAME, port: CONFIG.REDIS.PORT, @@ -83,14 +87,14 @@ class JobQueue { }) } - listForApi (state: JobState, start: number, count: number, sort: string) { - return new Promise((res, rej) => { - kue.Job.rangeByState(state, start, start + count - 1, sort, (err, jobs) => { - if (err) return rej(err) + async listForApi (state: JobState, start: number, count: number, sort: 'ASC' | 'DESC') { + const jobStrings = await Redis.Instance.listJobs(this.jobRedisPrefix, state, 'alpha', sort, start, count) - return res(jobs) - }) - }) + const jobPromises = jobStrings + .map(s => s.split('|')) + .map(([ , jobId ]) => this.getJob(parseInt(jobId, 10))) + + return Promise.all(jobPromises) } count (state: JobState) { @@ -144,6 +148,16 @@ class JobQueue { return Promise.all(promises) } + private getJob (id: number) { + return new Promise((res, rej) => { + kue.Job.get(id, (err, job) => { + if (err) return rej(err) + + return res(job) + }) + }) + } + static get Instance () { return this.instance || (this.instance = new this()) } diff --git a/server/lib/redis.ts b/server/lib/redis.ts index b284cab8f..2ecff939e 100644 --- a/server/lib/redis.ts +++ b/server/lib/redis.ts @@ -54,6 +54,18 @@ class Redis { return this.exists(this.buildViewKey(ip, videoUUID)) } + listJobs (jobsPrefix: string, state: string, mode: 'alpha', order: 'ASC' | 'DESC', offset: number, count: number) { + return new Promise((res, rej) => { + this.client.sort(jobsPrefix + ':jobs:' + state, 'by', mode, order, 'LIMIT', offset.toString(), count.toString(), (err, values) => { + if (err) return rej(err) + + + + return res(values) + }) + }) + } + private getValue (key: string) { return new Promise((res, rej) => { this.client.get(this.prefix + key, (err, value) => { -- 2.41.0