Fix long time update AgentName
This commit is contained in:
@@ -11,6 +11,12 @@ namespace Managing.Application.Abstractions.Grains
|
|||||||
/// <param name="agentName">The display name of the agent.</param>
|
/// <param name="agentName">The display name of the agent.</param>
|
||||||
Task InitializeAsync(int userId, string agentName);
|
Task InitializeAsync(int userId, string agentName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Updates only the agent name without recalculating summary.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="agentName">The new agent name.</param>
|
||||||
|
Task UpdateAgentNameAsync(string agentName);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a summary of the agent's stats for the AgentRegistryGrain.
|
/// Generates a summary of the agent's stats for the AgentRegistryGrain.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -51,8 +51,33 @@ public class AgentGrain : Grain, IAgentGrain
|
|||||||
{
|
{
|
||||||
_state.State.AgentName = agentName;
|
_state.State.AgentName = agentName;
|
||||||
await _state.WriteStateAsync();
|
await _state.WriteStateAsync();
|
||||||
await UpdateSummary();
|
|
||||||
_logger.LogInformation("Agent {UserId} initialized with name {AgentName}", userId, agentName);
|
// Create an empty AgentSummary for the new agent
|
||||||
|
var emptySummary = new AgentSummary
|
||||||
|
{
|
||||||
|
UserId = userId,
|
||||||
|
AgentName = agentName,
|
||||||
|
TotalPnL = 0,
|
||||||
|
TotalROI = 0,
|
||||||
|
Wins = 0,
|
||||||
|
Losses = 0,
|
||||||
|
Runtime = null,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
UpdatedAt = DateTime.UtcNow,
|
||||||
|
ActiveStrategiesCount = 0,
|
||||||
|
TotalVolume = 0,
|
||||||
|
TotalBalance = 0
|
||||||
|
};
|
||||||
|
|
||||||
|
await _agentService.SaveOrUpdateAgentSummary(emptySummary);
|
||||||
|
_logger.LogInformation("Agent {UserId} initialized with name {AgentName} and empty summary", userId, agentName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateAgentNameAsync(string agentName)
|
||||||
|
{
|
||||||
|
_state.State.AgentName = agentName;
|
||||||
|
await _state.WriteStateAsync();
|
||||||
|
_logger.LogInformation("Agent {UserId} updated with name {AgentName}", this.GetPrimaryKeyLong(), agentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task OnAgentSummaryUpdateAsync(AgentSummaryUpdateEvent updateEvent)
|
public async Task OnAgentSummaryUpdateAsync(AgentSummaryUpdateEvent updateEvent)
|
||||||
|
|||||||
@@ -109,6 +109,20 @@ public class UserService : IUserService
|
|||||||
{
|
{
|
||||||
account
|
account
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Initialize AgentGrain for new user (with empty agent name initially)
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(user.Id);
|
||||||
|
await agentGrain.InitializeAsync(user.Id, string.Empty);
|
||||||
|
_logger.LogInformation("AgentGrain initialized for new user {UserId}", user.Id);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to initialize AgentGrain for new user {UserId}", user.Id);
|
||||||
|
SentrySdk.CaptureException(ex);
|
||||||
|
// Don't throw here to avoid breaking the user creation process
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
@@ -165,17 +179,16 @@ public class UserService : IUserService
|
|||||||
user.AgentName = agentName;
|
user.AgentName = agentName;
|
||||||
await _userRepository.SaveOrUpdateUserAsync(user);
|
await _userRepository.SaveOrUpdateUserAsync(user);
|
||||||
|
|
||||||
// Initialize the AgentGrain for this user
|
// Update the AgentGrain with the new agent name (lightweight operation)
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(user.Id);
|
var agentGrain = _grainFactory.GetGrain<IAgentGrain>(user.Id);
|
||||||
await agentGrain.InitializeAsync(user.Id, agentName);
|
await agentGrain.UpdateAgentNameAsync(agentName);
|
||||||
_logger.LogInformation("AgentGrain initialized for user {UserId} with agent name {AgentName}", user.Id,
|
_logger.LogInformation("AgentGrain updated for user {UserId} with agent name {AgentName}", user.Id, agentName);
|
||||||
agentName);
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "Failed to initialize AgentGrain for user {UserId} with agent name {AgentName}",
|
_logger.LogError(ex, "Failed to update AgentGrain for user {UserId} with agent name {AgentName}",
|
||||||
user.Id, agentName);
|
user.Id, agentName);
|
||||||
// Don't throw here to avoid breaking the user update process
|
// Don't throw here to avoid breaking the user update process
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user