1
0
Fork 0
peertube/shared/extra-utils/server/jobs.ts

83 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-11-30 04:51:13 -05:00
import * as request from 'supertest'
2018-10-29 13:06:09 -04:00
import { Job, JobState } from '../../models'
2018-11-19 11:08:18 -05:00
import { wait } from '../miscs/miscs'
import { ServerInfo } from './servers'
2017-11-30 04:51:13 -05:00
function getJobsList (url: string, accessToken: string, state: JobState) {
const path = '/api/v1/jobs/' + state
2017-11-30 04:51:13 -05:00
return request(url)
.get(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
.expect(200)
.expect('Content-Type', /json/)
}
function getJobsListPaginationAndSort (url: string, accessToken: string, state: JobState, start: number, count: number, sort: string) {
const path = '/api/v1/jobs/' + state
2017-11-30 04:51:13 -05:00
return request(url)
.get(path)
.query({ start })
.query({ count })
.query({ sort })
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
.expect(200)
.expect('Content-Type', /json/)
}
async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
2019-01-08 05:26:41 -05:00
const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) : 2000
let servers: ServerInfo[]
if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ]
else servers = serversArg as ServerInfo[]
2018-07-10 11:02:20 -04:00
const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
2018-12-26 04:36:24 -05:00
let pendingRequests = false
2018-12-26 04:36:24 -05:00
function tasksBuilder () {
const tasks: Promise<any>[] = []
pendingRequests = false
// Check if each server has pending request
for (const server of servers) {
for (const state of states) {
const p = getJobsListPaginationAndSort(server.url, server.accessToken, state, 0, 10, '-createdAt')
2018-08-29 10:26:25 -04:00
.then(res => res.body.data)
.then((jobs: Job[]) => jobs.filter(j => j.type !== 'videos-views'))
.then(jobs => {
if (jobs.length !== 0) pendingRequests = true
})
tasks.push(p)
}
}
2018-12-26 04:36:24 -05:00
return tasks
}
do {
await Promise.all(tasksBuilder())
// 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())
}
if (pendingRequests) {
await wait(1000)
}
} while (pendingRequests)
}
2017-11-30 04:51:13 -05:00
// ---------------------------------------------------------------------------
export {
getJobsList,
waitJobs,
2017-11-30 04:51:13 -05:00
getJobsListPaginationAndSort
}