Bundle from worker to grain
This commit is contained in:
@@ -95,7 +95,7 @@ namespace Managing.Application.Backtests
|
||||
|
||||
try
|
||||
{
|
||||
var candles = GetCandles(config.Ticker, config.Timeframe, startDate, endDate);
|
||||
var candles = await GetCandles(config.Ticker, config.Timeframe, startDate, endDate);
|
||||
return await RunBacktestWithCandles(config, candles, user, save, withCandles, requestId, metadata);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -197,11 +197,11 @@ namespace Managing.Application.Backtests
|
||||
return await _accountService.GetAccountByAccountName(config.AccountName, false, false);
|
||||
}
|
||||
|
||||
private HashSet<Candle> GetCandles(Ticker ticker, Timeframe timeframe,
|
||||
private async Task<HashSet<Candle>> GetCandles(Ticker ticker, Timeframe timeframe,
|
||||
DateTime startDate, DateTime endDate)
|
||||
{
|
||||
var candles = _exchangeService.GetCandlesInflux(TradingExchanges.Evm, ticker,
|
||||
startDate, timeframe, endDate).Result;
|
||||
var candles = await _exchangeService.GetCandlesInflux(TradingExchanges.Evm, ticker,
|
||||
startDate, timeframe, endDate);
|
||||
|
||||
if (candles == null || candles.Count == 0)
|
||||
throw new Exception($"No candles for {ticker} on {timeframe} timeframe");
|
||||
@@ -277,19 +277,19 @@ namespace Managing.Application.Backtests
|
||||
return backtests;
|
||||
}
|
||||
|
||||
public IEnumerable<Backtest> GetBacktestsByRequestId(string requestId)
|
||||
public IEnumerable<Backtest> GetBacktestsByRequestId(Guid requestId)
|
||||
{
|
||||
var backtests = _backtestRepository.GetBacktestsByRequestId(requestId).ToList();
|
||||
return backtests;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Backtest>> GetBacktestsByRequestIdAsync(string requestId)
|
||||
public async Task<IEnumerable<Backtest>> GetBacktestsByRequestIdAsync(Guid requestId)
|
||||
{
|
||||
var backtests = await _backtestRepository.GetBacktestsByRequestIdAsync(requestId);
|
||||
return backtests;
|
||||
}
|
||||
|
||||
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByRequestIdPaginated(string requestId,
|
||||
public (IEnumerable<LightBacktest> Backtests, int TotalCount) GetBacktestsByRequestIdPaginated(Guid requestId,
|
||||
int page, int pageSize, string sortBy = "score", string sortOrder = "desc")
|
||||
{
|
||||
var (backtests, totalCount) =
|
||||
@@ -298,7 +298,7 @@ namespace Managing.Application.Backtests
|
||||
}
|
||||
|
||||
public async Task<(IEnumerable<LightBacktest> Backtests, int TotalCount)> GetBacktestsByRequestIdPaginatedAsync(
|
||||
string requestId, int page, int pageSize, string sortBy = "score", string sortOrder = "desc")
|
||||
Guid requestId, int page, int pageSize, string sortBy = "score", string sortOrder = "desc")
|
||||
{
|
||||
var (backtests, totalCount) =
|
||||
await _backtestRepository.GetBacktestsByRequestIdPaginatedAsync(requestId, page, pageSize, sortBy,
|
||||
@@ -384,7 +384,7 @@ namespace Managing.Application.Backtests
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteBacktestsByRequestIdAsync(string requestId)
|
||||
public async Task<bool> DeleteBacktestsByRequestIdAsync(Guid requestId)
|
||||
{
|
||||
try
|
||||
{
|
||||
@@ -418,6 +418,17 @@ namespace Managing.Application.Backtests
|
||||
public void InsertBundleBacktestRequestForUser(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
_backtestRepository.InsertBundleBacktestRequestForUser(user, bundleRequest);
|
||||
|
||||
// Trigger the BundleBacktestGrain to process this request
|
||||
TriggerBundleBacktestGrain(bundleRequest.RequestId);
|
||||
}
|
||||
|
||||
public async Task InsertBundleBacktestRequestForUserAsync(User user, BundleBacktestRequest bundleRequest)
|
||||
{
|
||||
await _backtestRepository.InsertBundleBacktestRequestForUserAsync(user, bundleRequest);
|
||||
|
||||
// Trigger the BundleBacktestGrain to process this request
|
||||
await TriggerBundleBacktestGrainAsync(bundleRequest.RequestId);
|
||||
}
|
||||
|
||||
public IEnumerable<BundleBacktestRequest> GetBundleBacktestRequestsByUser(User user)
|
||||
@@ -430,12 +441,12 @@ namespace Managing.Application.Backtests
|
||||
return await _backtestRepository.GetBundleBacktestRequestsByUserAsync(user);
|
||||
}
|
||||
|
||||
public BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, string id)
|
||||
public BundleBacktestRequest? GetBundleBacktestRequestByIdForUser(User user, Guid id)
|
||||
{
|
||||
return _backtestRepository.GetBundleBacktestRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public async Task<BundleBacktestRequest?> GetBundleBacktestRequestByIdForUserAsync(User user, string id)
|
||||
public async Task<BundleBacktestRequest?> GetBundleBacktestRequestByIdForUserAsync(User user, Guid id)
|
||||
{
|
||||
return await _backtestRepository.GetBundleBacktestRequestByIdForUserAsync(user, id);
|
||||
}
|
||||
@@ -450,12 +461,12 @@ namespace Managing.Application.Backtests
|
||||
await _backtestRepository.UpdateBundleBacktestRequestAsync(bundleRequest);
|
||||
}
|
||||
|
||||
public void DeleteBundleBacktestRequestByIdForUser(User user, string id)
|
||||
public void DeleteBundleBacktestRequestByIdForUser(User user, Guid id)
|
||||
{
|
||||
_backtestRepository.DeleteBundleBacktestRequestByIdForUser(user, id);
|
||||
}
|
||||
|
||||
public async Task DeleteBundleBacktestRequestByIdForUserAsync(User user, string id)
|
||||
public async Task DeleteBundleBacktestRequestByIdForUserAsync(User user, Guid id)
|
||||
{
|
||||
await _backtestRepository.DeleteBundleBacktestRequestByIdForUserAsync(user, id);
|
||||
}
|
||||
@@ -480,5 +491,65 @@ namespace Managing.Application.Backtests
|
||||
if (string.IsNullOrWhiteSpace(requestId) || response == null) return;
|
||||
await _hubContext.Clients.Group($"bundle-{requestId}").SendAsync("BundleBacktestUpdate", response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the BundleBacktestGrain to process a bundle request synchronously (fire and forget)
|
||||
/// </summary>
|
||||
private void TriggerBundleBacktestGrain(Guid bundleRequestId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bundleBacktestGrain = _grainFactory.GetGrain<IBundleBacktestGrain>(bundleRequestId);
|
||||
|
||||
// Fire and forget - don't await
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await bundleBacktestGrain.ProcessBundleRequestAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error triggering BundleBacktestGrain for request {RequestId}",
|
||||
bundleRequestId);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in TriggerBundleBacktestGrain for request {RequestId}", bundleRequestId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the BundleBacktestGrain to process a bundle request asynchronously
|
||||
/// </summary>
|
||||
private Task TriggerBundleBacktestGrainAsync(Guid bundleRequestId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var bundleBacktestGrain = _grainFactory.GetGrain<IBundleBacktestGrain>(bundleRequestId);
|
||||
|
||||
// Fire and forget - don't await the actual processing
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
await bundleBacktestGrain.ProcessBundleRequestAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error triggering BundleBacktestGrain for request {RequestId}",
|
||||
bundleRequestId);
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error in TriggerBundleBacktestGrainAsync for request {RequestId}",
|
||||
bundleRequestId);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user