From 8880f9f2983282d343d603a89abe5e1e6bff78e5 Mon Sep 17 00:00:00 2001 From: Rogger Valverde Date: Fri, 28 Jul 2023 13:09:15 -0600 Subject: [PATCH] perf(job): generate priority limit constant once (#2102) --- src/classes/job.ts | 27 ++++++++++++++++----------- src/interfaces/base-job-options.ts | 2 +- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/classes/job.ts b/src/classes/job.ts index 54de2cf637..d35a6f827a 100644 --- a/src/classes/job.ts +++ b/src/classes/job.ts @@ -45,6 +45,8 @@ const optsDecodeMap = { const optsEncodeMap = invert(optsDecodeMap); +export const PRIORITY_LIMIT = 2 ** 21; + /** * Job * @@ -1067,6 +1069,18 @@ export class Job< addJob(client: RedisClient, parentOpts?: ParentOpts): Promise { const jobData = this.asJSON(); + this.validateOptions(jobData); + + return this.scripts.addJob( + client, + jobData, + jobData.opts, + this.id, + parentOpts, + ); + } + + protected validateOptions(jobData: JobJson) { const exceedLimit = this.opts.sizeLimit && lengthInUtf8Bytes(jobData.data) > this.opts.sizeLimit; @@ -1099,19 +1113,10 @@ export class Job< throw new Error(`Priority should not be float`); } - const priorityLimit = 2 ** 21; - if (this.opts.priority > 2 ** 21) { - throw new Error(`Priority should be between 0 and ${priorityLimit}`); + if (this.opts.priority > PRIORITY_LIMIT) { + throw new Error(`Priority should be between 0 and ${PRIORITY_LIMIT}`); } } - - return this.scripts.addJob( - client, - jobData, - jobData.opts, - this.id, - parentOpts, - ); } protected saveStacktrace(multi: ChainableCommander, err: Error): void { diff --git a/src/interfaces/base-job-options.ts b/src/interfaces/base-job-options.ts index 71d54f14ba..2d630f8c3e 100644 --- a/src/interfaces/base-job-options.ts +++ b/src/interfaces/base-job-options.ts @@ -8,7 +8,7 @@ export interface DefaultJobOptions { timestamp?: number; /** - * Ranges from 1 (highest priority) to MAX_INT (lowest priority). Note that + * Ranges from 1 (highest priority) to 2 097 152 (lowest priority). Note that * using priorities has a slight impact on performance, * so do not use it if not required. */