From 68ff293965d6982c14a2d6e22b6c7b8ae45d9a9f Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 10 Jul 2024 23:33:58 -0500 Subject: [PATCH 1/2] NewPage --- .../Drivers/PlaywrightDriver/PlaywrightInstance.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs index a0fae6f3b..cb0ed015c 100644 --- a/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs +++ b/src/Plugins/BotSharp.Plugin.WebDriver/Drivers/PlaywrightDriver/PlaywrightInstance.cs @@ -115,15 +115,22 @@ public async Task NewPage(string ctxId, DataFetched? fetched) JsonElement? json = null; try { - json = await e.JsonAsync(); + if (e.Status == 200 && e.Ok) + { + json = await e.JsonAsync(); + } + else + { + Serilog.Log.Warning($"Response status: {e.Status} {e.StatusText}, OK: {e.Ok}"); + } } - catch(Exception ex) + catch (Exception ex) { Serilog.Log.Error(ex.ToString()); } finally { - fetched(e.Url.ToLower(), JsonSerializer.Serialize(json ?? new JsonElement())); + fetched(e.Url.ToLower(), JsonSerializer.Serialize(json)); } } }; From 5aa37e685873f79f9be09478a1177faee77b6b40 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 15 Jul 2024 16:38:47 -0500 Subject: [PATCH 2/2] Replace DistributedLocker --- .../BotSharp.Core/BotSharp.Core.csproj | 12 ++--- .../Infrastructures/DistributedLocker.cs | 50 ++++++++++--------- .../Repository/RepositoryPlugin.cs | 11 ++-- 3 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index da864c607..97dfa6cb9 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -11,13 +11,13 @@ Haiping Chen SciSharp STACK - LL Application Framework + AI Agent Application Framework Open source LLM application framework to build scalable, flexible and robust AI system. git https://github.com/SciSharp/BotSharp - Chatbot, Bot, LLM, AI, ChatGPT, OpenAI + Chatbot, Agent, LLM, AI, ChatGPT, OpenAI, Semantic Support dialogue status tracking. Since 2018 Haiping Chen https://github.com/SciSharp/BotSharp @@ -185,13 +185,13 @@ + - + - - - + + diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs index 82b655c79..0a5a7cb82 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/DistributedLocker.cs @@ -1,6 +1,4 @@ -using RedLockNet; -using RedLockNet.SERedis; -using RedLockNet.SERedis.Configuration; +using Medallion.Threading.Redis; using StackExchange.Redis; namespace BotSharp.Core.Infrastructures; @@ -8,42 +6,46 @@ namespace BotSharp.Core.Infrastructures; public class DistributedLocker { private readonly BotSharpDatabaseSettings _settings; - private readonly RedLockFactory _lockFactory; - public DistributedLocker(/*BotSharpDatabaseSettings settings*/) + public DistributedLocker(BotSharpDatabaseSettings settings) { - // _settings = settings; + _settings = settings; + } + + public async Task Lock(string resource, Func action, int timeoutInSeconds = 30) + { + var timeout = TimeSpan.FromSeconds(timeoutInSeconds); - var multiplexers = new List(); - foreach (var x in "".Split(';')) + var connection = await ConnectionMultiplexer.ConnectAsync(_settings.Redis); + var @lock = new RedisDistributedLock(resource, connection.GetDatabase()); + await using (var handle = await @lock.TryAcquireAsync(timeout)) { - var option = new ConfigurationOptions + if (handle != null) + { + await action(); + } + else { - AbortOnConnectFail = false, - EndPoints = { x } - }; - var _connMuliplexer = ConnectionMultiplexer.Connect(option); - multiplexers.Add(_connMuliplexer); + Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout."); + } } - - _lockFactory = RedLockFactory.Create(multiplexers); } - public async Task Lock(string resource, Func action) + public async Task Lock(string resource, Action action, int timeoutInSeconds = 30) { - var expiry = TimeSpan.FromSeconds(60); - var wait = TimeSpan.FromSeconds(30); - var retry = TimeSpan.FromSeconds(3); + var timeout = TimeSpan.FromSeconds(timeoutInSeconds); - await using (var redLock = await _lockFactory.CreateLockAsync(resource, expiry, wait, retry)) + var connection = await ConnectionMultiplexer.ConnectAsync(_settings.Redis); + var @lock = new RedisDistributedLock(resource, connection.GetDatabase()); + await using (var handle = await @lock.TryAcquireAsync(timeout)) { - if (redLock.IsAcquired) + if (handle != null) { - await action(); + action(); } else { - Console.WriteLine($"Acquire locak failed due to {resource} after {wait}s timeout."); + Serilog.Log.Logger.Error($"Acquire lock for {resource} failed due to after {timeout}s timeout."); } } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs b/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs index 3160597a5..bfbc0a6da 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/RepositoryPlugin.cs @@ -12,6 +12,9 @@ public class RepositoryPlugin : IBotSharpPlugin public void RegisterDI(IServiceCollection services, IConfiguration config) { + var myDatabaseSettings = new BotSharpDatabaseSettings(); + config.Bind("Database", myDatabaseSettings); + // In order to use EntityFramework.BootKit in other plugin services.AddScoped(provider => { @@ -25,14 +28,8 @@ public void RegisterDI(IServiceCollection services, IConfiguration config) return settingService.Bind("Database"); }); - services.AddScoped(provider => - { - var settingService = provider.GetRequiredService(); - return settingService.Bind("Database"); - }); + services.AddSingleton(provider => myDatabaseSettings); - var myDatabaseSettings = new BotSharpDatabaseSettings(); - config.Bind("Database", myDatabaseSettings); if (myDatabaseSettings.Default == RepositoryEnum.FileRepository) { services.AddScoped();