Add event
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
using Managing.Application.Abstractions;
|
||||
using Managing.Application.Abstractions.Grains;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
@@ -13,14 +14,17 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
|
||||
{
|
||||
private readonly IPersistentState<BotRegistryState> _state;
|
||||
private readonly ILogger<LiveBotRegistryGrain> _logger;
|
||||
private readonly IBotService _botService;
|
||||
|
||||
public LiveBotRegistryGrain(
|
||||
[PersistentState("bot-registry", "registry-store")]
|
||||
IPersistentState<BotRegistryState> state,
|
||||
ILogger<LiveBotRegistryGrain> logger)
|
||||
ILogger<LiveBotRegistryGrain> logger,
|
||||
IBotService botService)
|
||||
{
|
||||
_state = state;
|
||||
_logger = logger;
|
||||
_botService = botService;
|
||||
}
|
||||
|
||||
public override async Task OnActivateAsync(CancellationToken cancellationToken)
|
||||
@@ -60,6 +64,9 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
|
||||
_logger.LogInformation(
|
||||
"Bot {Identifier} registered successfully for user {UserId}. Total bots: {TotalBots}, Active bots: {ActiveBots}",
|
||||
identifier, userId, _state.State.TotalBotsCount, _state.State.ActiveBotsCount);
|
||||
|
||||
// Notify platform summary grain about strategy deployment
|
||||
await NotifyStrategyDeployedAsync(identifier, userId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -94,6 +101,9 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
|
||||
_logger.LogInformation(
|
||||
"Bot {Identifier} unregistered successfully from user {UserId}. Total bots: {TotalBots}",
|
||||
identifier, entryToRemove.UserId, _state.State.TotalBotsCount);
|
||||
|
||||
// Notify platform summary grain about strategy stopped
|
||||
await NotifyStrategyStoppedAsync(identifier, entryToRemove.UserId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -176,4 +186,66 @@ public class LiveBotRegistryGrain : Grain, ILiveBotRegistryGrain
|
||||
|
||||
return Task.FromResult(entry.Status);
|
||||
}
|
||||
|
||||
private async Task NotifyStrategyDeployedAsync(Guid identifier, int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get bot details for the event
|
||||
var bot = await _botService.GetBotByIdentifier(identifier);
|
||||
if (bot != null)
|
||||
{
|
||||
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
|
||||
|
||||
var deployedEvent = new StrategyDeployedEvent
|
||||
{
|
||||
StrategyId = identifier,
|
||||
AgentName = bot.User.AgentName,
|
||||
StrategyName = bot.Name
|
||||
};
|
||||
|
||||
await platformGrain.OnStrategyDeployedAsync(deployedEvent);
|
||||
_logger.LogDebug("Notified platform summary about strategy deployment: {StrategyName}", bot.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Could not find bot {Identifier} to notify platform summary", identifier);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to notify platform summary about strategy deployment for bot {Identifier}", identifier);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task NotifyStrategyStoppedAsync(Guid identifier, int userId)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get bot details for the event
|
||||
var bot = await _botService.GetBotByIdentifier(identifier);
|
||||
if (bot != null)
|
||||
{
|
||||
var platformGrain = GrainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
|
||||
|
||||
var stoppedEvent = new StrategyStoppedEvent
|
||||
{
|
||||
StrategyId = identifier,
|
||||
AgentName = bot.User?.Name ?? $"User-{userId}",
|
||||
StrategyName = bot.Name
|
||||
};
|
||||
|
||||
await platformGrain.OnStrategyStoppedAsync(stoppedEvent);
|
||||
_logger.LogDebug("Notified platform summary about strategy stopped: {StrategyName}", bot.Name);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Could not find bot {Identifier} to notify platform summary", identifier);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to notify platform summary about strategy stopped for bot {Identifier}", identifier);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -377,6 +377,27 @@ public class TradingBotBase : ITradingBot
|
||||
if (position.Status.Equals(PositionStatus.New))
|
||||
{
|
||||
await SetPositionStatus(position.SignalIdentifier, PositionStatus.Filled);
|
||||
|
||||
// Notify platform summary about the executed trade
|
||||
try
|
||||
{
|
||||
await ServiceScopeHelpers.WithScopedService<IGrainFactory>(_scopeFactory, async grainFactory =>
|
||||
{
|
||||
var platformGrain = grainFactory.GetGrain<IPlatformSummaryGrain>("platform-summary");
|
||||
var tradeExecutedEvent = new TradeExecutedEvent
|
||||
{
|
||||
TradeId = position.Identifier,
|
||||
Ticker = position.Ticker,
|
||||
Volume = position.Open.Price * position.Open.Quantity * position.Open.Leverage
|
||||
};
|
||||
|
||||
await platformGrain.OnTradeExecutedAsync(tradeExecutedEvent);
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.LogWarning(ex, "Failed to notify platform summary about trade execution for position {PositionId}", position.Identifier);
|
||||
}
|
||||
}
|
||||
|
||||
position = brokerPosition;
|
||||
|
||||
Reference in New Issue
Block a user