refactor(core): Use typedi to manage EventBus singletons (no-changelog) (#5795)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2023-03-29 20:38:47 +02:00
committed by GitHub
parent be373bb859
commit 522c790817
15 changed files with 145 additions and 154 deletions

View File

@@ -1,3 +1,4 @@
import { Service } from 'typedi';
import { LoggerProxy } from 'n8n-workflow';
import type { MessageEventBusDestinationOptions } from 'n8n-workflow';
import type { DeleteResult } from 'typeorm';
@@ -37,12 +38,9 @@ export interface MessageWithCallback {
confirmCallback: (message: EventMessageTypes, src: EventMessageConfirmSource) => void;
}
@Service()
export class MessageEventBus extends EventEmitter {
private static instance: MessageEventBus;
isInitialized: boolean;
logWriter: MessageEventBusLogWriter;
private isInitialized = false;
destinations: {
[key: string]: MessageEventBusDestination;
@@ -50,16 +48,8 @@ export class MessageEventBus extends EventEmitter {
private pushIntervalTimer: NodeJS.Timer;
constructor() {
constructor(private logWriter: MessageEventBusLogWriter) {
super();
this.isInitialized = false;
}
static getInstance(): MessageEventBus {
if (!MessageEventBus.instance) {
MessageEventBus.instance = new MessageEventBus();
}
return MessageEventBus.instance;
}
/**
@@ -93,7 +83,7 @@ export class MessageEventBus extends EventEmitter {
}
LoggerProxy.debug('Initializing event writer');
this.logWriter = await MessageEventBusLogWriter.getInstance();
await this.logWriter.startThread();
// unsent event check:
// - find unsent messages in current event log(s)
@@ -102,9 +92,9 @@ export class MessageEventBus extends EventEmitter {
LoggerProxy.debug('Checking for unsent event messages');
const unsentAndUnfinished = await this.getUnsentAndUnfinishedExecutions();
LoggerProxy.debug(
`Start logging into ${this.logWriter?.getLogFileName() ?? 'unknown filename'} `,
`Start logging into ${this.logWriter.getLogFileName() ?? 'unknown filename'} `,
);
this.logWriter?.startLogging();
this.logWriter.startLogging();
await this.send(unsentAndUnfinished.unsentMessages);
if (Object.keys(unsentAndUnfinished.unfinishedExecutions).length > 0) {
@@ -171,7 +161,7 @@ export class MessageEventBus extends EventEmitter {
async close() {
LoggerProxy.debug('Shutting down event writer...');
await this.logWriter?.close();
await this.logWriter.close();
for (const destinationName of Object.keys(this.destinations)) {
LoggerProxy.debug(
`Shutting down event destination ${this.destinations[destinationName].getId()}...`,
@@ -186,7 +176,7 @@ export class MessageEventBus extends EventEmitter {
msgs = [msgs];
}
for (const msg of msgs) {
this.logWriter?.putMessage(msg);
this.logWriter.putMessage(msg);
// if there are no set up destinations, immediately mark the event as sent
if (!this.shouldSendMsg(msg)) {
this.confirmSent(msg, { id: '0', name: 'eventBus' });
@@ -211,7 +201,7 @@ export class MessageEventBus extends EventEmitter {
}
confirmSent(msg: EventMessageTypes, source?: EventMessageConfirmSource) {
this.logWriter?.confirmMessageSent(msg.id, source);
this.logWriter.confirmMessageSent(msg.id, source);
}
private hasAnyDestinationSubscribedToEvent(msg: EventMessageTypes): boolean {
@@ -256,7 +246,7 @@ export class MessageEventBus extends EventEmitter {
async getEventsFailed(amount = 5): Promise<FailedEventSummary[]> {
const result: FailedEventSummary[] = [];
try {
const queryResult = await this.logWriter?.getMessagesAll();
const queryResult = await this.logWriter.getMessagesAll();
const uniques = uniqby(queryResult, 'id');
const filteredExecutionIds = uniques
.filter((e) =>
@@ -296,25 +286,25 @@ export class MessageEventBus extends EventEmitter {
}
async getEventsAll(): Promise<EventMessageTypes[]> {
const queryResult = await this.logWriter?.getMessagesAll();
const queryResult = await this.logWriter.getMessagesAll();
const filtered = uniqby(queryResult, 'id');
return filtered;
}
async getEventsSent(): Promise<EventMessageTypes[]> {
const queryResult = await this.logWriter?.getMessagesSent();
const queryResult = await this.logWriter.getMessagesSent();
const filtered = uniqby(queryResult, 'id');
return filtered;
}
async getEventsUnsent(): Promise<EventMessageTypes[]> {
const queryResult = await this.logWriter?.getMessagesUnsent();
const queryResult = await this.logWriter.getMessagesUnsent();
const filtered = uniqby(queryResult, 'id');
return filtered;
}
async getUnfinishedExecutions(): Promise<Record<string, EventMessageTypes[]>> {
const queryResult = await this.logWriter?.getUnfinishedExecutions();
const queryResult = await this.logWriter.getUnfinishedExecutions();
return queryResult;
}
@@ -322,7 +312,7 @@ export class MessageEventBus extends EventEmitter {
unsentMessages: EventMessageTypes[];
unfinishedExecutions: Record<string, EventMessageTypes[]>;
}> {
const queryResult = await this.logWriter?.getUnsentAndUnfinishedExecutions();
const queryResult = await this.logWriter.getUnsentAndUnfinishedExecutions();
return queryResult;
}
@@ -336,7 +326,7 @@ export class MessageEventBus extends EventEmitter {
executionId: string,
logHistory?: number,
): Promise<EventMessageTypes[]> {
const result = await this.logWriter?.getMessagesByExecutionId(executionId, logHistory);
const result = await this.logWriter.getMessagesByExecutionId(executionId, logHistory);
return result;
}
/**
@@ -355,5 +345,3 @@ export class MessageEventBus extends EventEmitter {
await this.send(new EventMessageNode(options));
}
}
export const eventBus = MessageEventBus.getInstance();

View File

@@ -1,3 +1,4 @@
import { Container } from 'typedi';
import { parse, stringify } from 'flatted';
import type { IRun, IRunExecutionData, ITaskData } from 'n8n-workflow';
import { NodeOperationError, WorkflowOperationError } from 'n8n-workflow';
@@ -5,12 +6,11 @@ import * as Db from '@/Db';
import type { EventMessageTypes, EventNamesTypes } from '../EventMessageClasses';
import type { DateTime } from 'luxon';
import { Push } from '@/push';
import type { IPushDataExecutionRecovered } from '../../Interfaces';
import { workflowExecutionCompleted } from '../../events/WorkflowStatistics';
import { eventBus } from './MessageEventBus';
import { Container } from 'typedi';
import type { IPushDataExecutionRecovered } from '@/Interfaces';
import { workflowExecutionCompleted } from '@/events/WorkflowStatistics';
import { InternalHooks } from '@/InternalHooks';
import { getWorkflowHooksMain } from '@/WorkflowExecuteAdditionalData';
import { MessageEventBus } from './MessageEventBus';
export async function recoverExecutionDataFromEventLogMessages(
executionId: string,
@@ -201,7 +201,7 @@ export async function recoverExecutionDataFromEventLogMessages(
await workflowExecutionCompleted(executionEntry.workflowData, iRunData);
// wait for UI to be back up and send the execution data
eventBus.once('editorUiConnected', function handleUiBackUp() {
Container.get(MessageEventBus).once('editorUiConnected', function handleUiBackUp() {
// add a small timeout to make sure the UI is back up
setTimeout(() => {
Container.get(Push).send('executionRecovered', {