Reduce Agent Summary call

This commit is contained in:
2025-09-15 00:19:21 +07:00
parent 37d57a1bb8
commit b0d2dcc6b9
10 changed files with 402 additions and 36 deletions

View File

@@ -1,15 +1,15 @@
using Managing.Application.Abstractions;
using Managing.Application.Abstractions.Grains;
using Managing.Application.Abstractions.Models;
using Managing.Application.Abstractions.Services;
using Managing.Application.Bots.Models;
using Managing.Application.Shared;
using Managing.Domain.Statistics;
using Microsoft.Extensions.Logging;
using static Managing.Common.Enums;
namespace Managing.Application.Bots.Grains;
public class AgentGrain : Grain, IAgentGrain, IRemindable
public class AgentGrain : Grain, IAgentGrain
{
private readonly IPersistentState<AgentGrainState> _state;
private readonly ILogger<AgentGrain> _logger;
@@ -19,7 +19,6 @@ public class AgentGrain : Grain, IAgentGrain, IRemindable
private readonly IUserService _userService;
private readonly IAccountService _accountService;
private readonly ITradingService _tradingService;
private const string _updateSummaryReminderName = "UpdateAgentSummary";
public AgentGrain(
[PersistentState("agent-state", "agent-store")]
@@ -54,42 +53,25 @@ public class AgentGrain : Grain, IAgentGrain, IRemindable
await _state.WriteStateAsync();
await UpdateSummary();
_logger.LogInformation("Agent {UserId} initialized with name {AgentName}", userId, agentName);
await RegisterReminderAsync();
}
private async Task RegisterReminderAsync()
public async Task OnAgentSummaryUpdateAsync(AgentSummaryUpdateEvent updateEvent)
{
try
{
var options = GrainHelpers.GetDynamicRandomizedTimerOptions(TimeSpan.FromMinutes(2), 200);
// Register a reminder that fires every 5 minutes
await this.RegisterOrUpdateReminder(_updateSummaryReminderName, options.dueTime,
options.period);
_logger.LogInformation("Reminder registered for agent {UserId} to update summary every 5 minutes",
this.GetPrimaryKeyLong());
_logger.LogInformation("Received agent summary update event for user {UserId}, event type: {EventType}",
this.GetPrimaryKeyLong(), updateEvent.EventType);
// Only update summary if the event is for this agent's bots
if (_state.State.BotIds.Contains(updateEvent.BotId))
{
await UpdateSummary();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to register reminder for agent {UserId}", this.GetPrimaryKeyLong());
}
}
public async Task ReceiveReminder(string reminderName, TickStatus status)
{
if (reminderName == _updateSummaryReminderName)
{
try
{
_logger.LogInformation("Reminder triggered for agent {UserId} to update summary",
this.GetPrimaryKeyLong());
await UpdateSummary();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating agent summary from reminder for user {UserId}",
this.GetPrimaryKeyLong());
}
_logger.LogError(ex, "Error processing agent summary update event for user {UserId}",
this.GetPrimaryKeyLong());
}
}
@@ -203,6 +185,9 @@ public class AgentGrain : Grain, IAgentGrain, IRemindable
{
await _state.WriteStateAsync();
_logger.LogInformation("Bot {BotId} registered to Agent {UserId}", botId, this.GetPrimaryKeyLong());
// Update summary after registering bot
await UpdateSummary();
}
}
@@ -212,6 +197,9 @@ public class AgentGrain : Grain, IAgentGrain, IRemindable
{
await _state.WriteStateAsync();
_logger.LogInformation("Bot {BotId} unregistered from Agent {UserId}", botId, this.GetPrimaryKeyLong());
// Update summary after unregistering bot
await UpdateSummary();
}
}
}