fix(core): Remove threads pkg, rewrite log writer worker (#5134)

This commit is contained in:
Michael Auerswald
2023-01-13 15:39:25 +01:00
committed by GitHub
parent b7faf4a0df
commit e845eb33f9
15 changed files with 287 additions and 286 deletions

View File

@@ -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');