Skip to content

Commit

Permalink
feat: distinguish job lock not found error
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiosaka committed Oct 25, 2023
1 parent c0b4c77 commit 2d72fdf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
6 changes: 3 additions & 3 deletions examples/job-stores/custom-job-store.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bun
import { isEqual } from "date-fns";
import { v4 } from "uuid";
import Cronyx, { BaseJobLock } from "../../src";
import Cronyx, { BaseJobLock, JobLockNotFoundError } from "../../src";
import type BaseJobStore from "../../src/job-store";

// Create a new cache by extending BaseJobStore interface
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class MemoryJobStore implements BaseJobStore<string> {
async deactivateJobLock(jobName: string, jobId: string): Promise<BaseJobLock<string>> {
const lastJobLock = await this.fetchLastJobLock(jobName);
if (!lastJobLock || !lastJobLock.isActive || lastJobLock._id !== jobId) {
throw new Error(`Cannot find job lock for ${jobName}`);
throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}

const detivatedJobLock = { ...lastJobLock, isActive: false, updatedAt: new Date() };
Expand All @@ -79,7 +79,7 @@ export default class MemoryJobStore implements BaseJobStore<string> {
async removeJobLock(jobName: string, jobId: string): Promise<void> {
const lastJobLock = await this.fetchLastJobLock(jobName);
if (!lastJobLock || lastJobLock._id !== jobId) {
throw new Error(`Cannot find job lock for ${jobName}`);
throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}

this.#jobLocks[jobName].pop();
Expand Down
5 changes: 5 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export class CronyxError extends Error {
return this.constructor.name;
}
}

/**
* @public
*/
export class JobLockNotFoundError extends Error {}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export { default as TypeormJobStore } from "./job-store/typeorm";
export { default as MysqlJobStore } from "./job-store/typeorm/mysql";
export { default as PostgresJobStore } from "./job-store/typeorm/postgres";
export { default as Job } from "./job";
export { CronyxError } from "./error";
export { CronyxError, JobLockNotFoundError } from "./error";

/**
* @public
Expand Down
5 changes: 3 additions & 2 deletions src/job-store/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { MongoClientOptions } from "mongodb";
import { createConnection } from "mongoose";
import type { Connection, Model, Types } from "mongoose";
import type BaseJobStore from ".";
import { JobLockNotFoundError } from "../error";
import type MongodbJobLock from "../job-lock/mongodb";
import { mongodbJobLockSchema } from "../job-lock/mongodb";

Expand Down Expand Up @@ -61,13 +62,13 @@ export default class MongodbJobStore implements BaseJobStore<Types.ObjectId> {
{ isActive: false, updatedAt: new Date() },
{ new: true },
);
if (!deactivatedJobLock) throw new Error(`Cannot find job lock for ${jobName}`);
if (!deactivatedJobLock) throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);

return deactivatedJobLock;
}

async removeJobLock(jobName: string, jobId: Types.ObjectId): Promise<void> {
const result = await this.#model.deleteOne({ _id: jobId, jobName, isActive: true });
if (result.deletedCount === 0) throw new Error(`Cannot find job lock for ${jobName}`);
if (result.deletedCount === 0) throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}
}
5 changes: 3 additions & 2 deletions src/job-store/redis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { isEqual } from "date-fns";
import { createClient, RedisClientOptions, WatchError } from "redis";
import type BaseJobStore from ".";
import { JobLockNotFoundError } from "../error";
import RedisJobLock from "../job-lock/redis";

const ACTIVE_JOB_LOCK_PREFIX = "joblocks:active:";
Expand Down Expand Up @@ -100,7 +101,7 @@ export default class RedisJobStore implements BaseJobStore<string> {
async deactivateJobLock(jobName: string, jobId: string): Promise<RedisJobLock> {
const activeJobLock = await this.#getActiveJobLock(jobName);
if (!activeJobLock || activeJobLock._id !== jobId) {
throw new Error(`Cannot find job lock for ${jobName}`);
throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}

const detivatedJobLock = { ...activeJobLock, isActive: false, updatedAt: new Date() };
Expand All @@ -116,7 +117,7 @@ export default class RedisJobStore implements BaseJobStore<string> {
async removeJobLock(jobName: string, jobId: string): Promise<void> {
const activeJobLock = await this.#getActiveJobLock(jobName);
if (!activeJobLock || activeJobLock._id !== jobId) {
throw new Error(`Cannot find job lock for ${jobName}`);
throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}

await this.#client.del(`${ACTIVE_JOB_LOCK_PREFIX}${jobName}`);
Expand Down
5 changes: 3 additions & 2 deletions src/job-store/typeorm/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Repository } from "typeorm";
import { DataSource } from "typeorm";
import type BaseJobStore from "..";
import { JobLockNotFoundError } from "../../error";
import { TypeormJobLockEntity } from "../../job-lock/typeorm";
import type TypeormJobLock from "../../job-lock/typeorm";
import { hasErrorCode } from "../../util";
Expand Down Expand Up @@ -74,13 +75,13 @@ export default abstract class TypeormJobStore implements BaseJobStore<string> {

async deactivateJobLock(jobName: string, jobId: string): Promise<TypeormJobLock> {
const deactivatedJobLock = await this.#repository.findOne({ where: { _id: jobId, jobName, isActive: true } });
if (!deactivatedJobLock) throw new Error(`Cannot find job lock for ${jobName}`);
if (!deactivatedJobLock) throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);

return await this.#repository.save({ ...deactivatedJobLock, isActive: false, updatedAt: new Date() });
}

async removeJobLock(jobName: string, jobId: string): Promise<void> {
const result = await this.#repository.delete({ _id: jobId, jobName, isActive: true });
if (result.affected === 0) throw new Error(`Cannot find job lock for ${jobName}`);
if (result.affected === 0) throw new JobLockNotFoundError(`Cannot find job lock for ${jobName}`);
}
}

0 comments on commit 2d72fdf

Please sign in to comment.