PeerTube/shared/extra-utils/server/jobs.ts

76 lines
1.8 KiB
TypeScript
Raw Normal View History

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