refactor(core): Make Logger a service (no-changelog) (#7494)
This commit is contained in:
committed by
GitHub
parent
db4e61ba24
commit
05586a900d
@@ -1,4 +1,4 @@
|
||||
import { LoggerProxy, jsonParse } from 'n8n-workflow';
|
||||
import { jsonParse } from 'n8n-workflow';
|
||||
import type { MessageEventBusDestinationOptions } from 'n8n-workflow';
|
||||
import type { DeleteResult } from 'typeorm';
|
||||
import { In } from 'typeorm';
|
||||
@@ -28,11 +28,12 @@ import {
|
||||
} from '../EventMessageClasses/EventMessageGeneric';
|
||||
import { recoverExecutionDataFromEventLogMessages } from './recoverEvents';
|
||||
import { METRICS_EVENT_NAME } from '../MessageEventBusDestination/Helpers.ee';
|
||||
import Container, { Service } from 'typedi';
|
||||
import { Container, Service } from 'typedi';
|
||||
import { ExecutionRepository, WorkflowRepository } from '@/databases/repositories';
|
||||
import type { AbstractEventMessageOptions } from '../EventMessageClasses/AbstractEventMessageOptions';
|
||||
import { getEventMessageObjectByType } from '../EventMessageClasses/Helpers';
|
||||
import { OrchestrationMainService } from '@/services/orchestration/main/orchestration.main.service';
|
||||
import { Logger } from '@/Logger';
|
||||
|
||||
export type EventMessageReturnMode = 'sent' | 'unsent' | 'all' | 'unfinished';
|
||||
|
||||
@@ -48,8 +49,6 @@ export interface MessageEventBusInitializeOptions {
|
||||
|
||||
@Service()
|
||||
export class MessageEventBus extends EventEmitter {
|
||||
private static instance: MessageEventBus;
|
||||
|
||||
isInitialized: boolean;
|
||||
|
||||
logWriter: MessageEventBusLogWriter;
|
||||
@@ -60,18 +59,11 @@ export class MessageEventBus extends EventEmitter {
|
||||
|
||||
private pushIntervalTimer: NodeJS.Timer;
|
||||
|
||||
constructor() {
|
||||
constructor(private readonly logger: Logger) {
|
||||
super();
|
||||
this.isInitialized = false;
|
||||
}
|
||||
|
||||
static getInstance(): MessageEventBus {
|
||||
if (!MessageEventBus.instance) {
|
||||
MessageEventBus.instance = new MessageEventBus();
|
||||
}
|
||||
return MessageEventBus.instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Needs to be called once at startup to set the event bus instance up. Will launch the event log writer and,
|
||||
* if configured to do so, the previously stored event destinations.
|
||||
@@ -85,7 +77,7 @@ export class MessageEventBus extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
|
||||
LoggerProxy.debug('Initializing event bus...');
|
||||
this.logger.debug('Initializing event bus...');
|
||||
|
||||
const savedEventDestinations = await Db.collections.EventDestinations.find({});
|
||||
if (savedEventDestinations.length > 0) {
|
||||
@@ -97,12 +89,12 @@ export class MessageEventBus extends EventEmitter {
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
if (error.message) LoggerProxy.debug(error.message as string);
|
||||
if (error.message) this.logger.debug(error.message as string);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LoggerProxy.debug('Initializing event writer');
|
||||
this.logger.debug('Initializing event writer');
|
||||
if (options?.workerId) {
|
||||
// only add 'worker' to log file name since the ID changes on every start and we
|
||||
// would not be able to recover the log files from the previous run not knowing it
|
||||
@@ -115,19 +107,19 @@ export class MessageEventBus extends EventEmitter {
|
||||
}
|
||||
|
||||
if (!this.logWriter) {
|
||||
LoggerProxy.warn('Could not initialize event writer');
|
||||
this.logger.warn('Could not initialize event writer');
|
||||
}
|
||||
|
||||
if (options?.skipRecoveryPass) {
|
||||
LoggerProxy.debug('Skipping unsent event check');
|
||||
this.logger.debug('Skipping unsent event check');
|
||||
} else {
|
||||
// unsent event check:
|
||||
// - find unsent messages in current event log(s)
|
||||
// - cycle event logs and start the logging to a fresh file
|
||||
// - retry sending events
|
||||
LoggerProxy.debug('Checking for unsent event messages');
|
||||
this.logger.debug('Checking for unsent event messages');
|
||||
const unsentAndUnfinished = await this.getUnsentAndUnfinishedExecutions();
|
||||
LoggerProxy.debug(
|
||||
this.logger.debug(
|
||||
`Start logging into ${this.logWriter?.getLogFileName() ?? 'unknown filename'} `,
|
||||
);
|
||||
this.logWriter?.startLogging();
|
||||
@@ -152,16 +144,16 @@ export class MessageEventBus extends EventEmitter {
|
||||
}
|
||||
|
||||
if (unfinishedExecutionIds.length > 0) {
|
||||
LoggerProxy.warn(`Found unfinished executions: ${unfinishedExecutionIds.join(', ')}`);
|
||||
LoggerProxy.info('This could be due to a crash of an active workflow or a restart of n8n.');
|
||||
this.logger.warn(`Found unfinished executions: ${unfinishedExecutionIds.join(', ')}`);
|
||||
this.logger.info('This could be due to a crash of an active workflow or a restart of n8n.');
|
||||
const activeWorkflows = await Container.get(WorkflowRepository).find({
|
||||
where: { active: true },
|
||||
select: ['id', 'name'],
|
||||
});
|
||||
if (activeWorkflows.length > 0) {
|
||||
LoggerProxy.info('Currently active workflows:');
|
||||
this.logger.info('Currently active workflows:');
|
||||
for (const workflowData of activeWorkflows) {
|
||||
LoggerProxy.info(` - ${workflowData.name} (ID: ${workflowData.id})`);
|
||||
this.logger.info(` - ${workflowData.name} (ID: ${workflowData.id})`);
|
||||
}
|
||||
}
|
||||
const recoveryAlreadyAttempted = this.logWriter?.isRecoveryProcessRunning();
|
||||
@@ -171,14 +163,14 @@ export class MessageEventBus extends EventEmitter {
|
||||
// a possible reason would be that recreating the workflow data itself caused e.g an OOM error
|
||||
// in that case, we do not want to retry the recovery process, but rather mark the executions as crashed
|
||||
if (recoveryAlreadyAttempted)
|
||||
LoggerProxy.warn('Skipped recovery process since it previously failed.');
|
||||
this.logger.warn('Skipped recovery process since it previously failed.');
|
||||
} else {
|
||||
// start actual recovery process and write recovery process flag file
|
||||
this.logWriter?.startRecoveryProcess();
|
||||
for (const executionId of unfinishedExecutionIds) {
|
||||
LoggerProxy.warn(`Attempting to recover execution ${executionId}`);
|
||||
this.logger.warn(`Attempting to recover execution ${executionId}`);
|
||||
if (!unsentAndUnfinished.unfinishedExecutions[executionId]?.length) {
|
||||
LoggerProxy.debug(
|
||||
this.logger.debug(
|
||||
`No event messages found, marking execution ${executionId} as 'crashed'`,
|
||||
);
|
||||
await Container.get(ExecutionRepository).markAsCrashed([executionId]);
|
||||
@@ -205,7 +197,7 @@ export class MessageEventBus extends EventEmitter {
|
||||
}, config.getEnv('eventBus.checkUnsentInterval'));
|
||||
}
|
||||
|
||||
LoggerProxy.debug('MessageEventBus initialized');
|
||||
this.logger.debug('MessageEventBus initialized');
|
||||
this.isInitialized = true;
|
||||
}
|
||||
|
||||
@@ -263,25 +255,25 @@ export class MessageEventBus extends EventEmitter {
|
||||
private async trySendingUnsent(msgs?: EventMessageTypes[]) {
|
||||
const unsentMessages = msgs ?? (await this.getEventsUnsent());
|
||||
if (unsentMessages.length > 0) {
|
||||
LoggerProxy.debug(`Found unsent event messages: ${unsentMessages.length}`);
|
||||
this.logger.debug(`Found unsent event messages: ${unsentMessages.length}`);
|
||||
for (const unsentMsg of unsentMessages) {
|
||||
LoggerProxy.debug(`Retrying: ${unsentMsg.id} ${unsentMsg.__type}`);
|
||||
this.logger.debug(`Retrying: ${unsentMsg.id} ${unsentMsg.__type}`);
|
||||
await this.emitMessage(unsentMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async close() {
|
||||
LoggerProxy.debug('Shutting down event writer...');
|
||||
this.logger.debug('Shutting down event writer...');
|
||||
await this.logWriter?.close();
|
||||
for (const destinationName of Object.keys(this.destinations)) {
|
||||
LoggerProxy.debug(
|
||||
this.logger.debug(
|
||||
`Shutting down event destination ${this.destinations[destinationName].getId()}...`,
|
||||
);
|
||||
await this.destinations[destinationName].close();
|
||||
}
|
||||
this.isInitialized = false;
|
||||
LoggerProxy.debug('EventBus shut down.');
|
||||
this.logger.debug('EventBus shut down.');
|
||||
}
|
||||
|
||||
async restart() {
|
||||
|
||||
Reference in New Issue
Block a user