단순 이관
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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.DataCalculateThread (1000ms loop) 이식.
|
||||
/// BitgetWebSocketClient 가 수집한 메시지를 주기적으로 꺼내어 candleDataLists 로 병합한다.
|
||||
/// </summary>
|
||||
public class WebSocketCalcBackgroundService : BackgroundService
|
||||
{
|
||||
private readonly BitgetWebSocketClient _ws;
|
||||
private readonly MarketState _state;
|
||||
private readonly BitgetOptions _options;
|
||||
private readonly ILogger<WebSocketCalcBackgroundService> _logger;
|
||||
|
||||
public WebSocketCalcBackgroundService(
|
||||
BitgetWebSocketClient ws,
|
||||
MarketState state,
|
||||
IOptions<BitgetOptions> options,
|
||||
ILogger<WebSocketCalcBackgroundService> logger)
|
||||
{
|
||||
_ws = ws;
|
||||
_state = state;
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_logger.LogInformation("WebSocketCalcBackgroundService 시작 (Interval={Ms}ms)", _options.WebSocketCalcIntervalMs);
|
||||
|
||||
var delay = TimeSpan.FromMilliseconds(Math.Max(200, _options.WebSocketCalcIntervalMs));
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Delay(delay, stoppingToken).ConfigureAwait(false);
|
||||
|
||||
// WebSocket 이 꺼져 있으면 처리할 데이터도 없으므로 skip
|
||||
if (!_ws.IsConnected) continue;
|
||||
|
||||
List<object> intervalsSnapshot;
|
||||
lock (_state.SyncRoot)
|
||||
intervalsSnapshot = _state.IntervalList.ToList();
|
||||
|
||||
_ws.CalculateDataList(BitgetWebSocketType.캔들정보, intervalsSnapshot);
|
||||
}
|
||||
catch (OperationCanceledException) { /* shutdown */ }
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "WebSocket 집계 루프 오류");
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("WebSocketCalcBackgroundService 종료");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user