Add Genetic workers
This commit is contained in:
131
src/Managing.Application.Workers/GeneticAlgorithmWorker.cs
Normal file
131
src/Managing.Application.Workers/GeneticAlgorithmWorker.cs
Normal file
@@ -0,0 +1,131 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Domain.Backtests;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
namespace Managing.Application.Workers;
|
||||
|
||||
/// <summary>
|
||||
/// Worker that processes genetic algorithm requests
|
||||
/// </summary>
|
||||
public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
{
|
||||
private readonly IGeneticService _geneticService;
|
||||
private readonly IBacktester _backtester;
|
||||
|
||||
public GeneticAlgorithmWorker(
|
||||
ILogger<GeneticAlgorithmWorker> logger,
|
||||
IWorkerService workerService,
|
||||
IGeneticService geneticService,
|
||||
IBacktester backtester)
|
||||
: base(WorkerType.GeneticAlgorithm, logger, TimeSpan.FromMinutes(5), workerService)
|
||||
{
|
||||
_geneticService = geneticService;
|
||||
_backtester = backtester;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("[GeneticAlgorithm] Starting genetic algorithm processing");
|
||||
|
||||
// TODO: Implement method to get pending genetic requests
|
||||
// For now, we'll create a placeholder for the genetic algorithm logic
|
||||
await ProcessPendingGeneticRequests(cancellationToken);
|
||||
|
||||
_logger.LogInformation("[GeneticAlgorithm] Completed genetic algorithm processing");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[GeneticAlgorithm] Error during genetic algorithm processing");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ProcessPendingGeneticRequests(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get pending genetic requests from the repository
|
||||
var pendingRequests = _geneticService.GetPendingGeneticRequests();
|
||||
|
||||
if (!pendingRequests.Any())
|
||||
{
|
||||
_logger.LogInformation("[GeneticAlgorithm] No pending genetic requests found");
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.LogInformation("[GeneticAlgorithm] Found {Count} pending genetic requests", pendingRequests.Count());
|
||||
|
||||
foreach (var request in pendingRequests)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation("[GeneticAlgorithm] Processing request {RequestId}", request.RequestId);
|
||||
|
||||
// Update status to Running
|
||||
request.Status = GeneticRequestStatus.Running;
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
|
||||
// Run genetic algorithm
|
||||
var results = await RunGeneticAlgorithm(request, cancellationToken);
|
||||
|
||||
// Update request with results
|
||||
request.Status = GeneticRequestStatus.Completed;
|
||||
request.CompletedAt = DateTime.UtcNow;
|
||||
request.BestFitness = results.BestFitness;
|
||||
request.BestIndividual = results.BestIndividual;
|
||||
request.ProgressInfo = results.ProgressInfo;
|
||||
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
|
||||
_logger.LogInformation("[GeneticAlgorithm] Successfully completed request {RequestId}", request.RequestId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
request.Status = GeneticRequestStatus.Failed;
|
||||
request.ErrorMessage = ex.Message;
|
||||
request.CompletedAt = DateTime.UtcNow;
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
|
||||
_logger.LogError(ex, "[GeneticAlgorithm] Error processing request {RequestId}", request.RequestId);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "[GeneticAlgorithm] Error retrieving pending genetic requests");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<GeneticAlgorithmResult> RunGeneticAlgorithm(GeneticRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
// TODO: Implement the actual genetic algorithm
|
||||
// This is where the genetic algorithm logic will be implemented
|
||||
|
||||
_logger.LogInformation("[GeneticAlgorithm] Placeholder: Would run genetic algorithm for request {RequestId}", request.RequestId);
|
||||
|
||||
// Simulate some processing time
|
||||
await Task.Delay(1000, cancellationToken);
|
||||
|
||||
return new GeneticAlgorithmResult
|
||||
{
|
||||
BestFitness = 0.85,
|
||||
BestIndividual = "placeholder_individual",
|
||||
ProgressInfo = "{\"generation\": 10, \"best_fitness\": 0.85}"
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Result of a genetic algorithm run
|
||||
/// </summary>
|
||||
public class GeneticAlgorithmResult
|
||||
{
|
||||
public double BestFitness { get; set; }
|
||||
public string BestIndividual { get; set; } = string.Empty;
|
||||
public string ProgressInfo { get; set; } = string.Empty;
|
||||
}
|
||||
Reference in New Issue
Block a user