diff options
Diffstat (limited to 'shared/extra-utils/server/jobs.ts')
-rw-r--r-- | shared/extra-utils/server/jobs.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts new file mode 100644 index 000000000..692b5e24d --- /dev/null +++ b/shared/extra-utils/server/jobs.ts | |||
@@ -0,0 +1,82 @@ | |||
1 | import * as request from 'supertest' | ||
2 | import { Job, JobState } from '../../models' | ||
3 | import { wait } from '../miscs/miscs' | ||
4 | import { ServerInfo } from './servers' | ||
5 | |||
6 | function getJobsList (url: string, accessToken: string, state: JobState) { | ||
7 | const path = '/api/v1/jobs/' + state | ||
8 | |||
9 | return request(url) | ||
10 | .get(path) | ||
11 | .set('Accept', 'application/json') | ||
12 | .set('Authorization', 'Bearer ' + accessToken) | ||
13 | .expect(200) | ||
14 | .expect('Content-Type', /json/) | ||
15 | } | ||
16 | |||
17 | function getJobsListPaginationAndSort (url: string, accessToken: string, state: JobState, start: number, count: number, sort: string) { | ||
18 | const path = '/api/v1/jobs/' + state | ||
19 | |||
20 | return request(url) | ||
21 | .get(path) | ||
22 | .query({ start }) | ||
23 | .query({ count }) | ||
24 | .query({ sort }) | ||
25 | .set('Accept', 'application/json') | ||
26 | .set('Authorization', 'Bearer ' + accessToken) | ||
27 | .expect(200) | ||
28 | .expect('Content-Type', /json/) | ||
29 | } | ||
30 | |||
31 | async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { | ||
32 | const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) : 2000 | ||
33 | let servers: ServerInfo[] | ||
34 | |||
35 | if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] | ||
36 | else servers = serversArg as ServerInfo[] | ||
37 | |||
38 | const states: JobState[] = [ 'waiting', 'active', 'delayed' ] | ||
39 | let pendingRequests = false | ||
40 | |||
41 | function tasksBuilder () { | ||
42 | const tasks: Promise<any>[] = [] | ||
43 | pendingRequests = false | ||
44 | |||
45 | // Check if each server has pending request | ||
46 | for (const server of servers) { | ||
47 | for (const state of states) { | ||
48 | const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt') | ||
49 | .then(res => res.body.data) | ||
50 | .then((jobs: Job[]) => jobs.filter(j => j.type !== 'videos-views')) | ||
51 | .then(jobs => { | ||
52 | if (jobs.length !== 0) pendingRequests = true | ||
53 | }) | ||
54 | tasks.push(p) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | return tasks | ||
59 | } | ||
60 | |||
61 | do { | ||
62 | await Promise.all(tasksBuilder()) | ||
63 | |||
64 | // Retry, in case of new jobs were created | ||
65 | if (pendingRequests === false) { | ||
66 | await wait(pendingJobWait) | ||
67 | await Promise.all(tasksBuilder()) | ||
68 | } | ||
69 | |||
70 | if (pendingRequests) { | ||
71 | await wait(1000) | ||
72 | } | ||
73 | } while (pendingRequests) | ||
74 | } | ||
75 | |||
76 | // --------------------------------------------------------------------------- | ||
77 | |||
78 | export { | ||
79 | getJobsList, | ||
80 | waitJobs, | ||
81 | getJobsListPaginationAndSort | ||
82 | } | ||