diff options
Diffstat (limited to 'shared/extra-utils/server/jobs.ts')
-rw-r--r-- | shared/extra-utils/server/jobs.ts | 82 |
1 files changed, 14 insertions, 68 deletions
diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts index 763374e03..64a0353eb 100644 --- a/shared/extra-utils/server/jobs.ts +++ b/shared/extra-utils/server/jobs.ts | |||
@@ -1,66 +1,17 @@ | |||
1 | import * as request from 'supertest' | ||
2 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
3 | import { getDebug, makeGetRequest } from '../../../shared/extra-utils' | ||
4 | import { Job, JobState, JobType, ServerDebug } from '../../models' | ||
5 | import { wait } from '../miscs/miscs' | ||
6 | import { ServerInfo } from './servers' | ||
7 | 1 | ||
8 | function buildJobsUrl (state?: JobState) { | 2 | import { JobState } from '../../models' |
9 | let path = '/api/v1/jobs' | 3 | import { wait } from '../miscs' |
4 | import { PeerTubeServer } from './server' | ||
10 | 5 | ||
11 | if (state) path += '/' + state | 6 | async function waitJobs (serversArg: PeerTubeServer[] | PeerTubeServer) { |
12 | |||
13 | return path | ||
14 | } | ||
15 | |||
16 | function getJobsList (url: string, accessToken: string, state?: JobState) { | ||
17 | const path = buildJobsUrl(state) | ||
18 | |||
19 | return request(url) | ||
20 | .get(path) | ||
21 | .set('Accept', 'application/json') | ||
22 | .set('Authorization', 'Bearer ' + accessToken) | ||
23 | .expect(HttpStatusCode.OK_200) | ||
24 | .expect('Content-Type', /json/) | ||
25 | } | ||
26 | |||
27 | function getJobsListPaginationAndSort (options: { | ||
28 | url: string | ||
29 | accessToken: string | ||
30 | start: number | ||
31 | count: number | ||
32 | sort: string | ||
33 | state?: JobState | ||
34 | jobType?: JobType | ||
35 | }) { | ||
36 | const { url, accessToken, state, start, count, sort, jobType } = options | ||
37 | const path = buildJobsUrl(state) | ||
38 | |||
39 | const query = { | ||
40 | start, | ||
41 | count, | ||
42 | sort, | ||
43 | jobType | ||
44 | } | ||
45 | |||
46 | return makeGetRequest({ | ||
47 | url, | ||
48 | path, | ||
49 | token: accessToken, | ||
50 | statusCodeExpected: HttpStatusCode.OK_200, | ||
51 | query | ||
52 | }) | ||
53 | } | ||
54 | |||
55 | async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | ||
56 | const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT | 7 | const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT |
57 | ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) | 8 | ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) |
58 | : 250 | 9 | : 250 |
59 | 10 | ||
60 | let servers: ServerInfo[] | 11 | let servers: PeerTubeServer[] |
61 | 12 | ||
62 | if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] | 13 | if (Array.isArray(serversArg) === false) servers = [ serversArg as PeerTubeServer ] |
63 | else servers = serversArg as ServerInfo[] | 14 | else servers = serversArg as PeerTubeServer[] |
64 | 15 | ||
65 | const states: JobState[] = [ 'waiting', 'active', 'delayed' ] | 16 | const states: JobState[] = [ 'waiting', 'active', 'delayed' ] |
66 | const repeatableJobs = [ 'videos-views', 'activitypub-cleaner' ] | 17 | const repeatableJobs = [ 'videos-views', 'activitypub-cleaner' ] |
@@ -72,15 +23,13 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | |||
72 | // Check if each server has pending request | 23 | // Check if each server has pending request |
73 | for (const server of servers) { | 24 | for (const server of servers) { |
74 | for (const state of states) { | 25 | for (const state of states) { |
75 | const p = getJobsListPaginationAndSort({ | 26 | const p = server.jobs.getJobsList({ |
76 | url: server.url, | 27 | state, |
77 | accessToken: server.accessToken, | ||
78 | state: state, | ||
79 | start: 0, | 28 | start: 0, |
80 | count: 10, | 29 | count: 10, |
81 | sort: '-createdAt' | 30 | sort: '-createdAt' |
82 | }).then(res => res.body.data) | 31 | }).then(body => body.data) |
83 | .then((jobs: Job[]) => jobs.filter(j => !repeatableJobs.includes(j.type))) | 32 | .then(jobs => jobs.filter(j => !repeatableJobs.includes(j.type))) |
84 | .then(jobs => { | 33 | .then(jobs => { |
85 | if (jobs.length !== 0) { | 34 | if (jobs.length !== 0) { |
86 | pendingRequests = true | 35 | pendingRequests = true |
@@ -90,9 +39,8 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | |||
90 | tasks.push(p) | 39 | tasks.push(p) |
91 | } | 40 | } |
92 | 41 | ||
93 | const p = getDebug(server.url, server.accessToken) | 42 | const p = server.debug.getDebug() |
94 | .then(res => res.body) | 43 | .then(obj => { |
95 | .then((obj: ServerDebug) => { | ||
96 | if (obj.activityPubMessagesWaiting !== 0) { | 44 | if (obj.activityPubMessagesWaiting !== 0) { |
97 | pendingRequests = true | 45 | pendingRequests = true |
98 | } | 46 | } |
@@ -123,7 +71,5 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | |||
123 | // --------------------------------------------------------------------------- | 71 | // --------------------------------------------------------------------------- |
124 | 72 | ||
125 | export { | 73 | export { |
126 | getJobsList, | 74 | waitJobs |
127 | waitJobs, | ||
128 | getJobsListPaginationAndSort | ||
129 | } | 75 | } |