parallel processing for routes and regions

This commit is contained in:
Anton Martynenko
2025-11-19 13:01:30 +01:00
parent 7f33567632
commit b66d3a0277
8 changed files with 331 additions and 133 deletions
@@ -1,5 +1,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using SatelliteProvider.Common.Configs;
using SatelliteProvider.Common.Interfaces;
namespace SatelliteProvider.Services;
@@ -8,21 +10,49 @@ public class RegionProcessingService : BackgroundService
{
private readonly IRegionRequestQueue _queue;
private readonly IRegionService _regionService;
private readonly ProcessingConfig _processingConfig;
private readonly ILogger<RegionProcessingService> _logger;
public RegionProcessingService(
IRegionRequestQueue queue,
IRegionService regionService,
IOptions<ProcessingConfig> processingConfig,
ILogger<RegionProcessingService> logger)
{
_queue = queue;
_regionService = regionService;
_processingConfig = processingConfig.Value;
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Region Processing Service started");
_logger.LogInformation("Region Processing Service started with {MaxConcurrent} parallel workers",
_processingConfig.MaxConcurrentRegions);
var workers = new List<Task>();
for (int i = 0; i < _processingConfig.MaxConcurrentRegions; i++)
{
var workerId = i + 1;
workers.Add(ProcessRegionWorkerAsync(workerId, stoppingToken));
}
await Task.WhenAll(workers);
_logger.LogInformation("Region Processing Service stopped");
}
private async Task ProcessRegionWorkerAsync(int workerId, CancellationToken stoppingToken)
{
if (workerId > 1)
{
var startupDelay = Random.Shared.Next(100, 500);
_logger.LogInformation("Region worker {WorkerId} starting with {Delay}ms delay to reduce contention",
workerId, startupDelay);
await Task.Delay(startupDelay, stoppingToken);
}
_logger.LogInformation("Region worker {WorkerId} started", workerId);
while (!stoppingToken.IsCancellationRequested)
{
@@ -32,9 +62,13 @@ public class RegionProcessingService : BackgroundService
if (request != null)
{
_logger.LogInformation("Dequeued region request {RegionId}", request.Id);
_logger.LogInformation("Worker {WorkerId}: Dequeued region request {RegionId}",
workerId, request.Id);
await _regionService.ProcessRegionAsync(request.Id, stoppingToken);
_logger.LogInformation("Worker {WorkerId}: Completed region {RegionId}",
workerId, request.Id);
}
}
catch (OperationCanceledException)
@@ -43,11 +77,11 @@ public class RegionProcessingService : BackgroundService
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing region request");
_logger.LogError(ex, "Worker {WorkerId}: Error processing region request", workerId);
}
}
_logger.LogInformation("Region Processing Service stopped");
_logger.LogInformation("Region worker {WorkerId} stopped", workerId);
}
}