Skip to content

Commit

Permalink
fix: prevent same job lock to be updated multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
yujiosaka committed Oct 25, 2023
1 parent 5fb3bcd commit 07247a4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/job-store/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class MongodbJobStore implements BaseJobStore<Types.ObjectId> {

async deactivateJobLock(jobName: string, jobId: Types.ObjectId): Promise<MongodbJobLock> {
const deactivatedJobLock = await this.#model.findOneAndUpdate(
{ _id: jobId },
{ _id: jobId, jobName, isActive: true },
{ isActive: false, updatedAt: new Date() },
{ new: true },
);
Expand All @@ -67,7 +67,7 @@ export default class MongodbJobStore implements BaseJobStore<Types.ObjectId> {
}

async removeJobLock(jobName: string, jobId: Types.ObjectId): Promise<void> {
const result = await this.#model.deleteOne({ _id: jobId });
const result = await this.#model.deleteOne({ _id: jobId, jobName, isActive: true });
if (result.deletedCount === 0) throw new Error(`Cannot find job lock for ${jobName}`);
}
}
6 changes: 3 additions & 3 deletions src/job-store/typeorm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ export default abstract class TypeormJobStore implements BaseJobStore<string> {
}

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

return await this.#repository.save(deactivatedJobLock);
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 });
const result = await this.#repository.delete({ _id: jobId, jobName, isActive: true });
if (result.affected === 0) throw new Error(`Cannot find job lock for ${jobName}`);
}
}

0 comments on commit 07247a4

Please sign in to comment.