Add Role based grain placement

This commit is contained in:
2025-09-18 20:17:28 +07:00
parent 530dd83daa
commit c2f3734021
16 changed files with 404 additions and 18 deletions

View File

@@ -3,12 +3,18 @@ using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Services;
using Managing.Application.Bots.Models;
using Managing.Application.Orleans;
using Managing.Domain.Statistics;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
namespace Managing.Application.Bots.Grains;
/// <summary>
/// Orleans grain for Agent operations.
/// Uses custom trading placement with load balancing and built-in fallback.
/// </summary>
[TradingPlacement] // Use custom trading placement with load balancing
public class AgentGrain : Grain, IAgentGrain
{
private readonly IPersistentState<AgentGrainState> _state;

View File

@@ -1,5 +1,6 @@
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Repositories;
using Managing.Application.Orleans;
using Managing.Common;
using Managing.Domain.Backtests;
using Managing.Domain.Bots;
@@ -20,8 +21,10 @@ namespace Managing.Application.Bots.Grains;
/// Orleans grain for backtest trading bot operations.
/// Uses composition with TradingBotBase to maintain separation of concerns.
/// This grain is stateless and follows the exact pattern of GetBacktestingResult from Backtester.cs.
/// Uses custom compute placement with random fallback.
/// </summary>
[StatelessWorker]
[TradingPlacement] // Use custom compute placement with random fallback
public class BacktestTradingBotGrain : Grain, IBacktestTradingBotGrain
{
private readonly ILogger<BacktestTradingBotGrain> _logger;

View File

@@ -1,5 +1,6 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Orleans;
using Managing.Core;
using Managing.Domain.Bots;
using Microsoft.Extensions.DependencyInjection;
@@ -13,12 +14,14 @@ namespace Managing.Application.Bots.Grains;
/// Fetches all running bots and pings them to ensure their reminders are properly registered.
/// This grain ensures that only one instance runs the initialization process
/// even in multi-silo environments.
/// Uses custom trading placement with load balancing and built-in fallback.
/// </summary>
[TradingPlacement] // Use custom trading placement with load balancing
public class BotReminderInitializerGrain : Grain, IBotReminderInitializerGrain, IRemindable
{
private readonly IServiceScopeFactory _scopeFactory;
private readonly ILogger<BotReminderInitializerGrain> _logger;
private const string CheckRunningBotsReminderName = "CheckRunningBotsReminder";
public BotReminderInitializerGrain(
@@ -37,11 +40,12 @@ public class BotReminderInitializerGrain : Grain, IBotReminderInitializerGrain,
{
try
{
_logger.LogInformation("BotReminderInitializerGrain starting - fetching running bots to reactivate reminders");
_logger.LogInformation(
"BotReminderInitializerGrain starting - fetching running bots to reactivate reminders");
// Get all running bots from the database
var runningBots = await GetRunningBotsAsync();
if (!runningBots.Any())
{
_logger.LogInformation("No running bots found to reactivate");
@@ -58,16 +62,17 @@ public class BotReminderInitializerGrain : Grain, IBotReminderInitializerGrain,
try
{
_logger.LogDebug("Reactivating bot {BotId} ({BotName})", bot.Identifier, bot.Name);
// First, update the bot status in the registry to Running
await botRegistry.UpdateBotStatus(bot.Identifier, BotStatus.Running);
_logger.LogDebug("Updated registry status to Running for bot {BotId} ({BotName})", bot.Identifier, bot.Name);
_logger.LogDebug("Updated registry status to Running for bot {BotId} ({BotName})", bot.Identifier,
bot.Name);
// Then ping the bot to reactivate it
var grain = GrainFactory.GetGrain<ILiveTradingBotGrain>(bot.Identifier);
var success = await grain.PingAsync();
if (success)
{
_logger.LogDebug("Successfully reactivated bot {BotId} ({BotName})", bot.Identifier, bot.Name);
@@ -94,7 +99,8 @@ public class BotReminderInitializerGrain : Grain, IBotReminderInitializerGrain,
TimeSpan.FromHours(1), // Start in 1 hour
TimeSpan.FromHours(1)); // Repeat every hour
_logger.LogInformation("BotReminderInitializerGrain completed - processed {Count} running bots", runningBots.Count());
_logger.LogInformation("BotReminderInitializerGrain completed - processed {Count} running bots",
runningBots.Count());
}
catch (Exception ex)
{
@@ -134,4 +140,4 @@ public class BotReminderInitializerGrain : Grain, IBotReminderInitializerGrain,
return Enumerable.Empty<Bot>();
}
}
}
}

View File

@@ -1,5 +1,6 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Orleans;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
@@ -9,7 +10,9 @@ namespace Managing.Application.Bots.Grains;
/// Orleans grain for LiveBotRegistry operations.
/// This grain acts as a central, durable directory for all LiveTradingBot grains.
/// It maintains a persistent, up-to-date list of all known bot IDs and their status.
/// Uses custom trading placement with load balancing and built-in fallback.
/// </summary>
[TradingPlacement] // Use custom trading placement with load balancing
public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
{
private readonly IPersistentState<BotRegistryState> _state;

View File

@@ -1,6 +1,7 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Services;
using Managing.Application.Orleans;
using Managing.Application.Shared;
using Managing.Common;
using Managing.Core;
@@ -19,7 +20,9 @@ namespace Managing.Application.Bots.Grains;
/// Orleans grain for live trading bot operations.
/// Uses composition with TradingBotBase to maintain separation of concerns.
/// This grain handles live trading scenarios with real-time market data and execution.
/// Uses custom trading placement with load balancing and built-in fallback.
/// </summary>
[TradingPlacement] // Use custom trading placement with load balancing
public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
{
private readonly IPersistentState<TradingBotGrainState> _state;