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