using Bitget_Server.Application.Options;
using Bitget_Server.Application.State;
using Bitget_Server.Domain.Enums;
using Bitget_Server.Infrastructure.Bitget;
using Microsoft.Extensions.Options;
namespace Bitget_Server.Application.BackgroundServices;
///
/// 기존 MainForm.getMarketInfoList_WebService (300ms loop) 이식.
/// USDT-FUTURES 마켓 Ticker 정보를 주기적으로 새로고침하여 MarketState.marketInfoList 를 갱신한다.
///
public class MarketInfoBackgroundService : BackgroundService
{
private readonly BitgetClient _client;
private readonly BitgetOptions _options;
private readonly MarketState _state;
private readonly ILogger _logger;
public MarketInfoBackgroundService(
BitgetClient client,
IOptions options,
MarketState state,
ILogger logger)
{
_client = client;
_options = options.Value;
_state = state;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("MarketInfoBackgroundService 시작 (Interval={Ms}ms)", _options.MarketInfoUpdateIntervalMs);
var delay = TimeSpan.FromMilliseconds(Math.Max(100, _options.MarketInfoUpdateIntervalMs));
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(delay, stoppingToken).ConfigureAwait(false);
await _client.SendWebServerAsync(
ExchangeWebReqType.BitgetMarket_Future,
ct: stoppingToken).ConfigureAwait(false);
// 결과는 BitgetClient.Parse 가 _state.marketInfoList 에 반영함
}
catch (OperationCanceledException) { /* shutdown */ }
catch (Exception ex)
{
_logger.LogWarning(ex, "MarketInfo 업데이트 오류");
}
}
_logger.LogInformation("MarketInfoBackgroundService 종료");
}
}