UNPKG

n8n

Version:

n8n Workflow Automation Tool

75 lines 3.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultiMainSetupLegacy = void 0; class MultiMainSetupLegacy { constructor(logger, instanceSettings, publisher, redisClientService, globalConfig, errorReporter, emit) { this.logger = logger; this.instanceSettings = instanceSettings; this.publisher = publisher; this.redisClientService = redisClientService; this.globalConfig = globalConfig; this.errorReporter = errorReporter; this.emit = emit; this.leaderKeyTtl = this.globalConfig.multiMainSetup.ttl; } async init() { const prefix = this.globalConfig.redis.prefix; const validPrefix = this.redisClientService.toValidPrefix(prefix); this.leaderKey = validPrefix + ':main_instance_leader'; await this.tryBecomeLeader(); } async shutdown() { const { isLeader } = this.instanceSettings; if (isLeader) await this.publisher.clear(this.leaderKey); } async checkLeader() { const leaderId = await this.publisher.get(this.leaderKey); const { hostId } = this.instanceSettings; if (leaderId === hostId) { if (!this.instanceSettings.isLeader) { this.errorReporter.info(`[Instance ID ${hostId}] Remote/Local leadership mismatch, marking self as leader`, { shouldBeLogged: true, shouldReport: true, }); this.instanceSettings.markAsLeader(); this.emit('leader-takeover'); } this.logger.debug(`[Instance ID ${hostId}] Leader is this instance`); await this.publisher.setExpiration(this.leaderKey, this.leaderKeyTtl); return; } if (leaderId && leaderId !== hostId) { this.logger.debug(`[Instance ID ${hostId}] Leader is other instance "${leaderId}"`); if (this.instanceSettings.isLeader) { this.instanceSettings.markAsFollower(); this.emit('leader-stepdown'); this.logger.warn('[Multi-main setup] Leader failed to renew leader key'); } return; } if (!leaderId) { this.logger.debug(`[Instance ID ${hostId}] Leadership vacant, attempting to become leader...`); this.instanceSettings.markAsFollower(); this.emit('leader-stepdown'); await this.tryBecomeLeader(); } } async tryBecomeLeader() { const { hostId } = this.instanceSettings; const keySetSuccessfully = await this.publisher.setIfNotExists(this.leaderKey, hostId, this.leaderKeyTtl); if (keySetSuccessfully) { this.logger.info(`[Instance ID ${hostId}] Leader is now this instance`); this.instanceSettings.markAsLeader(); this.emit('leader-takeover'); } else { this.instanceSettings.markAsFollower(); } } async fetchLeaderKey() { return await this.publisher.get(this.leaderKey); } } exports.MultiMainSetupLegacy = MultiMainSetupLegacy; //# sourceMappingURL=multi-main-setup-legacy.js.map