From 4cf3531c327d94c023e11e401e2791ef087b1c16 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sun, 1 Dec 2024 17:38:17 +0000 Subject: [PATCH 1/4] Add ExecutionTimeout --- .../Repositories/BotSharpDatabaseSettings.cs | 21 +++++++++++-------- .../BotSharp.Core/BotSharp.Core.csproj | 2 +- .../BotSharp.Plugin.MongoStorage.csproj | 4 ++-- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs index b3c26033..19f5b141 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs @@ -2,18 +2,19 @@ namespace BotSharp.Abstraction.Repositories; public class BotSharpDatabaseSettings : DatabaseBasicSettings { - public string[] Assemblies { get; set; } - public string FileRepository { get; set; } - public string BotSharpMongoDb { get; set; } - public string TablePrefix { get; set; } - public DbConnectionSetting BotSharp { get; set; } - public string Redis { get; set; } + public string[] Assemblies { get; set; } = []; + public string FileRepository { get; set; } = string.Empty; + public string BotSharpMongoDb { get; set; } = string.Empty; + public string TablePrefix { get; set; } = string.Empty; + public DbConnectionSetting BotSharp { get; set; } = new(); + public string Redis { get; set; } = string.Empty; + public bool EnableReplica { get; set; } = true; } public class DatabaseBasicSettings { - public string Default { get; set; } - public DbConnectionSetting DefaultConnection { get; set; } + public string Default { get; set; } = string.Empty; + public DbConnectionSetting DefaultConnection { get; set; } = new(); public bool EnableSqlLog { get; set; } public bool EnableSensitiveDataLogging { get; set; } public bool EnableRetryOnFailure { get; set; } @@ -23,9 +24,11 @@ public class DbConnectionSetting { public string Master { get; set; } public string[] Slavers { get; set; } + public int ConnectionTimeout { get; set; } = 30; + public int ExecutionTimeout { get; set; } = 30; public DbConnectionSetting() { - Slavers = new string[0]; + Slavers = []; } } \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 1c82c84f..1c9955aa 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -190,7 +190,7 @@ - + diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index ab14d864..dced7973 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -11,8 +11,8 @@ - - + + From 3dc76fc7f564636af94a0d99bea74327f4ddb352 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sun, 1 Dec 2024 23:06:21 +0000 Subject: [PATCH 2/4] Support port for event --- .../Events/IEventSubscriber.cs | 2 +- .../Infrastructures/Events/RedisSubscriber.cs | 20 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs index e04c2488..465644ad 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Infrastructures/Events/IEventSubscriber.cs @@ -6,7 +6,7 @@ public interface IEventSubscriber { Task SubscribeAsync(string channel, Func received); - Task SubscribeAsync(string channel, string group, bool priorityEnabled, + Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled, Func received, CancellationToken? stoppingToken = null); } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs index 34ff0079..9a3c9982 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/Events/RedisSubscriber.cs @@ -25,7 +25,7 @@ public class RedisSubscriber : IEventSubscriber }); } - public async Task SubscribeAsync(string channel, string group, bool priorityEnabled, + public async Task SubscribeAsync(string channel, string group, int? port, bool priorityEnabled, Func received, CancellationToken? stoppingToken = null) { @@ -42,6 +42,12 @@ public class RedisSubscriber : IEventSubscriber await CreateConsumerGroup(db, channel, group); } + var consumer = Environment.MachineName; + if (port.HasValue) + { + consumer += $"-{port}"; + } + while (true) { await Task.Delay(100); @@ -54,28 +60,28 @@ public class RedisSubscriber : IEventSubscriber if (priorityEnabled) { - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, received) > 0) + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.High}", group, consumer, received) > 0) { continue; } - if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, received) > 0) + if (await HandleGroupMessage(db, $"{channel}-{EventPriority.Medium}", group, consumer, received) > 0) { continue; } - await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, received); + await HandleGroupMessage(db, $"{channel}-{EventPriority.Low}", group, consumer, received); } else { - await HandleGroupMessage(db, channel, group, received); + await HandleGroupMessage(db, channel, group, consumer, received); } } } - private async Task HandleGroupMessage(IDatabase db, string channel, string group, Func received) + private async Task HandleGroupMessage(IDatabase db, string channel, string group, string consumer, Func received) { - var entries = await db.StreamReadGroupAsync(channel, group, Environment.MachineName, count: 1); + var entries = await db.StreamReadGroupAsync(channel, group, consumer, count: 1); foreach (var entry in entries) { _logger.LogInformation($"Consumer {Environment.MachineName} received: {channel} {entry.Values[0].Value}"); From 393acb785e8fcbf48535a625d5d9af747faf2a7f Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Sun, 1 Dec 2024 23:35:43 +0000 Subject: [PATCH 3/4] Remove Aspire.MongoDB.Driver --- .../BotSharp.Plugin.MongoStorage.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index dced7973..88709238 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -11,7 +11,6 @@ - From 5a3478d8146fa9ecfedf59125cff49c22a870d67 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 2 Dec 2024 15:06:14 +0000 Subject: [PATCH 4/4] ShopInfoFromProductList --- .../BotSharp.Abstraction/Utilities/MathExt.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs new file mode 100644 index 00000000..4ea18179 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/MathExt.cs @@ -0,0 +1,14 @@ +namespace BotSharp.Abstraction.Utilities; + +public static class MathExt +{ + public static int Max(int a, int b, int c) + { + return Math.Max(Math.Max(a, b), c); + } + + public static long Max(long a, long b, long c) + { + return Math.Max(Math.Max(a, b), c); + } +}