-
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: pep up queue, helps to alive broken cron tasks via
...:delay
…
…steam * fix: job name not necessarily * feat: add pepUp queue mutation
- Loading branch information
Showing
5 changed files
with
100 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { MutationError, ErrorCodeEnum } from './MutationError'; | ||
import { getBullConnection } from './getBullConnection'; | ||
import { Options } from '../definitions'; | ||
|
||
type Record = { | ||
id: string; | ||
nextTimestamp: string; | ||
}; | ||
|
||
type Result = { | ||
first: Record; | ||
last: Record; | ||
fixLast: Record; | ||
}; | ||
|
||
export async function addFixRecordToDelayStream( | ||
prefix: string, | ||
queueName: string, | ||
opts: Options, | ||
checkExistence: boolean = true | ||
): Promise<Result> { | ||
const redis = getBullConnection(opts); | ||
|
||
const fullQueueName = [prefix, queueName].join(':'); | ||
|
||
if (checkExistence) { | ||
const queueExists = await redis.exists([fullQueueName, 'meta'].join(':')); | ||
|
||
if (!queueExists) { | ||
throw new MutationError('Queue not found!', ErrorCodeEnum.QUEUE_NOT_FOUND); | ||
} | ||
} | ||
|
||
const streamName = fullQueueName + ':delay'; | ||
|
||
const first = await redis.xrange(streamName, '-', '+', 'count', 1); | ||
const last = await redis.xrevrange(streamName, '+', '-', 'count', 1); | ||
const defaultRec = { | ||
id: '0-0', | ||
nextTimestamp: '', | ||
}; | ||
const fixLastNextTimestamp = `${Date.now()}`; | ||
const fixLastId = await redis.xadd(streamName, '*', 'nextTimestamp', fixLastNextTimestamp); | ||
|
||
function readRecords(records: [string, string[]][]): Record { | ||
if (records.length > 0) { | ||
return { | ||
id: records[0][0], | ||
nextTimestamp: records[0][1][1], | ||
}; | ||
} else { | ||
return defaultRec; | ||
} | ||
} | ||
|
||
return { | ||
first: readRecords(first), | ||
last: readRecords(last), | ||
fixLast: { | ||
id: fixLastId, | ||
nextTimestamp: fixLastNextTimestamp, | ||
}, | ||
}; | ||
} |
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,32 @@ | ||
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose'; | ||
import { addFixRecordToDelayStream } from '../helpers'; | ||
import { Options } from '../definitions'; | ||
|
||
export function createQueuePepUpFC( | ||
sc: SchemaComposer<any>, | ||
opts: Options | ||
): ObjectTypeComposerFieldConfigAsObjectDefinition<any, any> { | ||
const { typePrefix } = opts; | ||
|
||
return { | ||
type: sc.createObjectTC({ | ||
name: `${typePrefix}QueuePepUpPayload`, | ||
fields: { records: 'JSON' }, | ||
}), | ||
args: { | ||
prefix: { | ||
type: 'String!', | ||
defaultValue: 'bull', | ||
}, | ||
queueName: 'String!', | ||
checkExistence: { | ||
type: 'Boolean', | ||
defaultValue: true, | ||
}, | ||
}, | ||
resolve: async (_, { prefix, queueName, checkExistence }) => { | ||
const records = await addFixRecordToDelayStream(prefix, queueName, opts, checkExistence); | ||
return { records }; | ||
}, | ||
}; | ||
} |