jobs.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { expect } from 'chai'
  2. import { wait } from '@shared/core-utils'
  3. import { JobState, JobType } from '../../models'
  4. import { PeerTubeServer } from './server'
  5. async function waitJobs (serversArg: PeerTubeServer[] | PeerTubeServer, skipDelayed = false) {
  6. const pendingJobWait = process.env.NODE_PENDING_JOB_WAIT
  7. ? parseInt(process.env.NODE_PENDING_JOB_WAIT, 10)
  8. : 250
  9. let servers: PeerTubeServer[]
  10. if (Array.isArray(serversArg) === false) servers = [ serversArg as PeerTubeServer ]
  11. else servers = serversArg as PeerTubeServer[]
  12. const states: JobState[] = [ 'waiting', 'active' ]
  13. if (!skipDelayed) states.push('delayed')
  14. const repeatableJobs: JobType[] = [ 'videos-views-stats', 'activitypub-cleaner' ]
  15. let pendingRequests: boolean
  16. function tasksBuilder () {
  17. const tasks: Promise<any>[] = []
  18. // Check if each server has pending request
  19. for (const server of servers) {
  20. for (const state of states) {
  21. const p = server.jobs.list({
  22. state,
  23. start: 0,
  24. count: 10,
  25. sort: '-createdAt'
  26. }).then(body => body.data)
  27. .then(jobs => jobs.filter(j => !repeatableJobs.includes(j.type)))
  28. .then(jobs => {
  29. if (jobs.length !== 0) {
  30. pendingRequests = true
  31. }
  32. })
  33. tasks.push(p)
  34. }
  35. const p = server.debug.getDebug()
  36. .then(obj => {
  37. if (obj.activityPubMessagesWaiting !== 0) {
  38. pendingRequests = true
  39. }
  40. })
  41. tasks.push(p)
  42. }
  43. return tasks
  44. }
  45. do {
  46. pendingRequests = false
  47. await Promise.all(tasksBuilder())
  48. // Retry, in case of new jobs were created
  49. if (pendingRequests === false) {
  50. await wait(pendingJobWait)
  51. await Promise.all(tasksBuilder())
  52. }
  53. if (pendingRequests) {
  54. await wait(pendingJobWait)
  55. }
  56. } while (pendingRequests)
  57. }
  58. async function expectNoFailedTranscodingJob (server: PeerTubeServer) {
  59. const { data } = await server.jobs.listFailed({ jobType: 'video-transcoding' })
  60. expect(data).to.have.lengthOf(0)
  61. }
  62. // ---------------------------------------------------------------------------
  63. export {
  64. waitJobs,
  65. expectNoFailedTranscodingJob
  66. }