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