2021-07-07 09:34:56 +02:00
|
|
|
|
2021-11-23 15:22:07 +01:00
|
|
|
import { expect } from 'chai'
|
2021-12-17 11:58:15 +01:00
|
|
|
import { wait } from '@shared/core-utils'
|
2021-11-09 10:11:20 +01:00
|
|
|
import { JobState, JobType } from '../../models'
|
2021-07-16 09:47:51 +02:00
|
|
|
import { PeerTubeServer } from './server'
|
2017-11-30 10:51:13 +01:00
|
|
|
|
2022-10-25 15:01:51 +02:00
|
|
|
async function waitJobs (
|
|
|
|
serversArg: PeerTubeServer[] | PeerTubeServer,
|
|
|
|
options: {
|
|
|
|
skipDelayed?: boolean // default false
|
|
|
|
} = {}
|
|
|
|
) {
|
|
|
|
const { skipDelayed = false } = options
|
|
|
|
|
2021-02-18 14:44:12 +01:00
|
|
|
const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT
|
|
|
|
? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10)
|
2021-05-07 14:48:39 +02:00
|
|
|
: 250
|
2021-02-18 14:44:12 +01:00
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
let servers: PeerTubeServer[]
|
2018-06-13 10:06:50 +02:00
|
|
|
|
2021-07-16 09:47:51 +02:00
|
|
|
if (Array.isArray(serversArg) === false) servers = [ serversArg as PeerTubeServer ]
|
|
|
|
else servers = serversArg as PeerTubeServer[]
|
2018-06-13 10:06:50 +02:00
|
|
|
|
2021-08-17 08:26:20 +02:00
|
|
|
const states: JobState[] = [ 'waiting', 'active' ]
|
|
|
|
if (!skipDelayed) states.push('delayed')
|
|
|
|
|
2021-11-09 10:11:20 +01:00
|
|
|
const repeatableJobs: JobType[] = [ 'videos-views-stats', 'activitypub-cleaner' ]
|
2019-07-31 15:57:32 +02:00
|
|
|
let pendingRequests: boolean
|
2018-06-13 10:06:50 +02:00
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
function tasksBuilder () {
|
|
|
|
const tasks: Promise<any>[] = []
|
2018-06-13 10:06:50 +02:00
|
|
|
|
|
|
|
// Check if each server has pending request
|
|
|
|
for (const server of servers) {
|
|
|
|
for (const state of states) {
|
2021-08-25 15:13:41 +02:00
|
|
|
const p = server.jobs.list({
|
2021-07-07 09:34:56 +02:00
|
|
|
state,
|
2019-12-04 14:49:59 +01:00
|
|
|
start: 0,
|
|
|
|
count: 10,
|
|
|
|
sort: '-createdAt'
|
2021-07-07 09:34:56 +02:00
|
|
|
}).then(body => body.data)
|
|
|
|
.then(jobs => jobs.filter(j => !repeatableJobs.includes(j.type)))
|
2018-08-29 16:26:25 +02:00
|
|
|
.then(jobs => {
|
2019-07-29 11:59:29 +02:00
|
|
|
if (jobs.length !== 0) {
|
|
|
|
pendingRequests = true
|
|
|
|
}
|
2018-06-13 10:06:50 +02:00
|
|
|
})
|
2021-02-18 14:44:12 +01:00
|
|
|
|
2018-06-13 10:06:50 +02:00
|
|
|
tasks.push(p)
|
|
|
|
}
|
2021-02-18 14:44:12 +01:00
|
|
|
|
2021-07-16 09:04:35 +02:00
|
|
|
const p = server.debug.getDebug()
|
2021-07-06 16:02:11 +02:00
|
|
|
.then(obj => {
|
2021-02-18 14:44:12 +01:00
|
|
|
if (obj.activityPubMessagesWaiting !== 0) {
|
|
|
|
pendingRequests = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
tasks.push(p)
|
2018-06-13 10:06:50 +02:00
|
|
|
}
|
|
|
|
|
2018-12-26 10:36:24 +01:00
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2019-07-31 15:57:32 +02:00
|
|
|
pendingRequests = false
|
2018-12-26 10:36:24 +01:00
|
|
|
await Promise.all(tasksBuilder())
|
2018-06-13 10:06:50 +02:00
|
|
|
|
|
|
|
// Retry, in case of new jobs were created
|
|
|
|
if (pendingRequests === false) {
|
2019-01-08 11:26:41 +01:00
|
|
|
await wait(pendingJobWait)
|
2018-12-26 10:36:24 +01:00
|
|
|
await Promise.all(tasksBuilder())
|
2018-06-13 10:06:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pendingRequests) {
|
2021-05-07 14:48:39 +02:00
|
|
|
await wait(pendingJobWait)
|
2018-06-13 10:06:50 +02:00
|
|
|
}
|
|
|
|
} while (pendingRequests)
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:22:07 +01:00
|
|
|
async function expectNoFailedTranscodingJob (server: PeerTubeServer) {
|
|
|
|
const { data } = await server.jobs.listFailed({ jobType: 'video-transcoding' })
|
|
|
|
expect(data).to.have.lengthOf(0)
|
|
|
|
}
|
|
|
|
|
2017-11-30 10:51:13 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2021-11-23 15:22:07 +01:00
|
|
|
waitJobs,
|
|
|
|
expectNoFailedTranscodingJob
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|