diff --git a/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs new file mode 100644 index 00000000..4bca1240 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs @@ -0,0 +1,12 @@ +namespace BotSharp.Abstraction.Crontab.Settings; + +public class CrontabSettings +{ + public CrontabBaseSetting EventSubscriber { get; set; } = new(); + public CrontabBaseSetting Watcher { get; set; } = new(); +} + +public class CrontabBaseSetting +{ + public bool Enabled { get; set; } = true; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/Session/ChatSessionOptions.cs b/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/Session/ChatSessionOptions.cs index 3bcba8ba..f4318fbb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/Session/ChatSessionOptions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Realtime/Models/Session/ChatSessionOptions.cs @@ -1,9 +1,12 @@ +using Microsoft.Extensions.Logging; using System.Text.Json; namespace BotSharp.Abstraction.Realtime.Models.Session; public class ChatSessionOptions { + public string Provider { get; set; } public int? BufferSize { get; set; } public JsonSerializerOptions? JsonOptions { get; set; } + public ILogger? Logger { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Settings/BotSharpDatabaseSettings.cs similarity index 95% rename from src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs rename to src/Infrastructure/BotSharp.Abstraction/Repositories/Settings/BotSharpDatabaseSettings.cs index 19f5b141..76fdf455 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Settings/BotSharpDatabaseSettings.cs @@ -1,4 +1,4 @@ -namespace BotSharp.Abstraction.Repositories; +namespace BotSharp.Abstraction.Repositories.Settings; public class BotSharpDatabaseSettings : DatabaseBasicSettings { diff --git a/src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs b/src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs index feef2546..51701968 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/CrontabPlugin.cs @@ -32,11 +32,22 @@ public class CrontabPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { + var settings = new CrontabSettings(); + config.Bind("Crontab", settings); + services.AddSingleton(settings); + services.AddScoped(); services.AddScoped(); services.AddScoped(); - services.AddHostedService(); - services.AddHostedService(); + if (settings.Watcher?.Enabled == true) + { + services.AddHostedService(); + } + + if (settings.EventSubscriber?.Enabled == true) + { + services.AddHostedService(); + } } } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Using.cs b/src/Infrastructure/BotSharp.Core.Crontab/Using.cs index f1b7a838..e1c1bca5 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Using.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Using.cs @@ -6,6 +6,7 @@ global using Microsoft.Extensions.DependencyInjection; global using BotSharp.Abstraction.Agents.Enums; global using BotSharp.Abstraction.Crontab; global using BotSharp.Abstraction.Crontab.Models; +global using BotSharp.Abstraction.Crontab.Settings; global using BotSharp.Abstraction.Agents; global using BotSharp.Abstraction.Plugins; global using BotSharp.Abstraction.Conversations.Models; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 5becbec6..fb4d49c4 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Repositories.Settings; using BotSharp.Abstraction.Tasks.Models; using BotSharp.Abstraction.Users.Enums; using System.IO; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs index 452f54c9..25f7d2d5 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.RefreshAgents.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Repositories.Enums; +using BotSharp.Abstraction.Repositories.Settings; using System.IO; namespace BotSharp.Core.Agents.Services; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs index 8365639b..f2fb4ff7 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Repositories.Enums; +using BotSharp.Abstraction.Repositories.Settings; using BotSharp.Abstraction.Users.Enums; using BotSharp.Abstraction.Users.Models; using System.IO; diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs index c54c4fd8..ca025447 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Repositories.Settings; using System.IO; using System.Reflection; diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 19b6df53..5c00de0d 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -16,6 +16,7 @@ using BotSharp.Abstraction.Templating; using BotSharp.Core.Templating; using BotSharp.Abstraction.Infrastructures.Enums; using BotSharp.Abstraction.Realtime; +using BotSharp.Abstraction.Repositories.Settings; namespace BotSharp.Core; @@ -71,17 +72,6 @@ public static class BotSharpCoreExtensions return services; } - //public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config) - //{ - // services.AddScoped(sp => - // { - // var myDatabaseSettings = sp.GetRequiredService(); - // return new FileRepository(myDatabaseSettings, sp); - // }); - - // return services; - //} - public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app) { if (app == null) diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.cs index ea2a68ac..6833ffa2 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Repositories.Settings; using System.IO; namespace BotSharp.Core.Files.Services; diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs index a040525f..30a967e8 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs @@ -24,7 +24,9 @@ public class DistributedLocker : IDistributedLocker var redis = _services.GetService(); if (redis == null) { +#if !DEBUG _logger.LogInformation($"The Redis server is experiencing issues and is not functioning as expected."); +#endif await action(); return true; } @@ -50,7 +52,9 @@ public class DistributedLocker : IDistributedLocker var redis = _services.GetRequiredService(); if (redis == null) { +#if !DEBUG _logger.LogWarning($"The Redis server is experiencing issues and is not functioning as expected."); +#endif action(); return false; } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataCollectionResult.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataCollectionResult.cs index 4f16397e..7364401a 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataCollectionResult.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataCollectionResult.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Realtime.Models.Session; using System.ClientModel; using System.Net.WebSockets; @@ -7,16 +6,16 @@ namespace BotSharp.Core.Infrastructures.Websocket; internal class AsyncWebsocketDataCollectionResult : AsyncCollectionResult { private readonly WebSocket _webSocket; - private readonly ChatSessionOptions? _sessionOptions; + private readonly ChatSessionOptions? _options; private readonly CancellationToken _cancellationToken; public AsyncWebsocketDataCollectionResult( WebSocket webSocket, - ChatSessionOptions? sessionOptions, + ChatSessionOptions? options, CancellationToken cancellationToken) { _webSocket = webSocket; - _sessionOptions = sessionOptions; + _options = options; _cancellationToken = cancellationToken; } @@ -27,7 +26,7 @@ internal class AsyncWebsocketDataCollectionResult : AsyncCollectionResult GetRawPagesAsync() { - await using var enumerator = new AsyncWebsocketDataResultEnumerator(_webSocket, _sessionOptions, _cancellationToken); + await using var enumerator = new AsyncWebsocketDataResultEnumerator(_webSocket, _options, _cancellationToken); while (await enumerator.MoveNextAsync().ConfigureAwait(false)) { yield return enumerator.Current; diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs index f89127e2..59eac990 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Websocket/AsyncWebsocketDataResultEnumerator.cs @@ -1,3 +1,4 @@ +using Microsoft.AspNetCore.Builder; using System.Buffers; using System.ClientModel; using System.Net.WebSockets; @@ -7,7 +8,7 @@ namespace BotSharp.Core.Infrastructures.Websocket; internal class AsyncWebsocketDataResultEnumerator : IAsyncEnumerator { private readonly WebSocket _webSocket; - private readonly ChatSessionOptions? _sessionOptions; + private readonly ChatSessionOptions? _options; private readonly CancellationToken _cancellationToken; private readonly byte[] _buffer; @@ -15,13 +16,13 @@ internal class AsyncWebsocketDataResultEnumerator : IAsyncEnumerator 0 ? sessionOptions.BufferSize.Value : DEFAULT_BUFFER_SIZE; + var bufferSize = options?.BufferSize > 0 ? options.BufferSize.Value : DEFAULT_BUFFER_SIZE; _buffer = ArrayPool.Shared.Rent(bufferSize); } @@ -44,7 +45,10 @@ internal class AsyncWebsocketDataResultEnumerator : IAsyncEnumerator