fix(core): Remove threads pkg, rewrite log writer worker (#5134)
This commit is contained in:
committed by
GitHub
parent
b7faf4a0df
commit
e845eb33f9
@@ -1065,12 +1065,6 @@ export const schema = {
|
||||
env: 'N8N_EVENTBUS_CHECKUNSENTINTERVAL',
|
||||
},
|
||||
logWriter: {
|
||||
syncFileAccess: {
|
||||
doc: 'Whether all file access happens synchronously within the thread.',
|
||||
format: Boolean,
|
||||
default: false,
|
||||
env: 'N8N_EVENTBUS_LOGWRITER_SYNCFILEACCESS',
|
||||
},
|
||||
keepLogCount: {
|
||||
doc: 'How many event log files to keep.',
|
||||
format: Number,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { DateTime } from 'luxon';
|
||||
import type { EventMessageTypeNames, JsonObject } from 'n8n-workflow';
|
||||
import { v4 as uuid } from 'uuid';
|
||||
|
||||
@@ -44,7 +44,6 @@ export interface EventMessageAuditOptions extends AbstractEventMessageOptions {
|
||||
}
|
||||
|
||||
export class EventMessageAudit extends AbstractEventMessage {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
||||
readonly __type = EventMessageTypeNames.audit;
|
||||
|
||||
eventName: EventNamesAuditType;
|
||||
|
||||
@@ -66,7 +66,6 @@ class MessageEventBus extends EventEmitter {
|
||||
|
||||
LoggerProxy.debug('Initializing event bus...');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
const savedEventDestinations = await Db.collections.EventDestinations.find({});
|
||||
if (savedEventDestinations.length > 0) {
|
||||
for (const destinationData of savedEventDestinations) {
|
||||
@@ -91,11 +90,9 @@ class MessageEventBus extends EventEmitter {
|
||||
LoggerProxy.debug('Checking for unsent event messages');
|
||||
const unsentAndUnfinished = await this.getUnsentAndUnfinishedExecutions();
|
||||
LoggerProxy.debug(
|
||||
`Start logging into ${
|
||||
(await this.logWriter?.getThread()?.getLogFileName()) ?? 'unknown filename'
|
||||
} `,
|
||||
`Start logging into ${this.logWriter?.getLogFileName() ?? 'unknown filename'} `,
|
||||
);
|
||||
await this.logWriter?.startLogging();
|
||||
this.logWriter?.startLogging();
|
||||
await this.send(unsentAndUnfinished.unsentMessages);
|
||||
|
||||
if (unsentAndUnfinished.unfinishedExecutions.size > 0) {
|
||||
@@ -130,10 +127,8 @@ class MessageEventBus extends EventEmitter {
|
||||
if (id && Object.keys(this.destinations).includes(id)) {
|
||||
result = [this.destinations[id].serialize()];
|
||||
} else {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
result = Object.keys(this.destinations).map((e) => this.destinations[e].serialize());
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
return result.sort((a, b) => (a.__type ?? '').localeCompare(b.__type ?? ''));
|
||||
}
|
||||
|
||||
@@ -175,7 +170,11 @@ class MessageEventBus extends EventEmitter {
|
||||
msgs = [msgs];
|
||||
}
|
||||
for (const msg of msgs) {
|
||||
await 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' });
|
||||
}
|
||||
await this.emitMessage(msg);
|
||||
}
|
||||
}
|
||||
@@ -192,8 +191,8 @@ class MessageEventBus extends EventEmitter {
|
||||
return false;
|
||||
}
|
||||
|
||||
async confirmSent(msg: EventMessageTypes, source?: EventMessageConfirmSource) {
|
||||
await this.logWriter?.confirmMessageSent(msg.id, source);
|
||||
confirmSent(msg: EventMessageTypes, source?: EventMessageConfirmSource) {
|
||||
this.logWriter?.confirmMessageSent(msg.id, source);
|
||||
}
|
||||
|
||||
private hasAnyDestinationSubscribedToEvent(msg: EventMessageTypes): boolean {
|
||||
@@ -210,22 +209,23 @@ class MessageEventBus extends EventEmitter {
|
||||
// this is for internal use ONLY and not for use with custom destinations!
|
||||
this.emit('message', msg);
|
||||
|
||||
LoggerProxy.debug(`Listeners: ${this.eventNames().join(',')}`);
|
||||
// LoggerProxy.debug(`Listeners: ${this.eventNames().join(',')}`);
|
||||
|
||||
// if there are no set up destinations, immediately mark the event as sent
|
||||
if (
|
||||
!isLogStreamingEnabled() ||
|
||||
Object.keys(this.destinations).length === 0 ||
|
||||
!this.hasAnyDestinationSubscribedToEvent(msg)
|
||||
) {
|
||||
await this.confirmSent(msg, { id: '0', name: 'eventBus' });
|
||||
} else {
|
||||
if (this.shouldSendMsg(msg)) {
|
||||
for (const destinationName of Object.keys(this.destinations)) {
|
||||
this.emit(this.destinations[destinationName].getId(), msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
shouldSendMsg(msg: EventMessageTypes): boolean {
|
||||
return (
|
||||
isLogStreamingEnabled() &&
|
||||
Object.keys(this.destinations).length > 0 &&
|
||||
this.hasAnyDestinationSubscribedToEvent(msg)
|
||||
);
|
||||
}
|
||||
|
||||
async getEventsAll(): Promise<EventMessageTypes[]> {
|
||||
const queryResult = await this.logWriter?.getMessagesAll();
|
||||
const filtered = uniqby(queryResult, 'id');
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
/* eslint-disable import/no-cycle */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
import { MessageEventBusDestinationTypeNames } from 'n8n-workflow';
|
||||
import type { EventDestinations } from '@/databases/entities/MessageEventBusDestinationEntity';
|
||||
import type { MessageEventBusDestination } from './MessageEventBusDestination.ee';
|
||||
@@ -10,7 +9,6 @@ import { MessageEventBusDestinationWebhook } from './MessageEventBusDestinationW
|
||||
export function messageEventBusDestinationFromDb(
|
||||
dbData: EventDestinations,
|
||||
): MessageEventBusDestination | null {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-assignment
|
||||
const destinationData = dbData.destination;
|
||||
if ('__type' in destinationData) {
|
||||
switch (destinationData.__type) {
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||
import { v4 as uuid } from 'uuid';
|
||||
import {
|
||||
INodeCredentials,
|
||||
@@ -83,7 +81,6 @@ export abstract class MessageEventBusDestination implements MessageEventBusDesti
|
||||
id: this.getId(),
|
||||
destination: this.serialize(),
|
||||
};
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
|
||||
const dbResult: InsertResult = await Db.collections.EventDestinations.upsert(data, {
|
||||
skipUpdateIfNoValuesChanged: true,
|
||||
conflictPaths: ['id'],
|
||||
@@ -97,7 +94,6 @@ export abstract class MessageEventBusDestination implements MessageEventBusDesti
|
||||
}
|
||||
|
||||
static async deleteFromDb(id: string): Promise<DeleteResult> {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
||||
const dbResult = await Db.collections.EventDestinations.delete({ id });
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,6 @@ export class MessageEventBusDestinationSentry
|
||||
|
||||
constructor(options: MessageEventBusDestinationSentryOptions) {
|
||||
super(options);
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
this.label = options.label ?? 'Sentry DSN';
|
||||
this.__type = options.__type ?? MessageEventBusDestinationTypeNames.sentry;
|
||||
this.dsn = options.dsn;
|
||||
@@ -85,7 +84,7 @@ export class MessageEventBusDestinationSentry
|
||||
);
|
||||
|
||||
if (sentryResult) {
|
||||
await eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
sendResult = true;
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -109,7 +108,6 @@ export class MessageEventBusDestinationSentry
|
||||
): MessageEventBusDestinationSentry | null {
|
||||
if (
|
||||
'__type' in data &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
data.__type === MessageEventBusDestinationTypeNames.sentry &&
|
||||
isMessageEventBusDestinationSentryOptions(data)
|
||||
) {
|
||||
|
||||
@@ -96,7 +96,7 @@ export class MessageEventBusDestinationSyslog
|
||||
if (error) {
|
||||
console.log(error);
|
||||
} else {
|
||||
await eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
sendResult = true;
|
||||
}
|
||||
},
|
||||
@@ -112,7 +112,6 @@ export class MessageEventBusDestinationSyslog
|
||||
|
||||
serialize(): MessageEventBusDestinationSyslogOptions {
|
||||
const abstractSerialized = super.serialize();
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return {
|
||||
...abstractSerialized,
|
||||
expectedStatusCode: this.expectedStatusCode,
|
||||
|
||||
@@ -192,8 +192,6 @@ export class MessageEventBusDestinationWebhook
|
||||
} catch (_) {
|
||||
console.log('JSON parameter need to be an valid JSON');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
this.axiosRequestOptions.params = jsonParse(this.jsonQuery);
|
||||
}
|
||||
}
|
||||
@@ -212,8 +210,6 @@ export class MessageEventBusDestinationWebhook
|
||||
} catch (_) {
|
||||
console.log('JSON parameter need to be an valid JSON');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
this.axiosRequestOptions.headers = jsonParse(this.jsonHeaders);
|
||||
}
|
||||
}
|
||||
@@ -222,7 +218,6 @@ export class MessageEventBusDestinationWebhook
|
||||
if (this.axiosRequestOptions.headers === undefined) {
|
||||
this.axiosRequestOptions.headers = {};
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
this.axiosRequestOptions.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
@@ -336,10 +331,8 @@ export class MessageEventBusDestinationWebhook
|
||||
password: httpBasicAuth.password as string,
|
||||
};
|
||||
} else if (httpHeaderAuth) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
this.axiosRequestOptions.headers[httpHeaderAuth.name as string] = httpHeaderAuth.value;
|
||||
} else if (httpQueryAuth) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
this.axiosRequestOptions.params[httpQueryAuth.name as string] = httpQueryAuth.value;
|
||||
} else if (httpDigestAuth) {
|
||||
this.axiosRequestOptions.auth = {
|
||||
@@ -353,13 +346,13 @@ export class MessageEventBusDestinationWebhook
|
||||
if (requestResponse) {
|
||||
if (this.responseCodeMustMatch) {
|
||||
if (requestResponse.status === this.expectedStatusCode) {
|
||||
await eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
sendResult = true;
|
||||
} else {
|
||||
sendResult = false;
|
||||
}
|
||||
} else {
|
||||
await eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
eventBus.confirmSent(msg, { id: this.id, name: this.label });
|
||||
sendResult = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,9 +3,8 @@
|
||||
import { isEventMessageOptions } from '../EventMessageClasses/AbstractEventMessage';
|
||||
import { UserSettings } from 'n8n-core';
|
||||
import path, { parse } from 'path';
|
||||
import { ModuleThread, spawn, Thread, Worker } from 'threads';
|
||||
import { MessageEventBusLogWriterWorker } from './MessageEventBusLogWriterWorker';
|
||||
import { createReadStream, existsSync } from 'fs';
|
||||
import { Worker } from 'worker_threads';
|
||||
import { createReadStream, existsSync, rmSync } from 'fs';
|
||||
import readline from 'readline';
|
||||
import { jsonParse, LoggerProxy } from 'n8n-workflow';
|
||||
import remove from 'lodash.remove';
|
||||
@@ -19,15 +18,21 @@ import {
|
||||
isEventMessageConfirm,
|
||||
} from '../EventMessageClasses/EventMessageConfirm';
|
||||
import { once as eventOnce } from 'events';
|
||||
import { inTest } from '../../constants';
|
||||
|
||||
interface MessageEventBusLogWriterOptions {
|
||||
syncFileAccess?: boolean;
|
||||
interface MessageEventBusLogWriterConstructorOptions {
|
||||
logBaseName?: string;
|
||||
logBasePath?: string;
|
||||
keepLogCount?: number;
|
||||
keepNumberOfFiles?: number;
|
||||
maxFileSizeInKB?: number;
|
||||
}
|
||||
|
||||
export interface MessageEventBusLogWriterOptions {
|
||||
logFullBasePath: string;
|
||||
keepNumberOfFiles: number;
|
||||
maxFileSizeInKB: number;
|
||||
}
|
||||
|
||||
interface ReadMessagesFromLogFileResult {
|
||||
loggedMessages: EventMessageTypes[];
|
||||
sentMessages: EventMessageTypes[];
|
||||
@@ -42,7 +47,11 @@ export class MessageEventBusLogWriter {
|
||||
|
||||
static options: Required<MessageEventBusLogWriterOptions>;
|
||||
|
||||
private worker: ModuleThread<MessageEventBusLogWriterWorker> | null;
|
||||
private _worker: Worker | undefined;
|
||||
|
||||
public get worker(): Worker | undefined {
|
||||
return this._worker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates the Writer and the corresponding worker thread.
|
||||
@@ -51,16 +60,17 @@ export class MessageEventBusLogWriter {
|
||||
* **Note** that starting to log will archive existing logs, so handle unsent events first before calling startLogging()
|
||||
*/
|
||||
static async getInstance(
|
||||
options?: MessageEventBusLogWriterOptions,
|
||||
options?: MessageEventBusLogWriterConstructorOptions,
|
||||
): Promise<MessageEventBusLogWriter> {
|
||||
if (!MessageEventBusLogWriter.instance) {
|
||||
MessageEventBusLogWriter.instance = new MessageEventBusLogWriter();
|
||||
MessageEventBusLogWriter.options = {
|
||||
logBaseName: options?.logBaseName ?? config.getEnv('eventBus.logWriter.logBaseName'),
|
||||
logBasePath: options?.logBasePath ?? UserSettings.getUserN8nFolderPath(),
|
||||
syncFileAccess:
|
||||
options?.syncFileAccess ?? config.getEnv('eventBus.logWriter.syncFileAccess'),
|
||||
keepLogCount: options?.keepLogCount ?? config.getEnv('eventBus.logWriter.keepLogCount'),
|
||||
logFullBasePath: path.join(
|
||||
options?.logBasePath ?? UserSettings.getUserN8nFolderPath(),
|
||||
options?.logBaseName ?? config.getEnv('eventBus.logWriter.logBaseName'),
|
||||
),
|
||||
keepNumberOfFiles:
|
||||
options?.keepNumberOfFiles ?? config.getEnv('eventBus.logWriter.keepLogCount'),
|
||||
maxFileSizeInKB:
|
||||
options?.maxFileSizeInKB ?? config.getEnv('eventBus.logWriter.maxFileSizeInKB'),
|
||||
};
|
||||
@@ -73,15 +83,19 @@ export class MessageEventBusLogWriter {
|
||||
* First archives existing log files one history level upwards,
|
||||
* then starts logging events into a fresh event log
|
||||
*/
|
||||
async startLogging() {
|
||||
await MessageEventBusLogWriter.instance.getThread()?.startLogging();
|
||||
startLogging() {
|
||||
if (this.worker) {
|
||||
this.worker.postMessage({ command: 'startLogging', data: {} });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pauses all logging. Events are still received by the worker, they just are not logged any more
|
||||
*/
|
||||
async pauseLogging() {
|
||||
await MessageEventBusLogWriter.instance.getThread()?.pauseLogging();
|
||||
if (this.worker) {
|
||||
this.worker.postMessage({ command: 'pauseLogging', data: {} });
|
||||
}
|
||||
}
|
||||
|
||||
private async startThread() {
|
||||
@@ -89,26 +103,23 @@ export class MessageEventBusLogWriter {
|
||||
await this.close();
|
||||
}
|
||||
await MessageEventBusLogWriter.instance.spawnThread();
|
||||
await MessageEventBusLogWriter.instance
|
||||
.getThread()
|
||||
?.initialize(
|
||||
path.join(
|
||||
MessageEventBusLogWriter.options.logBasePath,
|
||||
MessageEventBusLogWriter.options.logBaseName,
|
||||
),
|
||||
MessageEventBusLogWriter.options.syncFileAccess,
|
||||
MessageEventBusLogWriter.options.keepLogCount,
|
||||
MessageEventBusLogWriter.options.maxFileSizeInKB,
|
||||
);
|
||||
if (this.worker) {
|
||||
this.worker.postMessage({ command: 'initialize', data: MessageEventBusLogWriter.options });
|
||||
}
|
||||
}
|
||||
|
||||
private async spawnThread(): Promise<boolean> {
|
||||
this.worker = await spawn<MessageEventBusLogWriterWorker>(
|
||||
new Worker(`${parse(__filename).name}Worker`),
|
||||
);
|
||||
const parsedName = parse(__filename);
|
||||
let workerFileName;
|
||||
if (inTest) {
|
||||
workerFileName = './dist/eventbus/MessageEventBusWriter/MessageEventBusLogWriterWorker.js';
|
||||
} else {
|
||||
workerFileName = path.join(parsedName.dir, `${parsedName.name}Worker${parsedName.ext}`);
|
||||
}
|
||||
this._worker = new Worker(workerFileName);
|
||||
if (this.worker) {
|
||||
Thread.errors(this.worker).subscribe(async (error) => {
|
||||
LoggerProxy.error('Event Bus Log Writer thread error', error);
|
||||
this.worker.on('messageerror', async (error) => {
|
||||
LoggerProxy.error('Event Bus Log Writer thread error, attempting to restart...', error);
|
||||
await MessageEventBusLogWriter.instance.startThread();
|
||||
});
|
||||
return true;
|
||||
@@ -116,29 +127,25 @@ export class MessageEventBusLogWriter {
|
||||
return false;
|
||||
}
|
||||
|
||||
getThread(): ModuleThread<MessageEventBusLogWriterWorker> | undefined {
|
||||
if (this.worker) {
|
||||
return this.worker;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.worker) {
|
||||
await Thread.terminate(this.worker);
|
||||
this.worker = null;
|
||||
await this.worker.terminate();
|
||||
this._worker = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
async putMessage(msg: EventMessageTypes): Promise<void> {
|
||||
putMessage(msg: EventMessageTypes): void {
|
||||
if (this.worker) {
|
||||
await this.worker.appendMessageToLog(msg.serialize());
|
||||
this.worker.postMessage({ command: 'appendMessageToLog', data: msg.serialize() });
|
||||
}
|
||||
}
|
||||
|
||||
async confirmMessageSent(msgId: string, source?: EventMessageConfirmSource): Promise<void> {
|
||||
confirmMessageSent(msgId: string, source?: EventMessageConfirmSource): void {
|
||||
if (this.worker) {
|
||||
await this.worker.confirmMessageSent(new EventMessageConfirm(msgId, source).serialize());
|
||||
this.worker.postMessage({
|
||||
command: 'confirmMessageSent',
|
||||
data: new EventMessageConfirm(msgId, source).serialize(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,7 +162,7 @@ export class MessageEventBusLogWriter {
|
||||
? Math.min(config.get('eventBus.logWriter.keepLogCount') as number, logHistory)
|
||||
: (config.get('eventBus.logWriter.keepLogCount') as number);
|
||||
for (let i = logCount; i >= 0; i--) {
|
||||
const logFileName = await MessageEventBusLogWriter.instance.getThread()?.getLogFileName(i);
|
||||
const logFileName = this.getLogFileName(i);
|
||||
if (logFileName) {
|
||||
await this.readLoggedMessagesFromFile(results, mode, logFileName);
|
||||
}
|
||||
@@ -212,6 +219,22 @@ export class MessageEventBusLogWriter {
|
||||
return results;
|
||||
}
|
||||
|
||||
getLogFileName(counter?: number): string {
|
||||
if (counter) {
|
||||
return `${MessageEventBusLogWriter.options.logFullBasePath}-${counter}.log`;
|
||||
} else {
|
||||
return `${MessageEventBusLogWriter.options.logFullBasePath}.log`;
|
||||
}
|
||||
}
|
||||
|
||||
cleanAllLogs() {
|
||||
for (let i = 0; i <= MessageEventBusLogWriter.options.keepNumberOfFiles; i++) {
|
||||
if (existsSync(this.getLogFileName(i))) {
|
||||
rmSync(this.getLogFileName(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getMessagesByExecutionId(
|
||||
executionId: string,
|
||||
logHistory?: number,
|
||||
@@ -221,7 +244,7 @@ export class MessageEventBusLogWriter {
|
||||
? Math.min(config.get('eventBus.logWriter.keepLogCount') as number, logHistory)
|
||||
: (config.get('eventBus.logWriter.keepLogCount') as number);
|
||||
for (let i = 0; i < logCount; i++) {
|
||||
const logFileName = await MessageEventBusLogWriter.instance.getThread()?.getLogFileName(i);
|
||||
const logFileName = this.getLogFileName(i);
|
||||
if (logFileName) {
|
||||
result.push(...(await this.readFromFileByExecutionId(executionId, logFileName)));
|
||||
}
|
||||
|
||||
@@ -1,17 +1,11 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
import { appendFileSync, existsSync, rmSync, renameSync, openSync, closeSync } from 'fs';
|
||||
import { appendFile, stat } from 'fs/promises';
|
||||
import { expose, isWorkerRuntime } from 'threads/worker';
|
||||
|
||||
// -----------------------------------------
|
||||
// * This part runs in the Worker Thread ! *
|
||||
// -----------------------------------------
|
||||
|
||||
// all references to and imports from classes have been remove to keep memory usage low
|
||||
import { stat } from 'fs/promises';
|
||||
import { isMainThread, parentPort } from 'worker_threads';
|
||||
import type { MessageEventBusLogWriterOptions } from './MessageEventBusLogWriter';
|
||||
|
||||
let logFileBasePath = '';
|
||||
let loggingPaused = true;
|
||||
let syncFileAccess = false;
|
||||
let keepFiles = 10;
|
||||
let fileStatTimer: NodeJS.Timer;
|
||||
let maxLogFileSizeInKB = 102400;
|
||||
@@ -20,12 +14,8 @@ function setLogFileBasePath(basePath: string) {
|
||||
logFileBasePath = basePath;
|
||||
}
|
||||
|
||||
function setUseSyncFileAccess(useSync: boolean) {
|
||||
syncFileAccess = useSync;
|
||||
}
|
||||
|
||||
function setMaxLogFileSizeInKB(maxSizeInKB: number) {
|
||||
maxLogFileSizeInKB = maxSizeInKB;
|
||||
function setMaxLogFileSizeInKB(maxFileSizeInKB: number) {
|
||||
maxLogFileSizeInKB = maxFileSizeInKB;
|
||||
}
|
||||
|
||||
function setKeepFiles(keepNumberOfFiles: number) {
|
||||
@@ -81,65 +71,53 @@ function appendMessageSync(msg: any) {
|
||||
appendFileSync(buildLogFileNameWithCounter(), JSON.stringify(msg) + '\n');
|
||||
}
|
||||
|
||||
async function appendMessage(msg: any) {
|
||||
if (loggingPaused) {
|
||||
return;
|
||||
}
|
||||
await appendFile(buildLogFileNameWithCounter(), JSON.stringify(msg) + '\n');
|
||||
if (!isMainThread) {
|
||||
// -----------------------------------------
|
||||
// * This part runs in the Worker Thread ! *
|
||||
// -----------------------------------------
|
||||
parentPort?.on('message', async (msg: { command: string; data: any }) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const { command, data } = msg;
|
||||
try {
|
||||
switch (command) {
|
||||
case 'appendMessageToLog':
|
||||
case 'confirmMessageSent':
|
||||
appendMessageSync(data);
|
||||
parentPort?.postMessage({ command, data: true });
|
||||
break;
|
||||
case 'pauseLogging':
|
||||
loggingPaused = true;
|
||||
clearInterval(fileStatTimer);
|
||||
break;
|
||||
case 'initialize':
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const settings: MessageEventBusLogWriterOptions = {
|
||||
logFullBasePath: (data as MessageEventBusLogWriterOptions).logFullBasePath ?? '',
|
||||
keepNumberOfFiles: (data as MessageEventBusLogWriterOptions).keepNumberOfFiles ?? 10,
|
||||
maxFileSizeInKB: (data as MessageEventBusLogWriterOptions).maxFileSizeInKB ?? 102400,
|
||||
};
|
||||
setLogFileBasePath(settings.logFullBasePath);
|
||||
setKeepFiles(settings.keepNumberOfFiles);
|
||||
setMaxLogFileSizeInKB(settings.maxFileSizeInKB);
|
||||
break;
|
||||
case 'startLogging':
|
||||
if (logFileBasePath) {
|
||||
renameAndCreateLogs();
|
||||
loggingPaused = false;
|
||||
fileStatTimer = setInterval(async () => {
|
||||
await checkFileSize(buildLogFileNameWithCounter());
|
||||
}, 5000);
|
||||
}
|
||||
break;
|
||||
case 'cleanLogs':
|
||||
cleanAllLogs();
|
||||
parentPort?.postMessage('cleanedAllLogs');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} catch (error) {
|
||||
parentPort?.postMessage(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const messageEventBusLogWriterWorker = {
|
||||
async appendMessageToLog(msg: any) {
|
||||
if (syncFileAccess) {
|
||||
appendMessageSync(msg);
|
||||
} else {
|
||||
await appendMessage(msg);
|
||||
}
|
||||
},
|
||||
async confirmMessageSent(confirm: unknown) {
|
||||
if (syncFileAccess) {
|
||||
appendMessageSync(confirm);
|
||||
} else {
|
||||
await appendMessage(confirm);
|
||||
}
|
||||
},
|
||||
pauseLogging() {
|
||||
loggingPaused = true;
|
||||
clearInterval(fileStatTimer);
|
||||
},
|
||||
initialize(
|
||||
basePath: string,
|
||||
useSyncFileAccess = false,
|
||||
keepNumberOfFiles = 10,
|
||||
maxSizeInKB = 102400,
|
||||
) {
|
||||
setLogFileBasePath(basePath);
|
||||
setUseSyncFileAccess(useSyncFileAccess);
|
||||
setKeepFiles(keepNumberOfFiles);
|
||||
setMaxLogFileSizeInKB(maxSizeInKB);
|
||||
},
|
||||
startLogging() {
|
||||
if (logFileBasePath) {
|
||||
renameAndCreateLogs();
|
||||
loggingPaused = false;
|
||||
fileStatTimer = setInterval(async () => {
|
||||
await checkFileSize(buildLogFileNameWithCounter());
|
||||
}, 5000);
|
||||
}
|
||||
},
|
||||
getLogFileName(counter?: number) {
|
||||
if (logFileBasePath) {
|
||||
return buildLogFileNameWithCounter(counter);
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
cleanLogs() {
|
||||
cleanAllLogs();
|
||||
},
|
||||
};
|
||||
if (isWorkerRuntime()) {
|
||||
// Register the serializer on the worker thread
|
||||
expose(messageEventBusLogWriterWorker);
|
||||
}
|
||||
export type MessageEventBusLogWriterWorker = typeof messageEventBusLogWriterWorker;
|
||||
|
||||
@@ -55,17 +55,14 @@ const isWithQueryString = (candidate: unknown): candidate is { query: string } =
|
||||
const isMessageEventBusDestinationWebhookOptions = (
|
||||
candidate: unknown,
|
||||
): candidate is MessageEventBusDestinationWebhookOptions => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const o = candidate as MessageEventBusDestinationWebhookOptions;
|
||||
if (!o) return false;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
return o.url !== undefined;
|
||||
};
|
||||
|
||||
const isMessageEventBusDestinationOptions = (
|
||||
candidate: unknown,
|
||||
): candidate is MessageEventBusDestinationOptions => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const o = candidate as MessageEventBusDestinationOptions;
|
||||
if (!o) return false;
|
||||
return o.__type !== undefined;
|
||||
@@ -138,23 +135,20 @@ eventBusRouter.post(
|
||||
|
||||
eventBusRouter.get(
|
||||
'/destination',
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<any> => {
|
||||
ResponseHelper.send(async (req: express.Request): Promise<any> => {
|
||||
let result = [];
|
||||
if (isWithIdString(req.query)) {
|
||||
result = await eventBus.findDestination(req.query.id);
|
||||
} else {
|
||||
result = await eventBus.findDestination();
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return result;
|
||||
}),
|
||||
);
|
||||
|
||||
eventBusRouter.post(
|
||||
'/destination',
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<any> => {
|
||||
ResponseHelper.send(async (req: express.Request): Promise<any> => {
|
||||
if (!req.user || (req.user as User).globalRole.name !== 'owner') {
|
||||
throw new ResponseHelper.UnauthorizedError('Invalid request');
|
||||
}
|
||||
@@ -195,8 +189,7 @@ eventBusRouter.post(
|
||||
|
||||
eventBusRouter.get(
|
||||
'/testmessage',
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<any> => {
|
||||
ResponseHelper.send(async (req: express.Request): Promise<any> => {
|
||||
let result = false;
|
||||
if (isWithIdString(req.query)) {
|
||||
result = await eventBus.testDestination(req.query.id);
|
||||
@@ -207,8 +200,7 @@ eventBusRouter.get(
|
||||
|
||||
eventBusRouter.delete(
|
||||
'/destination',
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
ResponseHelper.send(async (req: express.Request, res: express.Response): Promise<any> => {
|
||||
ResponseHelper.send(async (req: express.Request): Promise<any> => {
|
||||
if (!req.user || (req.user as User).globalRole.name !== 'owner') {
|
||||
throw new ResponseHelper.UnauthorizedError('Invalid request');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user