n8n
Version:
n8n Workflow Automation Tool
153 lines • 6.67 kB
JavaScript
"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.WorkflowStatisticsRollupService = void 0;
const backend_common_1 = require("@n8n/backend-common");
const config_1 = require("@n8n/config");
const constants_1 = require("@n8n/constants");
const db_1 = require("@n8n/db");
const decorators_1 = require("@n8n/decorators");
const di_1 = require("@n8n/di");
const n8n_core_1 = require("n8n-core");
const n8n_workflow_1 = require("n8n-workflow");
const node_assert_1 = require("node:assert");
const workflow_statistics_service_1 = require("./workflow-statistics.service");
const BATCH_SIZE = 5000;
const STEADY_INTERVAL_MS = 5 * constants_1.Time.seconds.toMilliseconds;
const BUSY_INTERVAL_MS = 250;
const SKIP_WARN_THRESHOLD = 5;
let WorkflowStatisticsRollupService = class WorkflowStatisticsRollupService {
constructor(logger, errorReporter, instanceSettings, dbConnection, databaseConfig, dbLockService, repository, statisticsService) {
this.logger = logger;
this.errorReporter = errorReporter;
this.instanceSettings = instanceSettings;
this.dbConnection = dbConnection;
this.databaseConfig = databaseConfig;
this.dbLockService = dbLockService;
this.repository = repository;
this.statisticsService = statisticsService;
this.isShuttingDown = false;
this.consecutiveLockSkips = 0;
this.totalLockSkips = 0;
this.logger = this.logger.scoped('workflow-statistics');
}
get shouldRun() {
return (this.databaseConfig.type === 'postgresdb' &&
this.dbConnection.connectionState.migrated &&
this.instanceSettings.instanceType === 'main' &&
this.instanceSettings.isLeader &&
!this.isShuttingDown);
}
init() {
(0, node_assert_1.strict)(this.instanceSettings.instanceRole !== 'unset', 'Instance role is not set');
if (this.shouldRun)
this.start();
}
start() {
if (!this.shouldRun || this.timeout !== undefined)
return;
this.scheduleNext(0);
this.logger.debug('Workflow statistics rollup interval started');
}
stop() {
clearTimeout(this.timeout);
this.timeout = undefined;
}
async shutdown() {
this.isShuttingDown = true;
clearTimeout(this.timeout);
this.timeout = undefined;
await this.inflightRollup;
}
scheduleNext(delayMs) {
this.timeout = setTimeout(() => {
let nextDelayMs = STEADY_INTERVAL_MS;
this.inflightRollup = this.rollup()
.then((increments) => {
if (increments >= BATCH_SIZE)
nextDelayMs = BUSY_INTERVAL_MS;
})
.catch((error) => {
this.errorReporter.error(error, { shouldBeLogged: true });
})
.finally(() => {
this.inflightRollup = undefined;
if (this.timeout !== undefined)
this.scheduleNext(nextDelayMs);
});
}, delayMs);
}
async rollup() {
const result = await this.foldBatch();
if (!result)
return 0;
await this.emitMilestones(result.firstOccurrences);
return result.increments;
}
async foldBatch() {
try {
const result = await this.dbLockService.tryWithLock(1003, async (tx) => await this.repository.rollupIncrements(tx, BATCH_SIZE));
this.consecutiveLockSkips = 0;
return result;
}
catch (error) {
if (error instanceof n8n_workflow_1.OperationalError) {
this.registerLockSkip();
return null;
}
throw error;
}
}
registerLockSkip() {
this.consecutiveLockSkips++;
this.totalLockSkips++;
if (this.consecutiveLockSkips % SKIP_WARN_THRESHOLD !== 0)
return;
this.logger.warn('Workflow statistics rollup repeatedly skipped: lock held by another process', {
consecutiveLockSkips: this.consecutiveLockSkips,
totalLockSkips: this.totalLockSkips,
});
}
async emitMilestones(firstOccurrences) {
for (const occurrence of firstOccurrences) {
try {
await this.statisticsService.emitFirstOccurrenceEvent(occurrence.name, occurrence.workflowId, occurrence.workflowName, occurrence.firstEventMs);
}
catch (error) {
this.errorReporter.error(error, { shouldBeLogged: true });
}
}
}
};
exports.WorkflowStatisticsRollupService = WorkflowStatisticsRollupService;
__decorate([
(0, decorators_1.OnLeaderTakeover)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], WorkflowStatisticsRollupService.prototype, "start", null);
__decorate([
(0, decorators_1.OnLeaderStepdown)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], WorkflowStatisticsRollupService.prototype, "stop", null);
__decorate([
(0, decorators_1.OnShutdown)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], WorkflowStatisticsRollupService.prototype, "shutdown", null);
exports.WorkflowStatisticsRollupService = WorkflowStatisticsRollupService = __decorate([
(0, di_1.Service)(),
__metadata("design:paramtypes", [backend_common_1.Logger, n8n_core_1.ErrorReporter, n8n_core_1.InstanceSettings, db_1.DbConnection, config_1.DatabaseConfig, db_1.DbLockService, db_1.WorkflowStatisticsRepository, workflow_statistics_service_1.WorkflowStatisticsService])
], WorkflowStatisticsRollupService);
//# sourceMappingURL=workflow-statistics-rollup.service.js.map