refactor(core): Use an IoC container to manage singleton classes [Part-1] (no-changelog) (#5509)

* add typedi

* convert ActiveWorkflowRunner into an injectable service

* convert ExternalHooks into an injectable service

* convert InternalHooks into an injectable service

* convert LoadNodesAndCredentials into an injectable service

* convert NodeTypes and CredentialTypes into an injectable service

* convert ActiveExecutions into an injectable service

* convert WaitTracker into an injectable service

* convert Push into an injectable service

* convert ActiveWebhooks and  TestWebhooks into an injectable services

* handle circular references, and log errors when a circular dependency is found
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-02-21 19:21:56 +01:00
committed by GitHub
parent aca94bb995
commit 52f740b9e8
79 changed files with 594 additions and 634 deletions

View File

@@ -13,8 +13,9 @@ import type { JsonObject } from 'swagger-ui-express';
import config from '@/config';
import * as Db from '@/Db';
import { InternalHooksManager } from '@/InternalHooksManager';
import { getInstanceBaseUrl } from '@/UserManagement/UserManagementHelper';
import { Container } from 'typedi';
import { InternalHooks } from '@/InternalHooks';
async function createApiRouter(
version: string,
@@ -100,7 +101,7 @@ async function createApiRouter(
if (!user) return false;
void InternalHooksManager.getInstance().onUserInvokedApi({
void Container.get(InternalHooks).onUserInvokedApi({
user_id: user.id,
path: req.path,
method: req.method,

View File

@@ -19,6 +19,7 @@ import {
saveCredential,
toJsonSchema,
} from './credentials.service';
import { Container } from 'typedi';
export = {
createCredential: [
@@ -87,7 +88,7 @@ export = {
const { credentialTypeName } = req.params;
try {
CredentialTypes().getByName(credentialTypeName);
Container.get(CredentialTypes).getByName(credentialTypeName);
} catch (error) {
return res.status(404).json({ message: 'Not Found' });
}

View File

@@ -7,6 +7,7 @@ import { CredentialsHelper } from '@/CredentialsHelper';
import { CredentialTypes } from '@/CredentialTypes';
import type { CredentialRequest } from '../../../types';
import { toJsonSchema } from './credentials.service';
import { Container } from 'typedi';
export const validCredentialType = (
req: CredentialRequest.Create,
@@ -14,7 +15,7 @@ export const validCredentialType = (
next: express.NextFunction,
): express.Response | void => {
try {
CredentialTypes().getByName(req.body.type);
Container.get(CredentialTypes).getByName(req.body.type);
} catch (_) {
return res.status(400).json({ message: 'req.body.type is not a known type' });
}

View File

@@ -8,6 +8,7 @@ import type { User } from '@db/entities/User';
import { ExternalHooks } from '@/ExternalHooks';
import type { IDependency, IJsonSchema } from '../../../types';
import type { CredentialRequest } from '@/requests';
import { Container } from 'typedi';
export async function getCredentials(credentialId: string): Promise<ICredentialsDb | null> {
return Db.collections.Credentials.findOneBy({ id: credentialId });
@@ -62,7 +63,7 @@ export async function saveCredential(
scope: 'credential',
});
await ExternalHooks().run('credentials.create', [encryptedData]);
await Container.get(ExternalHooks).run('credentials.create', [encryptedData]);
return Db.transaction(async (transactionManager) => {
const savedCredential = await transactionManager.save<CredentialsEntity>(credential);
@@ -84,7 +85,7 @@ export async function saveCredential(
}
export async function removeCredential(credentials: CredentialsEntity): Promise<ICredentialsDb> {
await ExternalHooks().run('credentials.delete', [credentials.id]);
await Container.get(ExternalHooks).run('credentials.delete', [credentials.id]);
return Db.collections.Credentials.remove(credentials);
}

View File

@@ -8,12 +8,13 @@ import {
deleteExecution,
getExecutionsCount,
} from './executions.service';
import * as ActiveExecutions from '@/ActiveExecutions';
import { ActiveExecutions } from '@/ActiveExecutions';
import { authorize, validCursor } from '../../shared/middlewares/global.middleware';
import type { ExecutionRequest } from '../../../types';
import { getSharedWorkflowIds } from '../workflows/workflows.service';
import { encodeNextCursor } from '../../shared/services/pagination.service';
import { InternalHooksManager } from '@/InternalHooksManager';
import { Container } from 'typedi';
import { InternalHooks } from '@/InternalHooks';
export = {
deleteExecution: [
@@ -66,7 +67,7 @@ export = {
return res.status(404).json({ message: 'Not Found' });
}
void InternalHooksManager.getInstance().onUserRetrievedExecution({
void Container.get(InternalHooks).onUserRetrievedExecution({
user_id: req.user.id,
public_api: true,
});
@@ -95,7 +96,7 @@ export = {
}
// get running workflows so we exclude them from the result
const runningExecutionsIds = ActiveExecutions.getInstance()
const runningExecutionsIds = Container.get(ActiveExecutions)
.getActiveExecutions()
.map(({ id }) => id);
@@ -116,7 +117,7 @@ export = {
const count = await getExecutionsCount(filters);
void InternalHooksManager.getInstance().onUserRetrievedAllExecutions({
void Container.get(InternalHooks).onUserRetrievedAllExecutions({
user_id: req.user.id,
public_api: true,
});

View File

@@ -1,12 +1,11 @@
import type express from 'express';
import { Container } from 'typedi';
import type { FindManyOptions, FindOptionsWhere } from 'typeorm';
import { In } from 'typeorm';
import * as ActiveWorkflowRunner from '@/ActiveWorkflowRunner';
import { ActiveWorkflowRunner } from '@/ActiveWorkflowRunner';
import config from '@/config';
import { WorkflowEntity } from '@db/entities/WorkflowEntity';
import { InternalHooksManager } from '@/InternalHooksManager';
import { ExternalHooks } from '@/ExternalHooks';
import { addNodeIds, replaceInvalidCredentials } from '@/WorkflowHelpers';
import type { WorkflowRequest } from '../../../types';
@@ -29,6 +28,7 @@ import {
parseTagNames,
} from './workflows.service';
import { WorkflowsService } from '@/workflows/workflows.services';
import { InternalHooks } from '@/InternalHooks';
export = {
createWorkflow: [
@@ -50,8 +50,8 @@ export = {
const createdWorkflow = await createWorkflow(workflow, req.user, role);
await ExternalHooks().run('workflow.afterCreate', [createdWorkflow]);
void InternalHooksManager.getInstance().onWorkflowCreated(req.user, createdWorkflow, true);
await Container.get(ExternalHooks).run('workflow.afterCreate', [createdWorkflow]);
void Container.get(InternalHooks).onWorkflowCreated(req.user, createdWorkflow, true);
return res.json(createdWorkflow);
},
@@ -84,7 +84,7 @@ export = {
return res.status(404).json({ message: 'Not Found' });
}
void InternalHooksManager.getInstance().onUserRetrievedWorkflow({
void Container.get(InternalHooks).onUserRetrievedWorkflow({
user_id: req.user.id,
public_api: true,
});
@@ -145,7 +145,7 @@ export = {
count = await getWorkflowsCount(query);
}
void InternalHooksManager.getInstance().onUserRetrievedAllWorkflows({
void Container.get(InternalHooks).onUserRetrievedAllWorkflows({
user_id: req.user.id,
public_api: true,
});
@@ -182,7 +182,7 @@ export = {
await replaceInvalidCredentials(updateData);
addNodeIds(updateData);
const workflowRunner = ActiveWorkflowRunner.getInstance();
const workflowRunner = Container.get(ActiveWorkflowRunner);
if (sharedWorkflow.workflow.active) {
// When workflow gets saved always remove it as the triggers could have been
@@ -210,8 +210,8 @@ export = {
const updatedWorkflow = await getWorkflowById(sharedWorkflow.workflowId);
await ExternalHooks().run('workflow.afterUpdate', [updateData]);
void InternalHooksManager.getInstance().onWorkflowSaved(req.user, updateData, true);
await Container.get(ExternalHooks).run('workflow.afterUpdate', [updateData]);
void Container.get(InternalHooks).onWorkflowSaved(req.user, updateData, true);
return res.json(updatedWorkflow);
},
@@ -231,7 +231,7 @@ export = {
if (!sharedWorkflow.workflow.active) {
try {
await ActiveWorkflowRunner.getInstance().add(sharedWorkflow.workflowId, 'activate');
await Container.get(ActiveWorkflowRunner).add(sharedWorkflow.workflowId, 'activate');
} catch (error) {
if (error instanceof Error) {
return res.status(400).json({ message: error.message });
@@ -263,7 +263,7 @@ export = {
return res.status(404).json({ message: 'Not Found' });
}
const workflowRunner = ActiveWorkflowRunner.getInstance();
const workflowRunner = Container.get(ActiveWorkflowRunner);
if (sharedWorkflow.workflow.active) {
await workflowRunner.remove(sharedWorkflow.workflowId);