diff options
Diffstat (limited to 'shared/server-commands/server/jobs.ts')
-rw-r--r-- | shared/server-commands/server/jobs.ts | 118 |
1 files changed, 0 insertions, 118 deletions
diff --git a/shared/server-commands/server/jobs.ts b/shared/server-commands/server/jobs.ts deleted file mode 100644 index 8f131fba4..000000000 --- a/shared/server-commands/server/jobs.ts +++ /dev/null | |||
@@ -1,118 +0,0 @@ | |||
1 | |||
2 | import { expect } from 'chai' | ||
3 | import { wait } from '@shared/core-utils' | ||
4 | import { JobState, JobType, RunnerJobState } from '../../models' | ||
5 | import { PeerTubeServer } from './server' | ||
6 | |||
7 | async function waitJobs ( | ||
8 | serversArg: PeerTubeServer[] | PeerTubeServer, | ||
9 | options: { | ||
10 | skipDelayed?: boolean // default false | ||
11 | runnerJobs?: boolean // default false | ||
12 | } = {} | ||
13 | ) { | ||
14 | const { skipDelayed = false, runnerJobs = false } = options | ||
15 | |||
16 | const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT | ||
17 | ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) | ||
18 | : 250 | ||
19 | |||
20 | let servers: PeerTubeServer[] | ||
21 | |||
22 | if (Array.isArray(serversArg) === false) servers = [ serversArg as PeerTubeServer ] | ||
23 | else servers = serversArg as PeerTubeServer[] | ||
24 | |||
25 | const states: JobState[] = [ 'waiting', 'active' ] | ||
26 | if (!skipDelayed) states.push('delayed') | ||
27 | |||
28 | const repeatableJobs: JobType[] = [ 'videos-views-stats', 'activitypub-cleaner' ] | ||
29 | let pendingRequests: boolean | ||
30 | |||
31 | function tasksBuilder () { | ||
32 | const tasks: Promise<any>[] = [] | ||
33 | |||
34 | // Check if each server has pending request | ||
35 | for (const server of servers) { | ||
36 | if (process.env.DEBUG) console.log('Checking ' + server.url) | ||
37 | |||
38 | for (const state of states) { | ||
39 | |||
40 | const jobPromise = server.jobs.list({ | ||
41 | state, | ||
42 | start: 0, | ||
43 | count: 10, | ||
44 | sort: '-createdAt' | ||
45 | }).then(body => body.data) | ||
46 | .then(jobs => jobs.filter(j => !repeatableJobs.includes(j.type))) | ||
47 | .then(jobs => { | ||
48 | if (jobs.length !== 0) { | ||
49 | pendingRequests = true | ||
50 | |||
51 | if (process.env.DEBUG) { | ||
52 | console.log(jobs) | ||
53 | } | ||
54 | } | ||
55 | }) | ||
56 | |||
57 | tasks.push(jobPromise) | ||
58 | } | ||
59 | |||
60 | const debugPromise = server.debug.getDebug() | ||
61 | .then(obj => { | ||
62 | if (obj.activityPubMessagesWaiting !== 0) { | ||
63 | pendingRequests = true | ||
64 | |||
65 | if (process.env.DEBUG) { | ||
66 | console.log('AP messages waiting: ' + obj.activityPubMessagesWaiting) | ||
67 | } | ||
68 | } | ||
69 | }) | ||
70 | tasks.push(debugPromise) | ||
71 | |||
72 | if (runnerJobs) { | ||
73 | const runnerJobsPromise = server.runnerJobs.list({ count: 100 }) | ||
74 | .then(({ data }) => { | ||
75 | for (const job of data) { | ||
76 | if (job.state.id !== RunnerJobState.COMPLETED) { | ||
77 | pendingRequests = true | ||
78 | |||
79 | if (process.env.DEBUG) { | ||
80 | console.log(job) | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | }) | ||
85 | tasks.push(runnerJobsPromise) | ||
86 | } | ||
87 | } | ||
88 | |||
89 | return tasks | ||
90 | } | ||
91 | |||
92 | do { | ||
93 | pendingRequests = false | ||
94 | await Promise.all(tasksBuilder()) | ||
95 | |||
96 | // Retry, in case of new jobs were created | ||
97 | if (pendingRequests === false) { | ||
98 | await wait(pendingJobWait) | ||
99 | await Promise.all(tasksBuilder()) | ||
100 | } | ||
101 | |||
102 | if (pendingRequests) { | ||
103 | await wait(pendingJobWait) | ||
104 | } | ||
105 | } while (pendingRequests) | ||
106 | } | ||
107 | |||
108 | async function expectNoFailedTranscodingJob (server: PeerTubeServer) { | ||
109 | const { data } = await server.jobs.listFailed({ jobType: 'video-transcoding' }) | ||
110 | expect(data).to.have.lengthOf(0) | ||
111 | } | ||
112 | |||
113 | // --------------------------------------------------------------------------- | ||
114 | |||
115 | export { | ||
116 | waitJobs, | ||
117 | expectNoFailedTranscodingJob | ||
118 | } | ||