aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-07 09:34:56 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:17 +0200
commit9c6327f803aaf4200672f1fc40b2f43786daca47 (patch)
tree962b975a5c3515804ed5d269019ce0aa3f5da6b3 /shared/extra-utils/server
parentc3d29f694bf8c910f917be655626d0f80871124f (diff)
downloadPeerTube-9c6327f803aaf4200672f1fc40b2f43786daca47.tar.gz
PeerTube-9c6327f803aaf4200672f1fc40b2f43786daca47.tar.zst
PeerTube-9c6327f803aaf4200672f1fc40b2f43786daca47.zip
Introduce jobs command
Diffstat (limited to 'shared/extra-utils/server')
-rw-r--r--shared/extra-utils/server/index.ts2
-rw-r--r--shared/extra-utils/server/jobs-command.ts35
-rw-r--r--shared/extra-utils/server/jobs.ts67
-rw-r--r--shared/extra-utils/server/servers.ts3
4 files changed, 47 insertions, 60 deletions
diff --git a/shared/extra-utils/server/index.ts b/shared/extra-utils/server/index.ts
index 258084623..b5b6b2116 100644
--- a/shared/extra-utils/server/index.ts
+++ b/shared/extra-utils/server/index.ts
@@ -2,3 +2,5 @@ export * from './contact-form-command'
2export * from './debug-command' 2export * from './debug-command'
3export * from './follows-command' 3export * from './follows-command'
4export * from './follows' 4export * from './follows'
5export * from './jobs'
6export * from './jobs-command'
diff --git a/shared/extra-utils/server/jobs-command.ts b/shared/extra-utils/server/jobs-command.ts
new file mode 100644
index 000000000..758b4a4af
--- /dev/null
+++ b/shared/extra-utils/server/jobs-command.ts
@@ -0,0 +1,35 @@
1import { pick } from 'lodash'
2import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
3import { Job, JobState, JobType, ResultList } from '../../models'
4import { AbstractCommand, OverrideCommandOptions } from '../shared'
5
6export class JobsCommand extends AbstractCommand {
7
8 getJobsList (options: OverrideCommandOptions & {
9 state?: JobState
10 jobType?: JobType
11 start?: number
12 count?: number
13 sort?: string
14 } = {}) {
15 const path = this.buildJobsUrl(options.state)
16
17 const query = pick(options, [ 'start', 'count', 'sort', 'jobType' ])
18
19 return this.getRequestBody<ResultList<Job>>({
20 ...options,
21
22 path,
23 query,
24 defaultExpectedStatus: HttpStatusCode.OK_200
25 })
26 }
27
28 private buildJobsUrl (state?: JobState) {
29 let path = '/api/v1/jobs'
30
31 if (state) path += '/' + state
32
33 return path
34 }
35}
diff --git a/shared/extra-utils/server/jobs.ts b/shared/extra-utils/server/jobs.ts
index a3683913a..b4b3d52e7 100644
--- a/shared/extra-utils/server/jobs.ts
+++ b/shared/extra-utils/server/jobs.ts
@@ -1,57 +1,8 @@
1import * as request from 'supertest' 1
2import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' 2import { JobState } from '../../models'
3import { makeGetRequest } from '../../../shared/extra-utils'
4import { Job, JobState, JobType } from '../../models'
5import { wait } from '../miscs/miscs' 3import { wait } from '../miscs/miscs'
6import { ServerInfo } from './servers' 4import { ServerInfo } from './servers'
7 5
8function buildJobsUrl (state?: JobState) {
9 let path = '/api/v1/jobs'
10
11 if (state) path += '/' + state
12
13 return path
14}
15
16function getJobsList (url: string, accessToken: string, state?: JobState) {
17 const path = buildJobsUrl(state)
18
19 return request(url)
20 .get(path)
21 .set('Accept', 'application/json')
22 .set('Authorization', 'Bearer ' + accessToken)
23 .expect(HttpStatusCode.OK_200)
24 .expect('Content-Type', /json/)
25}
26
27function getJobsListPaginationAndSort (options: {
28 url: string
29 accessToken: string
30 start: number
31 count: number
32 sort: string
33 state?: JobState
34 jobType?: JobType
35}) {
36 const { url, accessToken, state, start, count, sort, jobType } = options
37 const path = buildJobsUrl(state)
38
39 const query = {
40 start,
41 count,
42 sort,
43 jobType
44 }
45
46 return makeGetRequest({
47 url,
48 path,
49 token: accessToken,
50 statusCodeExpected: HttpStatusCode.OK_200,
51 query
52 })
53}
54
55async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { 6async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
56 const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT 7 const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT
57 ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10) 8 ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10)
@@ -72,15 +23,13 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
72 // Check if each server has pending request 23 // Check if each server has pending request
73 for (const server of servers) { 24 for (const server of servers) {
74 for (const state of states) { 25 for (const state of states) {
75 const p = getJobsListPaginationAndSort({ 26 const p = server.jobsCommand.getJobsList({
76 url: server.url, 27 state,
77 accessToken: server.accessToken,
78 state: state,
79 start: 0, 28 start: 0,
80 count: 10, 29 count: 10,
81 sort: '-createdAt' 30 sort: '-createdAt'
82 }).then(res => res.body.data) 31 }).then(body => body.data)
83 .then((jobs: Job[]) => jobs.filter(j => !repeatableJobs.includes(j.type))) 32 .then(jobs => jobs.filter(j => !repeatableJobs.includes(j.type)))
84 .then(jobs => { 33 .then(jobs => {
85 if (jobs.length !== 0) { 34 if (jobs.length !== 0) {
86 pendingRequests = true 35 pendingRequests = true
@@ -122,7 +71,5 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) {
122// --------------------------------------------------------------------------- 71// ---------------------------------------------------------------------------
123 72
124export { 73export {
125 getJobsList, 74 waitJobs
126 waitJobs,
127 getJobsListPaginationAndSort
128} 75}
diff --git a/shared/extra-utils/server/servers.ts b/shared/extra-utils/server/servers.ts
index 7ac80cea0..5511ce0b0 100644
--- a/shared/extra-utils/server/servers.ts
+++ b/shared/extra-utils/server/servers.ts
@@ -19,6 +19,7 @@ import { SearchCommand } from '../search'
19import { ContactFormCommand } from './contact-form-command' 19import { ContactFormCommand } from './contact-form-command'
20import { DebugCommand } from './debug-command' 20import { DebugCommand } from './debug-command'
21import { FollowsCommand } from './follows-command' 21import { FollowsCommand } from './follows-command'
22import { JobsCommand } from './jobs-command'
22 23
23interface ServerInfo { 24interface ServerInfo {
24 app: ChildProcess 25 app: ChildProcess
@@ -83,6 +84,7 @@ interface ServerInfo {
83 contactFormCommand?: ContactFormCommand 84 contactFormCommand?: ContactFormCommand
84 debugCommand?: DebugCommand 85 debugCommand?: DebugCommand
85 followsCommand?: FollowsCommand 86 followsCommand?: FollowsCommand
87 jobsCommand?: JobsCommand
86} 88}
87 89
88function parallelTests () { 90function parallelTests () {
@@ -299,6 +301,7 @@ async function runServer (server: ServerInfo, configOverrideArg?: any, args = []
299 server.contactFormCommand = new ContactFormCommand(server) 301 server.contactFormCommand = new ContactFormCommand(server)
300 server.debugCommand = new DebugCommand(server) 302 server.debugCommand = new DebugCommand(server)
301 server.followsCommand = new FollowsCommand(server) 303 server.followsCommand = new FollowsCommand(server)
304 server.jobsCommand = new JobsCommand(server)
302 305
303 res(server) 306 res(server)
304 }) 307 })