Skip to content

Commit

Permalink
chore: move finishedErrors back to the scripts class
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Oct 12, 2024
1 parent deaa75d commit 72d4896
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 62 deletions.
3 changes: 1 addition & 2 deletions src/classes/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
lengthInUtf8Bytes,
parseObjectValues,
tryCatch,
finishedErrors,
} from '../utils';
import { Backoffs } from './backoffs';
import { Scripts, raw2NextJobData } from './scripts';
Expand Down Expand Up @@ -748,7 +747,7 @@ export class Job<

const result = results[results.length - 1][1] as number;
if (result < 0) {
throw finishedErrors({
throw this.scripts.finishedErrors({
code: result,
jobId: this.id,
command,
Expand Down
76 changes: 57 additions & 19 deletions src/classes/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ import {
RedisJobOptions,
} from '../types';
import { ErrorCode } from '../enums';
import {
array2obj,
finishedErrors,
getParentKey,
isRedisVersionLowerThan,
} from '../utils';
import { array2obj, getParentKey, isRedisVersionLowerThan } from '../utils';
import { ChainableCommander } from 'ioredis';

export type JobData = [JobJsonRaw | number, string?];
Expand Down Expand Up @@ -225,7 +220,7 @@ export class Scripts {
}

if (<number>result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: <number>result,
parentKey: parentOpts.parentKey,
command: 'addJob',
Expand Down Expand Up @@ -424,7 +419,7 @@ export class Scripts {
const result = await (<any>client).removeJob(args);

if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'removeJob',
Expand Down Expand Up @@ -463,7 +458,7 @@ export class Scripts {
const result = await (<any>client).updateData(keys.concat([dataJson]));

if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId: job.id,
command: 'updateData',
Expand All @@ -489,7 +484,7 @@ export class Scripts {
);

if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'updateProgress',
Expand All @@ -514,7 +509,7 @@ export class Scripts {
);

if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'addLog',
Expand Down Expand Up @@ -598,7 +593,7 @@ export class Scripts {

const result = await (<any>client).moveToFinished(args);
if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'moveToFinished',
Expand Down Expand Up @@ -662,7 +657,7 @@ export class Scripts {
case 1:
return false;
default:
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
parentKey,
Expand Down Expand Up @@ -826,7 +821,7 @@ export class Scripts {
const args = this.changeDelayArgs(jobId, delay);
const result = await (<any>client).changeDelay(args);
if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'changeDelay',
Expand Down Expand Up @@ -863,7 +858,7 @@ export class Scripts {
const args = this.changePriorityArgs(jobId, priority, lifo);
const result = await (<any>client).changePriority(args);
if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'changePriority',
Expand Down Expand Up @@ -981,7 +976,7 @@ export class Scripts {
const args = this.moveToDelayedArgs(jobId, timestamp, token, delay, opts);
const result = await (<any>client).moveToDelayed(args);
if (result < 0) {
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'moveToDelayed',
Expand Down Expand Up @@ -1017,7 +1012,7 @@ export class Scripts {
case 1:
return false;
default:
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId,
command: 'moveToWaitingChildren',
Expand Down Expand Up @@ -1176,7 +1171,7 @@ export class Scripts {
case 1:
return;
default:
throw finishedErrors({
throw this.finishedErrors({
code: result,
jobId: job.id,
command: 'reprocessJob',
Expand Down Expand Up @@ -1240,7 +1235,7 @@ export class Scripts {

const code = await (<any>client).promote(keys.concat(args));
if (code < 0) {
throw finishedErrors({
throw this.finishedErrors({
code,
jobId,
command: 'promote',
Expand Down Expand Up @@ -1424,6 +1419,49 @@ export class Scripts {
};
}
}

finishedErrors({
code,
jobId,
parentKey,
command,
state,
}: {
code: number;
jobId?: string;
parentKey?: string;
command: string;
state?: string;
}): Error {
switch (code) {
case ErrorCode.JobNotExist:
return new Error(`Missing key for job ${jobId}. ${command}`);
case ErrorCode.JobLockNotExist:
return new Error(`Missing lock for job ${jobId}. ${command}`);
case ErrorCode.JobNotInState:
return new Error(
`Job ${jobId} is not in the ${state} state. ${command}`,
);
case ErrorCode.JobPendingDependencies:
return new Error(`Job ${jobId} has pending dependencies. ${command}`);
case ErrorCode.ParentJobNotExist:
return new Error(`Missing key for parent job ${parentKey}. ${command}`);
case ErrorCode.JobLockMismatch:
return new Error(
`Lock mismatch for job ${jobId}. Cmd ${command} from ${state}`,
);
case ErrorCode.ParentJobCannotBeReplaced:
return new Error(
`The parent job ${parentKey} cannot be replaced. ${command}`,
);
case ErrorCode.JobBelongsToJobScheduler:
return new Error(
`Job ${jobId} belongs to a job scheduler and cannot be removed directly. ${command}`,
);
default:
return new Error(`Unknown code ${code} error for ${jobId}. ${command}`);
}
}
}

export function raw2NextJobData(raw: any[]) {
Expand Down
41 changes: 0 additions & 41 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,47 +252,6 @@ export const toString = (value: any): string => {

export const QUEUE_EVENT_SUFFIX = ':qe';

export const finishedErrors = ({
code,
jobId,
parentKey,
command,
state,
}: {
code: number;
jobId?: string;
parentKey?: string;
command: string;
state?: string;
}): Error => {
switch (code) {
case ErrorCode.JobNotExist:
return new Error(`Missing key for job ${jobId}. ${command}`);
case ErrorCode.JobLockNotExist:
return new Error(`Missing lock for job ${jobId}. ${command}`);
case ErrorCode.JobNotInState:
return new Error(`Job ${jobId} is not in the ${state} state. ${command}`);
case ErrorCode.JobPendingDependencies:
return new Error(`Job ${jobId} has pending dependencies. ${command}`);
case ErrorCode.ParentJobNotExist:
return new Error(`Missing key for parent job ${parentKey}. ${command}`);
case ErrorCode.JobLockMismatch:
return new Error(
`Lock mismatch for job ${jobId}. Cmd ${command} from ${state}`,
);
case ErrorCode.ParentJobCannotBeReplaced:
return new Error(
`The parent job ${parentKey} cannot be replaced. ${command}`,
);
case ErrorCode.JobBelongsToJobScheduler:
return new Error(
`Job ${jobId} belongs to a job scheduler and cannot be removed directly. ${command}`,
);
default:
return new Error(`Unknown code ${code} error for ${jobId}. ${command}`);
}
};

export const readPackageJson: () => { name: string; version: string } = () => {
const packageJsonPossiblePaths = [
join(__dirname, '../package.json'),
Expand Down

0 comments on commit 72d4896

Please sign in to comment.