Fix Runtime

This commit is contained in:
2025-10-06 00:55:18 +07:00
parent 6cbfff38d0
commit dab4807334
8 changed files with 172 additions and 9 deletions

View File

@@ -168,11 +168,15 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
_tradingBot = CreateTradingBotInstance(_state.State.Config);
await _tradingBot.Start(previousStatus);
// Set startup time when bot actually starts running
_state.State.StartupTime = DateTime.UtcNow;
// Track runtime: set LastStartTime when bot starts
// Set startup time only once (first successful start)
if (_state.State.StartupTime == default)
{
_state.State.StartupTime = DateTime.UtcNow;
}
// Track runtime: set LastStartTime when bot starts and clear LastStopTime
_state.State.LastStartTime = DateTime.UtcNow;
_state.State.LastStopTime = null;
await _state.WriteStateAsync();
@@ -205,6 +209,14 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
if (status == BotStatus.Running && _tradingBot != null)
{
await RegisterReminder();
// Ensure runtime timestamps are consistent if already running
if (!_state.State.LastStartTime.HasValue)
{
_state.State.LastStartTime = DateTime.UtcNow;
_state.State.LastStopTime = null;
await _state.WriteStateAsync();
await SaveBotAsync(BotStatus.Running);
}
_logger.LogInformation("LiveTradingBotGrain {GrainId} is already running", this.GetPrimaryKey());
return;
}
@@ -732,7 +744,7 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
try
{
Bot bot = null;
if (_tradingBot == null || _state.State.User == null)
if (_tradingBot == null)
{
// Save bot statistics for saved bots
bot = new Bot
@@ -754,6 +766,25 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
}
else
{
// Ensure we have a User reference; fetch from DB if missing
if (_state.State.User == null)
{
try
{
var existingBot = await ServiceScopeHelpers.WithScopedService<IBotService, Bot>(
_scopeFactory,
async botService => await botService.GetBotByIdentifier(_state.State.Identifier));
if (existingBot?.User != null)
{
_state.State.User = existingBot.User;
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unable to load user for bot {BotId} while saving stats", _state.State.Identifier);
}
}
// Calculate statistics using TradingBox helpers
var (tradeWins, tradeLosses) = TradingBox.GetWinLossCount(_tradingBot.Positions);
var pnl = _tradingBot.GetProfitAndLoss();
@@ -781,6 +812,9 @@ public class LiveTradingBotGrain : Grain, ILiveTradingBotGrain, IRemindable
User = _state.State.User,
Status = status,
StartupTime = _state.State.StartupTime,
LastStartTime = _state.State.LastStartTime,
LastStopTime = _state.State.LastStopTime,
AccumulatedRunTimeSeconds = _state.State.AccumulatedRunTimeSeconds,
CreateDate = _state.State.CreateDate,
TradeWins = tradeWins,
TradeLosses = tradeLosses,

View File

@@ -359,6 +359,9 @@ namespace Managing.Application.ManageBot
|| existingBot.LongPositionCount != bot.LongPositionCount
|| existingBot.ShortPositionCount != bot.ShortPositionCount
|| !string.Equals(existingBot.Name, bot.Name, StringComparison.Ordinal)
|| existingBot.AccumulatedRunTimeSeconds != bot.AccumulatedRunTimeSeconds
|| existingBot.LastStartTime != bot.LastStartTime
|| existingBot.LastStopTime != bot.LastStopTime
|| existingBot.Ticker != bot.Ticker)
{
_tradingBotLogger.LogInformation("Update bot statistics for bot {BotId}",