diff options
author | Chocobozzz <me@florianbigard.com> | 2019-04-15 15:26:15 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-04-24 16:25:52 +0200 |
commit | 94565d52bb2883e09f16d1363170ac9c0dccb7a1 (patch) | |
tree | 3dcd20cd7b5a5cca80bce32b655cdbfaddf7aa59 /shared/utils/server/jobs.ts | |
parent | 4ee7a4c9ac9280cda930a281c2d5a9a4c409cc14 (diff) | |
download | PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.gz PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.tar.zst PeerTube-94565d52bb2883e09f16d1363170ac9c0dccb7a1.zip |
Shared utils -> extra-utils
Because they need dev dependencies
Diffstat (limited to 'shared/utils/server/jobs.ts')
-rw-r--r-- | shared/utils/server/jobs.ts | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts deleted file mode 100644 index 692b5e24d..000000000 --- a/shared/utils/server/jobs.ts +++ /dev/null | |||
@@ -1,82 +0,0 @@ | |||
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 | } | ||