"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StaleMemberCleanupService = void 0;
const backend_common_1 = require("@n8n/backend-common");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const n8n_core_1 = require("n8n-core");
const instance_registry_service_1 = require("./instance-registry.service");
const instance_registry_types_1 = require("./instance-registry.types");
let StaleMemberCleanupService = class StaleMemberCleanupService {
    constructor(logger, instanceSettings, instanceRegistryService) {
        this.instanceSettings = instanceSettings;
        this.instanceRegistryService = instanceRegistryService;
        this.isShuttingDown = false;
        this.logger = logger.scoped('instance-registry');
    }
    init() {
        if (this.instanceSettings.isLeader)
            this.startCleanup();
    }
    startCleanup() {
        if (this.isShuttingDown)
            return;
        this.cleanupInterval = setInterval(async () => await this.cleanup(), instance_registry_types_1.REGISTRY_CONSTANTS.RECONCILIATION_INTERVAL_MS);
        this.logger.debug('Stale member cleanup scheduled');
    }
    stopCleanup() {
        clearInterval(this.cleanupInterval);
        this.cleanupInterval = undefined;
    }
    async cleanup() {
        try {
            const removed = await this.instanceRegistryService.cleanupStaleMembers();
            if (removed > 0) {
                this.logger.info('Cleaned up stale registry members', { removed });
            }
        }
        catch (error) {
            this.logger.warn('Failed to clean up stale registry members', { error });
        }
    }
    shutdown() {
        this.isShuttingDown = true;
        this.stopCleanup();
    }
};
exports.StaleMemberCleanupService = StaleMemberCleanupService;
__decorate([
    (0, decorators_1.OnLeaderTakeover)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], StaleMemberCleanupService.prototype, "startCleanup", null);
__decorate([
    (0, decorators_1.OnLeaderStepdown)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], StaleMemberCleanupService.prototype, "stopCleanup", null);
__decorate([
    (0, decorators_1.OnShutdown)(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", void 0)
], StaleMemberCleanupService.prototype, "shutdown", null);
exports.StaleMemberCleanupService = StaleMemberCleanupService = __decorate([
    (0, di_1.Service)(),
    __metadata("design:paramtypes", [backend_common_1.Logger,
        n8n_core_1.InstanceSettings,
        instance_registry_service_1.InstanceRegistryService])
], StaleMemberCleanupService);
//# sourceMappingURL=stale-member-cleanup.service.js.map