diff --git a/app/MindWork AI Studio/Program.cs b/app/MindWork AI Studio/Program.cs index 85667ac9..072dd3ad 100644 --- a/app/MindWork AI Studio/Program.cs +++ b/app/MindWork AI Studio/Program.cs @@ -160,6 +160,7 @@ internal sealed class Program // Get the logging factory for e.g., static classes: LOGGER_FACTORY = app.Services.GetRequiredService(); + MessageBus.INSTANCE.Initialize(LOGGER_FACTORY.CreateLogger()); // Get a program logger: var programLogger = app.Services.GetRequiredService>(); diff --git a/app/MindWork AI Studio/Tools/MessageBus.cs b/app/MindWork AI Studio/Tools/MessageBus.cs index e37e54fc..6f27da87 100644 --- a/app/MindWork AI Studio/Tools/MessageBus.cs +++ b/app/MindWork AI Studio/Tools/MessageBus.cs @@ -15,9 +15,17 @@ public sealed class MessageBus private readonly ConcurrentQueue messageQueue = new(); private readonly SemaphoreSlim sendingSemaphore = new(1, 1); + private static ILogger? LOG; + private MessageBus() { } + + public void Initialize(ILogger logger) + { + LOG = logger; + LOG.LogInformation("Message bus initialized."); + } /// /// Define for which components and events you want to receive messages. @@ -48,7 +56,7 @@ public sealed class MessageBus public async Task SendMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data = default) { this.messageQueue.Enqueue(new Message(sendingComponent, triggeredEvent, data)); - + try { await this.sendingSemaphore.WaitAsync(); @@ -61,11 +69,16 @@ public sealed class MessageBus var eventFilter = this.componentEvents[receiver]; if (eventFilter.Length == 0 || eventFilter.Contains(triggeredEvent)) + // We don't await the task here because we don't want to block the message bus: _ = receiver.ProcessMessage(message.SendingComponent, message.TriggeredEvent, message.Data); } } } + catch (Exception e) + { + LOG?.LogError(e, "Error while sending message."); + } finally { this.sendingSemaphore.Release();