-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jobsRetry mutation, where id argument is array (#75)
- Loading branch information
Showing
6 changed files
with
80 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function getAsArray<T>(value: T | T[]): T[] { | ||
return Array.isArray(value) ? value : [value]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose'; | ||
import { MutationError, ErrorCodeEnum } from '../helpers'; | ||
import { JobStatusEnum, getJobStatusEnumTC } from '../types'; | ||
import { findQueue } from '../helpers'; | ||
import { Options } from '../definitions'; | ||
import { getAsArray } from '../helpers/getAsArray'; | ||
|
||
export function createJobsRetryFC( | ||
sc: SchemaComposer<any>, | ||
opts: Options | ||
): ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> { | ||
const { typePrefix } = opts; | ||
|
||
return { | ||
type: sc.createObjectTC({ | ||
name: `${typePrefix}JobsRetryPayload`, | ||
fields: { | ||
ids: '[String]', | ||
state: getJobStatusEnumTC(sc, opts), | ||
}, | ||
}), | ||
args: { | ||
prefix: { | ||
type: 'String!', | ||
defaultValue: 'bull', | ||
}, | ||
queueName: 'String!', | ||
ids: '[String!]!', | ||
}, | ||
resolve: async (_, { prefix, queueName, ids }) => { | ||
const queue = await findQueue(prefix, queueName, opts); | ||
const _ids = getAsArray(ids); | ||
|
||
if (_ids.length > 100) { | ||
throw new MutationError( | ||
'Arg. <id> constraint: send less than 100 IDs.', | ||
ErrorCodeEnum.OTHER_ERROR | ||
); | ||
} | ||
|
||
const promises: Promise<void>[] = []; | ||
|
||
for (const _id of _ids) { | ||
promises.push( | ||
queue.getJob(_id).then((job) => { | ||
if (!job) | ||
throw new MutationError(`Job ${_id} not found!`, ErrorCodeEnum.JOB_NOT_FOUND, _id); | ||
return job.retry(); | ||
}) | ||
); | ||
} | ||
|
||
// Let there be a delay (await), | ||
// this will make the execution more obvious to client. | ||
await Promise.all(promises); | ||
|
||
return { | ||
ids, | ||
state: JobStatusEnum.WAITING, | ||
}; | ||
}, | ||
}; | ||
} |