fix(core): Change VariablesService to DI and use caching (#6827)

* support redis cluster

* cleanup, fix config schema

* set default prefix to bull

* initial commit

* improve logging

* improve types and refactor

* list support and refactor

* fix redis service and tests

* add comment

* add redis and cache prefix

* use injection

* lint fix

* clean schema comments

* improve naming, tests, cluster client

* merge master

* cache returns unknown instead of T

* update cache service, tests and doc

* remove console.log

* VariablesService as DI, add caching, fix tests

* do not cache null or undefined values

* import fix

* more DI and remove collections

* fix merge

* lint fix

* rename to ~Cached

* fix test for CI

* fix ActiveWorkflowRunner test
This commit is contained in:
Michael Auerswald
2023-08-02 14:51:09 +02:00
committed by GitHub
parent 41d8a18d47
commit 659ca26fe7
11 changed files with 99 additions and 51 deletions

View File

@@ -1,6 +1,5 @@
import { Container } from 'typedi';
import { Container, Service } from 'typedi';
import type { Variables } from '@db/entities/Variables';
import { collections } from '@/Db';
import { InternalHooks } from '@/InternalHooks';
import { generateNanoId } from '@db/utils/generators';
import { canCreateNewVariable } from './enviromentHelpers';
@@ -9,12 +8,9 @@ import { VariablesService } from './variables.service';
export class VariablesLicenseError extends Error {}
export class VariablesValidationError extends Error {}
@Service()
export class EEVariablesService extends VariablesService {
static async getCount(): Promise<number> {
return collections.Variables.count();
}
static validateVariable(variable: Omit<Variables, 'id'>): void {
validateVariable(variable: Omit<Variables, 'id'>): void {
if (variable.key.length > 50) {
throw new VariablesValidationError('key cannot be longer than 50 characters');
}
@@ -26,23 +22,25 @@ export class EEVariablesService extends VariablesService {
}
}
static async create(variable: Omit<Variables, 'id'>): Promise<Variables> {
async create(variable: Omit<Variables, 'id'>): Promise<Variables> {
if (!canCreateNewVariable(await this.getCount())) {
throw new VariablesLicenseError('Variables limit reached');
}
this.validateVariable(variable);
void Container.get(InternalHooks).onVariableCreated({ variable_type: variable.type });
return collections.Variables.save({
const saveResult = await this.variablesRepository.save({
...variable,
id: generateNanoId(),
});
await this.updateCache();
return saveResult;
}
static async update(id: string, variable: Omit<Variables, 'id'>): Promise<Variables> {
async update(id: string, variable: Omit<Variables, 'id'>): Promise<Variables> {
this.validateVariable(variable);
await collections.Variables.update(id, variable);
return (await this.get(id))!;
await this.variablesRepository.update(id, variable);
await this.updateCache();
return (await this.getCached(id))!;
}
}