aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/utils/server/jobs.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-02-11 11:52:34 +0100
committerChocobozzz <me@florianbigard.com>2019-02-11 11:52:34 +0100
commit88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch)
treeb242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /shared/utils/server/jobs.ts
parent53a94c7cfa8368da4cd248d65df8346905938f0c (diff)
parent9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff)
downloadPeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz
PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst
PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip
Merge branch 'develop' into pr/1217
Diffstat (limited to 'shared/utils/server/jobs.ts')
-rw-r--r--shared/utils/server/jobs.ts82
1 files changed, 82 insertions, 0 deletions
diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts
new file mode 100644
index 000000000..692b5e24d
--- /dev/null
+++ b/shared/utils/server/jobs.ts
@@ -0,0 +1,82 @@
1import * as request from 'supertest'
2import { Job, JobState } from '../../models'
3import { wait } from '../miscs/miscs'
4import { ServerInfo } from './servers'
5
6function 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
17function 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
31async 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
78export {
79 getJobsList,
80 waitJobs,
81 getJobsListPaginationAndSort
82}