From 9639bd175726b73f8fe664b5ced12a72407b1f0b Mon Sep 17 00:00:00 2001 From: buoyantair Date: Mon, 29 Oct 2018 22:18:31 +0530 Subject: Move utils to /shared Move utils used by /server/tools/* & /server/tests/**/* into /shared folder. Issue: #1336 --- shared/utils/server/jobs.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 shared/utils/server/jobs.ts (limited to 'shared/utils/server/jobs.ts') diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts new file mode 100644 index 000000000..4c02cace5 --- /dev/null +++ b/shared/utils/server/jobs.ts @@ -0,0 +1,77 @@ +import * as request from 'supertest' +import { Job, JobState } from '../../../../shared/models' +import { ServerInfo, wait } from '../index' + +function getJobsList (url: string, accessToken: string, state: JobState) { + const path = '/api/v1/jobs/' + state + + 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 + + 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) { + let servers: ServerInfo[] + + if (Array.isArray(serversArg) === false) servers = [ serversArg as ServerInfo ] + else servers = serversArg as ServerInfo[] + + const states: JobState[] = [ 'waiting', 'active', 'delayed' ] + const tasks: Promise[] = [] + let pendingRequests: boolean + + do { + 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') + .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) + } + } + + await Promise.all(tasks) + + // Retry, in case of new jobs were created + if (pendingRequests === false) { + await wait(1000) + + await Promise.all(tasks) + } + + if (pendingRequests) { + await wait(1000) + } + } while (pendingRequests) +} + +// --------------------------------------------------------------------------- + +export { + getJobsList, + waitJobs, + getJobsListPaginationAndSort +} -- cgit v1.2.3 From d4681c0074ba51c62a3aeb9fb3f2cd071dd21e32 Mon Sep 17 00:00:00 2001 From: buoyantair Date: Mon, 29 Oct 2018 22:36:09 +0530 Subject: Fix dependency issues --- shared/utils/server/jobs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shared/utils/server/jobs.ts') diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts index 4c02cace5..3eec1d7d7 100644 --- a/shared/utils/server/jobs.ts +++ b/shared/utils/server/jobs.ts @@ -1,5 +1,5 @@ import * as request from 'supertest' -import { Job, JobState } from '../../../../shared/models' +import { Job, JobState } from '../../models' import { ServerInfo, wait } from '../index' function getJobsList (url: string, accessToken: string, state: JobState) { -- cgit v1.2.3 From 1de1d05f4c61fe059fa5e24e79c92582f0e7e4b3 Mon Sep 17 00:00:00 2001 From: Josh Morel Date: Sun, 6 Jan 2019 13:15:06 -0500 Subject: increase waitJobs pendingJobs timeout to 2000 --- shared/utils/server/jobs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'shared/utils/server/jobs.ts') diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts index 7c7e89824..f4623f896 100644 --- a/shared/utils/server/jobs.ts +++ b/shared/utils/server/jobs.ts @@ -58,7 +58,7 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { // Retry, in case of new jobs were created if (pendingRequests === false) { - await wait(1000) + await wait(2000) await Promise.all(tasks) } -- cgit v1.2.3 From cef534ed53e4518fe0acf581bfe880788d42fc36 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 26 Dec 2018 10:36:24 +0100 Subject: Add user notification base code --- shared/utils/server/jobs.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'shared/utils/server/jobs.ts') diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts index f4623f896..6218c0b66 100644 --- a/shared/utils/server/jobs.ts +++ b/shared/utils/server/jobs.ts @@ -35,10 +35,10 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { else servers = serversArg as ServerInfo[] const states: JobState[] = [ 'waiting', 'active', 'delayed' ] - const tasks: Promise[] = [] - let pendingRequests: boolean + let pendingRequests = false - do { + function tasksBuilder () { + const tasks: Promise[] = [] pendingRequests = false // Check if each server has pending request @@ -54,13 +54,16 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { } } - await Promise.all(tasks) + return tasks + } + + do { + await Promise.all(tasksBuilder()) // Retry, in case of new jobs were created if (pendingRequests === false) { await wait(2000) - - await Promise.all(tasks) + await Promise.all(tasksBuilder()) } if (pendingRequests) { -- cgit v1.2.3 From 2f1548fda32c3ba9e53913270394eedfacd55986 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 8 Jan 2019 11:26:41 +0100 Subject: Add notifications in the client --- shared/utils/server/jobs.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'shared/utils/server/jobs.ts') diff --git a/shared/utils/server/jobs.ts b/shared/utils/server/jobs.ts index 6218c0b66..692b5e24d 100644 --- a/shared/utils/server/jobs.ts +++ b/shared/utils/server/jobs.ts @@ -29,6 +29,7 @@ function getJobsListPaginationAndSort (url: string, accessToken: string, state: } async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { + 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 ] @@ -62,7 +63,7 @@ async function waitJobs (serversArg: ServerInfo[] | ServerInfo) { // Retry, in case of new jobs were created if (pendingRequests === false) { - await wait(2000) + await wait(pendingJobWait) await Promise.all(tasksBuilder()) } -- cgit v1.2.3