feat: Workflow History pruning and prune time settings (#7343)
Github issue / Community forum post (link here to close automatically):
This commit is contained in:
3
packages/cli/src/workflows/workflowHistory/constants.ts
Normal file
3
packages/cli/src/workflows/workflowHistory/constants.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { TIME } from '@/constants';
|
||||
|
||||
export const WORKFLOW_HISTORY_PRUNE_INTERVAL = 1 * TIME.HOUR;
|
||||
@@ -1,4 +1,5 @@
|
||||
import { License } from '@/License';
|
||||
import config from '@/config';
|
||||
import Container from 'typedi';
|
||||
|
||||
export function isWorkflowHistoryLicensed() {
|
||||
@@ -7,5 +8,28 @@ export function isWorkflowHistoryLicensed() {
|
||||
}
|
||||
|
||||
export function isWorkflowHistoryEnabled() {
|
||||
return isWorkflowHistoryLicensed();
|
||||
return isWorkflowHistoryLicensed() && config.getEnv('workflowHistory.enabled');
|
||||
}
|
||||
|
||||
export function getWorkflowHistoryLicensePruneTime() {
|
||||
return Container.get(License).getWorkflowHistoryPruneLimit();
|
||||
}
|
||||
|
||||
// Time in hours
|
||||
export function getWorkflowHistoryPruneTime(): number {
|
||||
const licenseTime = Container.get(License).getWorkflowHistoryPruneLimit();
|
||||
const configTime = config.getEnv('workflowHistory.pruneTime');
|
||||
|
||||
// License is infinite and config time is infinite
|
||||
if (licenseTime === -1) {
|
||||
return configTime;
|
||||
}
|
||||
|
||||
// License is not infinite but config is, use license time
|
||||
if (configTime === -1) {
|
||||
return licenseTime;
|
||||
}
|
||||
|
||||
// Return the smallest of the license or config if not infinite
|
||||
return Math.min(configTime, licenseTime);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import { WorkflowHistoryRepository } from '@/databases/repositories';
|
||||
import { Service } from 'typedi';
|
||||
import { WORKFLOW_HISTORY_PRUNE_INTERVAL } from './constants';
|
||||
import { getWorkflowHistoryPruneTime, isWorkflowHistoryEnabled } from './workflowHistoryHelper.ee';
|
||||
import { DateTime } from 'luxon';
|
||||
import { LessThan } from 'typeorm';
|
||||
|
||||
@Service()
|
||||
export class WorkflowHistoryManager {
|
||||
pruneTimer?: NodeJS.Timeout;
|
||||
|
||||
constructor(private workflowHistoryRepo: WorkflowHistoryRepository) {}
|
||||
|
||||
init() {
|
||||
if (this.pruneTimer !== undefined) {
|
||||
clearInterval(this.pruneTimer);
|
||||
}
|
||||
|
||||
this.pruneTimer = setInterval(async () => this.prune(), WORKFLOW_HISTORY_PRUNE_INTERVAL);
|
||||
}
|
||||
|
||||
shutdown() {
|
||||
if (this.pruneTimer !== undefined) {
|
||||
clearInterval(this.pruneTimer);
|
||||
this.pruneTimer = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async prune() {
|
||||
if (!isWorkflowHistoryEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pruneHours = getWorkflowHistoryPruneTime();
|
||||
// No prune time set
|
||||
if (pruneHours === -1) {
|
||||
return;
|
||||
}
|
||||
const pruneDateTime = DateTime.now().minus({ hours: pruneHours }).toJSDate();
|
||||
|
||||
await this.workflowHistoryRepo.delete({
|
||||
createdAt: LessThan(pruneDateTime),
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user