diff --git a/packages/cli/src/scaling/multi-main-setup.ee.ts b/packages/cli/src/scaling/multi-main-setup.ee.ts index 52f186bdcc..aa977ddc6a 100644 --- a/packages/cli/src/scaling/multi-main-setup.ee.ts +++ b/packages/cli/src/scaling/multi-main-setup.ee.ts @@ -108,15 +108,17 @@ export class MultiMainSetup extends TypedEmitter { const { hostId } = this.instanceSettings; // this can only succeed if leadership is currently vacant - const keySetSuccessfully = await this.publisher.setIfNotExists(this.leaderKey, hostId); + const keySetSuccessfully = await this.publisher.setIfNotExists( + this.leaderKey, + hostId, + this.leaderKeyTtl, + ); if (keySetSuccessfully) { this.logger.debug(`[Instance ID ${hostId}] Leader is now this instance`); this.instanceSettings.markAsLeader(); - await this.publisher.setExpiration(this.leaderKey, this.leaderKeyTtl); - this.emit('leader-takeover'); } else { this.instanceSettings.markAsFollower(); diff --git a/packages/cli/src/scaling/pubsub/publisher.service.ts b/packages/cli/src/scaling/pubsub/publisher.service.ts index a0d2079704..d70060edfc 100644 --- a/packages/cli/src/scaling/pubsub/publisher.service.ts +++ b/packages/cli/src/scaling/pubsub/publisher.service.ts @@ -86,10 +86,9 @@ export class Publisher { // @TODO: The following methods are not pubsub-specific. Consider a dedicated client for multi-main setup. - async setIfNotExists(key: string, value: string) { - const success = await this.client.setnx(key, value); - - return !!success; + async setIfNotExists(key: string, value: string, ttl: number) { + const result = await this.client.set(key, value, 'EX', ttl, 'NX'); + return result === 'OK'; } async setExpiration(key: string, ttl: number) {