From bbe69f9c2be6276187ed48f111f966e3f38be79a Mon Sep 17 00:00:00 2001 From: cryptooda Date: Wed, 23 Jul 2025 02:02:26 +0700 Subject: [PATCH] Add parralelism for bundle worker --- .../BundleBacktestWorker.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Managing.Application.Workers/BundleBacktestWorker.cs b/src/Managing.Application.Workers/BundleBacktestWorker.cs index d446768..93da987 100644 --- a/src/Managing.Application.Workers/BundleBacktestWorker.cs +++ b/src/Managing.Application.Workers/BundleBacktestWorker.cs @@ -37,6 +37,9 @@ public class BundleBacktestWorker : BaseWorker protected override async Task Run(CancellationToken cancellationToken) { + var maxDegreeOfParallelism = 3; + using var semaphore = new SemaphoreSlim(maxDegreeOfParallelism); + var processingTasks = new List(); try { // Get pending bundle backtest requests @@ -47,8 +50,21 @@ public class BundleBacktestWorker : BaseWorker if (cancellationToken.IsCancellationRequested) break; - await ProcessBundleRequest(bundleRequest, cancellationToken); + await semaphore.WaitAsync(cancellationToken); + var task = Task.Run(async () => + { + try + { + await ProcessBundleRequest(bundleRequest, cancellationToken); + } + finally + { + semaphore.Release(); + } + }, cancellationToken); + processingTasks.Add(task); } + await Task.WhenAll(processingTasks); await RetryUnfinishedBacktestsInFailedBundles(cancellationToken); }