60 lines
2.0 KiB
C#
60 lines
2.0 KiB
C#
|
|
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;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 기존 MainForm.getMarketInfoList_WebService (300ms loop) 이식.
|
||
|
|
/// USDT-FUTURES 마켓 Ticker 정보를 주기적으로 새로고침하여 MarketState.marketInfoList 를 갱신한다.
|
||
|
|
/// </summary>
|
||
|
|
public class MarketInfoBackgroundService : BackgroundService
|
||
|
|
{
|
||
|
|
private readonly BitgetClient _client;
|
||
|
|
private readonly BitgetOptions _options;
|
||
|
|
private readonly MarketState _state;
|
||
|
|
private readonly ILogger<MarketInfoBackgroundService> _logger;
|
||
|
|
|
||
|
|
public MarketInfoBackgroundService(
|
||
|
|
BitgetClient client,
|
||
|
|
IOptions<BitgetOptions> options,
|
||
|
|
MarketState state,
|
||
|
|
ILogger<MarketInfoBackgroundService> 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 종료");
|
||
|
|
}
|
||
|
|
}
|