2017-11-30 10:51:13 +01:00
|
|
|
import * as request from 'supertest'
|
2020-12-14 12:00:35 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2021-02-18 14:44:12 +01:00
|
|
|
import { getDebug, makeGetRequest } from '../../../shared/extra-utils'
|
|
|
|
import { Job, JobState, JobType, ServerDebug } from '../../models'
|
2018-11-19 17:08:18 +01:00
|
|
|
import { wait } from '../miscs/miscs'
|
2018-11-20 07:58:13 +01:00
|
|
|
import { ServerInfo } from './servers'
|
2017-11-30 10:51:13 +01:00
|
|
|
|
2020-12-14 12:00:35 +01:00
|
|
|
function buildJobsUrl (state?: JobState) {
|
|
|
|
let path = '/api/v1/jobs'
|
|
|
|
|
|
|
|
if (state) path += '/' + state
|
|
|
|
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
function getJobsList (url: string, accessToken: string, state?: JobState) {
|
|
|
|
const path = buildJobsUrl(state)
|
2017-11-30 10:51:13 +01:00
|
|
|
|
|
|
|
return request(url)
|
2020-01-31 16:56:52 +01:00
|
|
|
.get(path)
|
|
|
|
.set('Accept', 'application/json')
|
|
|
|
.set('Authorization', 'Bearer ' + accessToken)
|
2020-12-07 14:32:36 +01:00
|
|
|
.expect(HttpStatusCode.OK_200)
|
2020-01-31 16:56:52 +01:00
|
|
|
.expect('Content-Type', /json/)
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|
|
|
|
|
2019-12-04 14:49:59 +01:00
|
|
|
function getJobsListPaginationAndSort (options: {
|
2020-01-31 16:56:52 +01:00
|
|
|
url: string
|
|
|
|
accessToken: string
|
|
|
|
start: number
|
|
|
|
count: number
|
|
|
|
sort: string
|
2020-12-14 12:00:35 +01:00
|
|
|
state?: JobState
|
2019-12-04 14:49:59 +01:00
|
|
|
jobType?: JobType
|
|
|
|
}) {
|
|
|
|
const { url, accessToken, state, start, count, sort, jobType } = options
|
2020-12-14 12:00:35 +01:00
|
|
|
const path = buildJobsUrl(state)
|
2017-11-30 10:51:13 +01:00
|
|
|
|
2019-12-04 14:49:59 +01:00
|
|
|
const query = {
|
|
|
|
start,
|
|
|
|
count,
|
|
|
|
sort,
|
|
|
|
jobType
|
|
|
|
}
|
|
|
|
|
|
|
|
return makeGetRequest({
|
|
|
|
url,
|
|
|
|
path,
|
|
|
|
token: accessToken,
|
2020-12-07 14:32:36 +01:00
|
|
|
statusCodeExpected: HttpStatusCode.OK_200,
|
2019-12-04 14:49:59 +01:00
|
|
|
query
|
|
|
|
})
|
2017-11-30 10:51:13 +01:00
|
|
|
}
|
|
|
|
|
2018-06-13 10:06:50 +02:00
|
|
|
async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
|
2021-02-18 14:44:12 +01:00
|
|
|
const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT
|
|
|
|
? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10)
|
|
|
|
: 500
|
|
|
|
|
2018-06-13 10:06:50 +02:00
|
|
|
let servers: ServerInfo[]
|
|
|
|
|
|
|
|
if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ]
|
|
|
|
else servers = serversArg as ServerInfo[]
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
const states: JobState[] = [ 'waiting', 'active', 'delayed' ]
|
2021-02-26 16:26:27 +01:00
|
|
|
const repeatableJobs = [ 'videos-views', '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) {
|
2019-12-04 14:49:59 +01:00
|
|
|
const p = getJobsListPaginationAndSort({
|
|
|
|
url: server.url,
|
|
|
|
accessToken: server.accessToken,
|
|
|
|
state: state,
|
|
|
|
start: 0,
|
|
|
|
count: 10,
|
|
|
|
sort: '-createdAt'
|
2021-02-18 14:44:12 +01:00
|
|
|
}).then(res => res.body.data)
|
2021-02-26 16:26:27 +01:00
|
|
|
.then((jobs: Job[]) => 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
|
|
|
|
|
|
|
const p = getDebug(server.url, server.accessToken)
|
|
|
|
.then(res => res.body)
|
|
|
|
.then((obj: ServerDebug) => {
|
|
|
|
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) {
|
|
|
|
await wait(1000)
|
|
|
|
}
|
|
|
|
} while (pendingRequests)
|
|
|
|
}
|
|
|
|
|
2017-11-30 10:51:13 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
getJobsList,
|
2018-06-13 10:06:50 +02:00
|
|
|
waitJobs,
|
2017-11-30 10:51:13 +01:00
|
|
|
getJobsListPaginationAndSort
|
|
|
|
}
|