Postgres (#30)
* Add postgres * Migrate users * Migrate geneticRequest * Try to fix Concurrent call * Fix asyncawait * Fix async and concurrent * Migrate backtests * Add cache for user by address * Fix backtest migration * Fix not open connection * Fix backtest command error * Fix concurrent * Fix all concurrency * Migrate TradingRepo * Fix scenarios * Migrate statistic repo * Save botbackup * Add settings et moneymanagement * Add bot postgres * fix a bit more backups * Fix bot model * Fix loading backup * Remove cache market for read positions * Add workers to postgre * Fix workers api * Reduce get Accounts for workers * Migrate synth to postgre * Fix backtest saved * Remove mongodb * botservice decorrelation * Fix tradingbot scope call * fix tradingbot * fix concurrent * Fix scope for genetics * Fix account over requesting * Fix bundle backtest worker * fix a lot of things * fix tab backtest * Remove optimized moneymanagement * Add light signal to not use User and too much property * Make money management lighter * insert indicators to awaitable * Migrate add strategies to await * Refactor scenario and indicator retrieval to use asynchronous methods throughout the application * add more async await * Add services * Fix and clean * Fix bot a bit * Fix bot and add message for cooldown * Remove fees * Add script to deploy db * Update dfeeploy script * fix script * Add idempotent script and backup * finish script migration * Fix did user and agent name on start bot
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Managing.Application.Abstractions.Services;
|
||||
using Managing.Application.Workers.Abstractions;
|
||||
using Managing.Core;
|
||||
using Managing.Domain.Backtests;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using static Managing.Common.Enums;
|
||||
|
||||
@@ -11,18 +12,15 @@ namespace Managing.Application.Workers;
|
||||
/// </summary>
|
||||
public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
{
|
||||
private readonly IGeneticService _geneticService;
|
||||
private readonly IBacktester _backtester;
|
||||
private readonly IServiceScopeFactory _scopeFactory;
|
||||
|
||||
public GeneticAlgorithmWorker(
|
||||
ILogger<GeneticAlgorithmWorker> logger,
|
||||
IWorkerService workerService,
|
||||
IGeneticService geneticService,
|
||||
IBacktester backtester)
|
||||
: base(WorkerType.GeneticAlgorithm, logger, TimeSpan.FromMinutes(5), workerService)
|
||||
IServiceProvider serviceProvider,
|
||||
IServiceScopeFactory scopeFactory)
|
||||
: base(WorkerType.GeneticAlgorithm, logger, TimeSpan.FromMinutes(5), serviceProvider)
|
||||
{
|
||||
_geneticService = geneticService;
|
||||
_backtester = backtester;
|
||||
_scopeFactory = scopeFactory;
|
||||
}
|
||||
|
||||
protected override async Task Run(CancellationToken cancellationToken)
|
||||
@@ -48,8 +46,9 @@ public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
{
|
||||
try
|
||||
{
|
||||
// Get pending genetic requests from the repository
|
||||
var pendingRequests = _geneticService.GetPendingGeneticRequests();
|
||||
// Get pending genetic requests from the repository using scoped service
|
||||
var pendingRequests = await ServiceScopeHelpers.WithScopedService<IGeneticService, IEnumerable<GeneticRequest>>(_scopeFactory,
|
||||
async geneticService => await geneticService.GetGeneticRequestsAsync(GeneticRequestStatus.Pending));
|
||||
|
||||
if (!pendingRequests.Any())
|
||||
{
|
||||
@@ -65,21 +64,24 @@ public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
{
|
||||
_logger.LogInformation("[GeneticAlgorithm] Processing request {RequestId}", request.RequestId);
|
||||
|
||||
// Update status to Running
|
||||
// Update status to Running using scoped service
|
||||
request.Status = GeneticRequestStatus.Running;
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
await ServiceScopeHelpers.WithScopedService<IGeneticService>(_scopeFactory,
|
||||
async geneticService => await geneticService.UpdateGeneticRequestAsync(request));
|
||||
|
||||
// Run genetic algorithm using the service
|
||||
var results = await _geneticService.RunGeneticAlgorithm(request, cancellationToken);
|
||||
var results = await ServiceScopeHelpers.WithScopedServices<IGeneticService, IBacktester, GeneticAlgorithmResult>(_scopeFactory,
|
||||
async (geneticService, backtester) => await geneticService.RunGeneticAlgorithm(request, cancellationToken));
|
||||
|
||||
// Update request with results
|
||||
// Update request with results using scoped service
|
||||
request.Status = GeneticRequestStatus.Completed;
|
||||
request.CompletedAt = DateTime.UtcNow;
|
||||
request.BestFitness = results.BestFitness;
|
||||
request.BestIndividual = results.BestIndividual;
|
||||
request.ProgressInfo = results.ProgressInfo;
|
||||
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
await ServiceScopeHelpers.WithScopedService<IGeneticService>(_scopeFactory,
|
||||
async geneticService => await geneticService.UpdateGeneticRequestAsync(request));
|
||||
|
||||
_logger.LogInformation("[GeneticAlgorithm] Successfully completed request {RequestId}", request.RequestId);
|
||||
}
|
||||
@@ -88,7 +90,8 @@ public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
request.Status = GeneticRequestStatus.Failed;
|
||||
request.ErrorMessage = ex.Message;
|
||||
request.CompletedAt = DateTime.UtcNow;
|
||||
_geneticService.UpdateGeneticRequest(request);
|
||||
await ServiceScopeHelpers.WithScopedService<IGeneticService>(_scopeFactory,
|
||||
async geneticService => await geneticService.UpdateGeneticRequestAsync(request));
|
||||
|
||||
_logger.LogError(ex, "[GeneticAlgorithm] Error processing request {RequestId}", request.RequestId);
|
||||
}
|
||||
@@ -100,6 +103,4 @@ public class GeneticAlgorithmWorker : BaseWorker<GeneticAlgorithmWorker>
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user