From 04113c901b77757a2fabe4bb2fe241412378e6e1 Mon Sep 17 00:00:00 2001 From: Evan Tahler Date: Mon, 23 Dec 2024 10:57:00 -0800 Subject: [PATCH] tryForLeader failure emits an error rather than throws --- src/core/scheduler.ts | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/core/scheduler.ts b/src/core/scheduler.ts index ae898c6b..a1cb7773 100644 --- a/src/core/scheduler.ts +++ b/src/core/scheduler.ts @@ -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;