Skip to content

Commit

Permalink
perf(job): generate priority limit constant once (#2102)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Jul 28, 2023
1 parent 2ad2530 commit 8880f9f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
27 changes: 16 additions & 11 deletions src/classes/job.ts
Expand Up @@ -45,6 +45,8 @@ const optsDecodeMap = {

const optsEncodeMap = invert(optsDecodeMap);

export const PRIORITY_LIMIT = 2 ** 21;

/**
* Job
*
Expand Down Expand Up @@ -1067,6 +1069,18 @@ export class Job<
addJob(client: RedisClient, parentOpts?: ParentOpts): Promise<string> {
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;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/base-job-options.ts
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 8880f9f

Please sign in to comment.