Skip to content

Commit

Permalink
tryForLeader failure emits an error rather than throws (#1137)
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler authored Dec 23, 2024
1 parent f08ed43 commit 7fff34b
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions src/core/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,30 @@ export class Scheduler extends EventEmitter {
return;
}

const lockedByMe = await this.connection.redis.set(
leaderKey,
this.options.name,
"EX",
this.options.leaderLockTimeout,
"NX",
);

if (lockedByMe && lockedByMe.toUpperCase() === "OK") {
return true;
}

const currentLeaderName = await this.connection.redis.get(leaderKey);
if (currentLeaderName === this.options.name) {
await this.connection.redis.expire(
try {
const lockedByMe = await this.connection.redis.set(
leaderKey,
this.options.name,
"EX",
this.options.leaderLockTimeout,
"NX",
);
return true;

if (lockedByMe && lockedByMe.toUpperCase() === "OK") {
return true;
}

const currentLeaderName = await this.connection.redis.get(leaderKey);
if (currentLeaderName === this.options.name) {
await this.connection.redis.expire(
leaderKey,
this.options.leaderLockTimeout,
);
return true;
}
} catch (error) {
this.emit("error", error);
return false;
}

return false;
Expand Down

0 comments on commit 7fff34b

Please sign in to comment.