- Aggiunto TradeHistoryService per persistenza trade/posizioni attive su disco (JSON, auto-save/restore) - Logging centralizzato (LoggingService) con livelli, categorie, simbolo e buffer circolare (500 log) - Nuova pagina Logs: monitoraggio real-time, filtri avanzati, cancellazione log, colorazione livelli - Sezione "Dati Persistenti" in Settings: conteggio trade, dimensione dati, reset con conferma modale - Background service per salvataggio sicuro su shutdown/stop container - Aggiornata sidebar, stili modali/bottoni danger, .gitignore e documentazione (README, CHANGELOG, UNRAID_INSTALL, checklist) - Versione 1.3.0
59 lines
1.8 KiB
C#
59 lines
1.8 KiB
C#
namespace TradingBot.Services;
|
|
|
|
/// <summary>
|
|
/// Background service for automatic data persistence on application shutdown
|
|
/// </summary>
|
|
public class TradingBotBackgroundService : BackgroundService
|
|
{
|
|
private readonly TradingBotService _tradingBotService;
|
|
private readonly ILogger<TradingBotBackgroundService> _logger;
|
|
private readonly IHostApplicationLifetime _lifetime;
|
|
|
|
public TradingBotBackgroundService(
|
|
TradingBotService tradingBotService,
|
|
ILogger<TradingBotBackgroundService> logger,
|
|
IHostApplicationLifetime lifetime)
|
|
{
|
|
_tradingBotService = tradingBotService;
|
|
_logger = logger;
|
|
_lifetime = lifetime;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
_logger.LogInformation("TradingBot Background Service started");
|
|
|
|
// Register shutdown handler
|
|
_lifetime.ApplicationStopping.Register(OnShutdown);
|
|
|
|
// Keep service running
|
|
await Task.Delay(Timeout.Infinite, stoppingToken);
|
|
}
|
|
|
|
private void OnShutdown()
|
|
{
|
|
_logger.LogInformation("Application shutdown detected, saving trade data...");
|
|
|
|
try
|
|
{
|
|
// Stop bot if running
|
|
if (_tradingBotService.Status.IsRunning)
|
|
{
|
|
_tradingBotService.Stop();
|
|
}
|
|
|
|
_logger.LogInformation("Trade data saved successfully on shutdown");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error saving data on shutdown");
|
|
}
|
|
}
|
|
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
_logger.LogInformation("TradingBot Background Service stopping");
|
|
await base.StopAsync(cancellationToken);
|
|
}
|
|
}
|