feat(core): Add configurable execution history limit (#5505)

* Prune execution data when more than cofnfigured limit

* use stricter typings

* use `pruneDataMaxCount`

---------

Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
This commit is contained in:
Ahsan Virani 2023-02-20 10:28:38 +01:00 committed by GitHub
parent 9a1e7b52f7
commit db702932f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View file

@ -43,6 +43,7 @@ import {
} from 'n8n-workflow';
import pick from 'lodash.pick';
import type { FindOptionsWhere } from 'typeorm';
import { LessThanOrEqual } from 'typeorm';
import { DateUtils } from 'typeorm/util/DateUtils';
import config from '@/config';
@ -202,6 +203,7 @@ async function pruneExecutionData(this: WorkflowHooks): Promise<void> {
throttling = true;
const timeout = config.getEnv('executions.pruneDataTimeout'); // in seconds
const maxAge = config.getEnv('executions.pruneDataMaxAge'); // in h
const maxCount = config.getEnv('executions.pruneDataMaxCount');
const date = new Date(); // today
date.setHours(date.getHours() - maxAge);
@ -209,7 +211,21 @@ async function pruneExecutionData(this: WorkflowHooks): Promise<void> {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const utcDate = DateUtils.mixedDateToUtcDatetimeString(date);
const toPrune = { stoppedAt: LessThanOrEqual(utcDate) };
const toPrune: FindOptionsWhere<IExecutionFlattedDb> = { stoppedAt: LessThanOrEqual(utcDate) };
if (maxCount > 0) {
const executions = await Db.collections.Execution.find({
select: ['id'],
skip: maxCount,
take: 1,
order: { id: 'DESC' },
});
if (executions[0]) {
toPrune.id = LessThanOrEqual(executions[0].id);
}
}
const isBinaryModeDefaultMode = config.getEnv('binaryDataManager.mode') === 'default';
try {
const executions = isBinaryModeDefaultMode

View file

@ -325,6 +325,16 @@ export const schema = {
default: 3600,
env: 'EXECUTIONS_DATA_PRUNE_TIMEOUT',
},
// Additional pruning option to delete executions if total count exceeds the configured max.
// Deletes the oldest entries first
// Default is 0 = No limit
pruneDataMaxCount: {
doc: 'Maximum number of executions to keep in DB. Default 0 = no limit',
format: Number,
default: 0,
env: 'EXECUTIONS_DATA_PRUNE_MAX_COUNT',
},
},
queue: {