From 84f3e91dc62b912087030f727579298021700027 Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 30 Jul 2025 20:44:58 +0700 Subject: [PATCH] Try fixing orleans server runtime --- src/Managing.Api/Program.cs | 2 ++ .../Bots/Grains/LiveTradingBotGrain.cs | 8 +++++- src/Managing.Bootstrap/ApiBootstrap.cs | 26 ++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Managing.Api/Program.cs b/src/Managing.Api/Program.cs index 859eb5d..fa83e38 100644 --- a/src/Managing.Api/Program.cs +++ b/src/Managing.Api/Program.cs @@ -287,4 +287,6 @@ app.UseEndpoints(endpoints => }); }); + + app.Run(); \ No newline at end of file diff --git a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs index 2d313b4..6a47f5d 100644 --- a/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs +++ b/src/Managing.Application/Bots/Grains/LiveTradingBotGrain.cs @@ -409,7 +409,7 @@ public class LiveTradingBotGrain : Grain, ITradingBotGrain { try { - if (_tradingBot == null || State.Status != BotStatus.Up) + if (_tradingBot == null || State.Status != BotStatus.Up || _isDisposed) { return; } @@ -422,6 +422,12 @@ public class LiveTradingBotGrain : Grain, ITradingBotGrain await SaveBackupToState(); } + catch (ObjectDisposedException) + { + // Gracefully handle disposed service provider during shutdown + _logger.LogInformation("Service provider disposed during shutdown for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey()); + return; + } catch (Exception ex) { _logger.LogError(ex, "Error during bot execution cycle for LiveTradingBotGrain {GrainId}", this.GetPrimaryKey()); diff --git a/src/Managing.Bootstrap/ApiBootstrap.cs b/src/Managing.Bootstrap/ApiBootstrap.cs index a2beeee..ddb2336 100644 --- a/src/Managing.Bootstrap/ApiBootstrap.cs +++ b/src/Managing.Bootstrap/ApiBootstrap.cs @@ -45,6 +45,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Orleans.Configuration; namespace Managing.Bootstrap; @@ -100,9 +101,22 @@ public static class ApiBootstrap } siloBuilder - .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Information)) - .UseDashboard(options => { }) - .AddMemoryGrainStorageAsDefault() + .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Information)); + + // Only enable dashboard in development to avoid shutdown issues + if (!isProduction) + { + siloBuilder.UseDashboard(options => + { + // Configure dashboard with proper shutdown handling + options.Port = 8080; + options.HostSelf = true; + options.CounterUpdateIntervalMs = 10000; // 10 seconds + options.HideTrace = true; // Hide trace to reduce dashboard overhead + }); + } + + siloBuilder.AddMemoryGrainStorageAsDefault() .ConfigureServices(services => { // Register existing services for Orleans DI @@ -112,6 +126,12 @@ public static class ApiBootstrap services.AddTransient(); services.AddTransient(); services.AddTransient(); + }) + .Configure(options => + { + // Configure cluster options + options.ServiceId = "ManagingApp"; + options.ClusterId = configuration["ASPNETCORE_ENVIRONMENT"] ?? "Development"; }); }) ;