jobs-command.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { pick } from '@shared/core-utils'
  2. import { HttpStatusCode, Job, JobState, JobType, ResultList } from '@shared/models'
  3. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  4. export class JobsCommand extends AbstractCommand {
  5. async getLatest (options: OverrideCommandOptions & {
  6. jobType: JobType
  7. }) {
  8. const { data } = await this.list({ ...options, start: 0, count: 1, sort: '-createdAt' })
  9. if (data.length === 0) return undefined
  10. return data[0]
  11. }
  12. pauseJobQueue (options: OverrideCommandOptions = {}) {
  13. const path = '/api/v1/jobs/pause'
  14. return this.postBodyRequest({
  15. ...options,
  16. path,
  17. implicitToken: true,
  18. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  19. })
  20. }
  21. resumeJobQueue (options: OverrideCommandOptions = {}) {
  22. const path = '/api/v1/jobs/resume'
  23. return this.postBodyRequest({
  24. ...options,
  25. path,
  26. implicitToken: true,
  27. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  28. })
  29. }
  30. list (options: OverrideCommandOptions & {
  31. state?: JobState
  32. jobType?: JobType
  33. start?: number
  34. count?: number
  35. sort?: string
  36. } = {}) {
  37. const path = this.buildJobsUrl(options.state)
  38. const query = pick(options, [ 'start', 'count', 'sort', 'jobType' ])
  39. return this.getRequestBody<ResultList<Job>>({
  40. ...options,
  41. path,
  42. query,
  43. implicitToken: true,
  44. defaultExpectedStatus: HttpStatusCode.OK_200
  45. })
  46. }
  47. listFailed (options: OverrideCommandOptions & {
  48. jobType?: JobType
  49. }) {
  50. const path = this.buildJobsUrl('failed')
  51. return this.getRequestBody<ResultList<Job>>({
  52. ...options,
  53. path,
  54. query: { start: 0, count: 50 },
  55. implicitToken: true,
  56. defaultExpectedStatus: HttpStatusCode.OK_200
  57. })
  58. }
  59. private buildJobsUrl (state?: JobState) {
  60. let path = '/api/v1/jobs'
  61. if (state) path += '/' + state
  62. return path
  63. }
  64. }