|
| 1 | +import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose'; |
| 2 | +import { MutationError, ErrorCodeEnum } from '../helpers'; |
| 3 | +import { JobStatusEnum, getJobStatusEnumTC } from '../types'; |
| 4 | +import { findQueue } from '../helpers'; |
| 5 | +import { Options } from '../definitions'; |
| 6 | +import { getAsArray } from '../helpers/getAsArray'; |
| 7 | + |
| 8 | +export function createJobsRetryFC( |
| 9 | + sc: SchemaComposer<any>, |
| 10 | + opts: Options |
| 11 | +): ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> { |
| 12 | + const { typePrefix } = opts; |
| 13 | + |
| 14 | + return { |
| 15 | + type: sc.createObjectTC({ |
| 16 | + name: `${typePrefix}JobsRetryPayload`, |
| 17 | + fields: { |
| 18 | + ids: '[String]', |
| 19 | + state: getJobStatusEnumTC(sc, opts), |
| 20 | + }, |
| 21 | + }), |
| 22 | + args: { |
| 23 | + prefix: { |
| 24 | + type: 'String!', |
| 25 | + defaultValue: 'bull', |
| 26 | + }, |
| 27 | + queueName: 'String!', |
| 28 | + ids: '[String!]!', |
| 29 | + }, |
| 30 | + resolve: async (_, { prefix, queueName, ids }) => { |
| 31 | + const queue = await findQueue(prefix, queueName, opts); |
| 32 | + const _ids = getAsArray(ids); |
| 33 | + |
| 34 | + if (_ids.length > 100) { |
| 35 | + throw new MutationError( |
| 36 | + 'Arg. <id> constraint: send less than 100 IDs.', |
| 37 | + ErrorCodeEnum.OTHER_ERROR |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + const promises: Promise<void>[] = []; |
| 42 | + |
| 43 | + for (const _id of _ids) { |
| 44 | + promises.push( |
| 45 | + queue.getJob(_id).then((job) => { |
| 46 | + if (!job) |
| 47 | + throw new MutationError(`Job ${_id} not found!`, ErrorCodeEnum.JOB_NOT_FOUND, _id); |
| 48 | + return job.retry(); |
| 49 | + }) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + // Let there be a delay (await), |
| 54 | + // this will make the execution more obvious to client. |
| 55 | + await Promise.all(promises); |
| 56 | + |
| 57 | + return { |
| 58 | + ids, |
| 59 | + state: JobStatusEnum.WAITING, |
| 60 | + }; |
| 61 | + }, |
| 62 | + }; |
| 63 | +} |
0 commit comments