Update jobs

This commit is contained in:
2025-11-09 04:48:15 +07:00
parent 7e08e63dd1
commit 747bda2700
20 changed files with 2281 additions and 217 deletions

View File

@@ -0,0 +1,39 @@
# Use the official Microsoft .NET runtime as the base image (no ASP.NET needed for console worker)
FROM mcr.microsoft.com/dotnet/runtime:8.0 AS base
WORKDIR /app
# Use the official Microsoft .NET SDK image to build the code.
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# Copy project files for dependency restoration
COPY ["Managing.Workers/Managing.Workers.csproj", "Managing.Workers/"]
COPY ["Managing.Bootstrap/Managing.Bootstrap.csproj", "Managing.Bootstrap/"]
COPY ["Managing.Application/Managing.Application.csproj", "Managing.Application/"]
COPY ["Managing.Application.Abstractions/Managing.Application.Abstractions.csproj", "Managing.Application.Abstractions/"]
COPY ["Managing.Common/Managing.Common.csproj", "Managing.Common/"]
COPY ["Managing.Core/Managing.Core.csproj", "Managing.Core/"]
COPY ["Managing.Domain/Managing.Domain.csproj", "Managing.Domain/"]
COPY ["Managing.Infrastructure.Database/Managing.Infrastructure.Databases.csproj", "Managing.Infrastructure.Database/"]
COPY ["Managing.Infrastructure.Exchanges/Managing.Infrastructure.Exchanges.csproj", "Managing.Infrastructure.Exchanges/"]
COPY ["Managing.Infrastructure.Messengers/Managing.Infrastructure.Messengers.csproj", "Managing.Infrastructure.Messengers/"]
COPY ["Managing.Infrastructure.Storage/Managing.Infrastructure.Storage.csproj", "Managing.Infrastructure.Storage/"]
COPY ["Managing.Infrastructure.Web3/Managing.Infrastructure.Evm.csproj", "Managing.Infrastructure.Web3/"]
# Restore dependencies for all projects
RUN dotnet restore "Managing.Workers/Managing.Workers.csproj"
# Copy everything else and build
COPY . .
WORKDIR "/src/Managing.Workers"
RUN dotnet build "Managing.Workers.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Managing.Workers.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Managing.Workers.dll"]

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-Managing.Workers-ff3f3987-4da4-4140-9180-b84c9e07b25f</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.11"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.10"/>
<PackageReference Include="Sentry" Version="5.5.1"/>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Managing.Bootstrap\Managing.Bootstrap.csproj"/>
<ProjectReference Include="..\Managing.Application\Managing.Application.csproj"/>
<ProjectReference Include="..\Managing.Application.Abstractions\Managing.Application.Abstractions.csproj"/>
<ProjectReference Include="..\Managing.Infrastructure.Database\Managing.Infrastructure.Databases.csproj"/>
<ProjectReference Include="..\Managing.Infrastructure.Exchanges\Managing.Infrastructure.Exchanges.csproj"/>
<ProjectReference Include="..\Managing.Common\Managing.Common.csproj"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,295 @@
using Managing.Application.Workers;
using Managing.Bootstrap;
using Managing.Common;
using Managing.Infrastructure.Databases.InfluxDb.Models;
using Managing.Infrastructure.Databases.PostgreSql;
using Managing.Infrastructure.Databases.PostgreSql.Configurations;
using Microsoft.EntityFrameworkCore;
// Explicitly set the environment before creating the host builder
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")
?? Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT")
?? "Development";
// Log environment detection before host creation
var dotnetEnv = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
var aspnetcoreEnv = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine("🔧 Environment Configuration");
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine($" DOTNET_ENVIRONMENT: {dotnetEnv ?? "(not set)"}");
Console.WriteLine($" ASPNETCORE_ENVIRONMENT: {aspnetcoreEnv ?? "(not set)"}");
Console.WriteLine($" Selected Environment: {environment}");
Console.WriteLine("═══════════════════════════════════════════════════════════");
var host = Host.CreateDefaultBuilder(args)
.UseEnvironment(environment) // Explicitly set the environment
.ConfigureAppConfiguration((hostingContext, config) =>
{
var detectedEnv = hostingContext.HostingEnvironment.EnvironmentName;
Console.WriteLine($" Detected Environment: {detectedEnv}");
if (detectedEnv != environment)
{
Console.WriteLine($" ⚠️ WARNING: Environment mismatch! Expected: {environment}, Got: {detectedEnv}");
}
config.SetBasePath(AppContext.BaseDirectory);
// Load configuration files in order (later files override earlier ones)
// 1. Base appsettings.json (always loaded)
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Console.WriteLine(" ✓ Loaded: appsettings.json");
// 2. Load ONLY the environment-specific file (not other environments)
if (!string.IsNullOrEmpty(detectedEnv))
{
var envFile = $"appsettings.{detectedEnv}.json";
config.AddJsonFile(envFile, optional: true, reloadOnChange: true);
Console.WriteLine($" ✓ Loaded: {envFile} (optional)");
}
// 3. Environment variables and user secrets (highest priority)
config.AddEnvironmentVariables()
.AddUserSecrets<Program>();
Console.WriteLine("═══════════════════════════════════════════════════════════");
})
.ConfigureServices((hostContext, services) =>
{
var configuration = hostContext.Configuration;
// Initialize Sentry
SentrySdk.Init(options =>
{
options.Dsn = configuration["Sentry:Dsn"];
options.Debug = false;
options.SendDefaultPii = true;
options.AutoSessionTracking = true;
options.IsGlobalModeEnabled = false;
options.TracesSampleRate = 0.1;
options.Environment = hostContext.HostingEnvironment.EnvironmentName;
});
// Configure database
var postgreSqlConnectionString = configuration.GetSection(Constants.Databases.PostgreSql)["ConnectionString"];
// Log database connection details (mask password for security)
if (!string.IsNullOrEmpty(postgreSqlConnectionString))
{
var connectionParts = postgreSqlConnectionString.Split(';')
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => p.Trim())
.ToDictionary(
p => p.Split('=')[0].Trim(),
p => p.Contains('=') ? p.Substring(p.IndexOf('=') + 1).Trim() : string.Empty,
StringComparer.OrdinalIgnoreCase);
var host = connectionParts.GetValueOrDefault("Host", "unknown");
var port = connectionParts.GetValueOrDefault("Port", "unknown");
var database = connectionParts.GetValueOrDefault("Database", "unknown");
var username = connectionParts.GetValueOrDefault("Username", "unknown");
var maskedConnectionString = postgreSqlConnectionString
.Replace(connectionParts.GetValueOrDefault("Password", ""), "***", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine("📊 PostgreSQL Database Configuration");
Console.WriteLine("═══════════════════════════════════════════════════════════");
Console.WriteLine($" Host: {host}");
Console.WriteLine($" Port: {port}");
Console.WriteLine($" Database: {database}");
Console.WriteLine($" Username: {username}");
Console.WriteLine($" Connection String: {maskedConnectionString}");
Console.WriteLine("═══════════════════════════════════════════════════════════");
}
else
{
Console.WriteLine("⚠️ WARNING: PostgreSQL connection string is empty or not configured!");
}
services.Configure<PostgreSqlSettings>(configuration.GetSection(Constants.Databases.PostgreSql));
services.Configure<InfluxDbSettings>(configuration.GetSection(Constants.Databases.InfluxDb));
// Add DbContext
services.AddDbContext<ManagingDbContext>((serviceProvider, options) =>
{
options.UseNpgsql(postgreSqlConnectionString, npgsqlOptions =>
{
npgsqlOptions.CommandTimeout(60);
npgsqlOptions.EnableRetryOnFailure(maxRetryCount: 5, maxRetryDelay: TimeSpan.FromSeconds(10), errorCodesToAdd: null);
});
if (hostContext.HostingEnvironment.IsDevelopment())
{
options.EnableDetailedErrors();
options.EnableSensitiveDataLogging();
// SQL logging disabled - uncomment below line if needed for debugging
// options.LogTo(Console.WriteLine, LogLevel.Information);
}
options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
options.EnableServiceProviderCaching();
}, ServiceLifetime.Scoped);
// Register compute dependencies (no Orleans)
services.RegisterComputeDependencies(configuration);
// Configure BacktestComputeWorker options
services.Configure<BacktestComputeWorkerOptions>(
configuration.GetSection(BacktestComputeWorkerOptions.SectionName));
// Get task slot from CapRover ({{.Task.Slot}}) or environment variable
// This identifies which instance of the worker is running
var taskSlot = Environment.GetEnvironmentVariable("TASK_SLOT") ??
Environment.GetEnvironmentVariable("CAPROVER_TASK_SLOT") ??
"0";
// Override WorkerId from environment variable if provided, otherwise use task slot
var workerId = Environment.GetEnvironmentVariable("WORKER_ID") ??
configuration["BacktestComputeWorker:WorkerId"] ??
$"{Environment.MachineName}-{taskSlot}";
services.Configure<BacktestComputeWorkerOptions>(options =>
{
options.WorkerId = workerId;
});
// Configure GeneticComputeWorker options
services.Configure<GeneticComputeWorkerOptions>(
configuration.GetSection(GeneticComputeWorkerOptions.SectionName));
// Override Genetic WorkerId from environment variable if provided, otherwise use task slot
var geneticWorkerId = Environment.GetEnvironmentVariable("GENETIC_WORKER_ID") ??
configuration["GeneticComputeWorker:WorkerId"] ??
$"{Environment.MachineName}-genetic-{taskSlot}";
services.Configure<GeneticComputeWorkerOptions>(options =>
{
options.WorkerId = geneticWorkerId;
});
// Register the backtest compute worker if enabled
var isBacktestWorkerEnabled = configuration.GetValue<bool>("WorkerBacktestCompute", false);
if (isBacktestWorkerEnabled)
{
services.AddHostedService<BacktestComputeWorker>();
}
// Register the genetic compute worker if enabled
var isGeneticWorkerEnabled = configuration.GetValue<bool>("WorkerGeneticCompute", false);
if (isGeneticWorkerEnabled)
{
services.AddHostedService<GeneticComputeWorker>();
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.ClearProviders();
logging.AddConsole();
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
})
.Build();
// Log worker status
var logger = host.Services.GetRequiredService<ILogger<Program>>();
var config = host.Services.GetRequiredService<IConfiguration>();
// Log PostgreSQL connection details
var postgreSqlConnectionString = config.GetSection(Constants.Databases.PostgreSql)["ConnectionString"];
if (!string.IsNullOrEmpty(postgreSqlConnectionString))
{
var connectionParts = postgreSqlConnectionString.Split(';')
.Where(p => !string.IsNullOrWhiteSpace(p))
.Select(p => p.Trim())
.ToDictionary(
p => p.Split('=')[0].Trim(),
p => p.Contains('=') ? p.Substring(p.IndexOf('=') + 1).Trim() : string.Empty,
StringComparer.OrdinalIgnoreCase);
var hostName = connectionParts.GetValueOrDefault("Host", "unknown");
var port = connectionParts.GetValueOrDefault("Port", "unknown");
var database = connectionParts.GetValueOrDefault("Database", "unknown");
var username = connectionParts.GetValueOrDefault("Username", "unknown");
logger.LogInformation("═══════════════════════════════════════════════════════════");
logger.LogInformation("📊 PostgreSQL Database Configuration");
logger.LogInformation("═══════════════════════════════════════════════════════════");
logger.LogInformation(" Host: {Host}", hostName);
logger.LogInformation(" Port: {Port}", port);
logger.LogInformation(" Database: {Database}", database);
logger.LogInformation(" Username: {Username}", username);
logger.LogInformation("═══════════════════════════════════════════════════════════");
// Test database connection
try
{
using var scope = host.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<ManagingDbContext>();
var canConnect = await dbContext.Database.CanConnectAsync();
if (canConnect)
{
logger.LogInformation("✅ Database connection test: SUCCESS");
}
else
{
logger.LogWarning("⚠️ Database connection test: FAILED - Cannot connect to database");
}
}
catch (Exception ex)
{
logger.LogError(ex, "❌ Database connection test: FAILED - {Error}", ex.Message);
}
}
else
{
logger.LogWarning("⚠️ WARNING: PostgreSQL connection string is empty or not configured!");
}
var isBacktestWorkerEnabled = config.GetValue<bool>("WorkerBacktestCompute", false);
var isGeneticWorkerEnabled = config.GetValue<bool>("WorkerGeneticCompute", false);
if (isBacktestWorkerEnabled)
{
var taskSlot = Environment.GetEnvironmentVariable("TASK_SLOT") ??
Environment.GetEnvironmentVariable("CAPROVER_TASK_SLOT") ??
"0";
var backtestWorkerId = Environment.GetEnvironmentVariable("WORKER_ID") ??
config["BacktestComputeWorker:WorkerId"] ??
$"{Environment.MachineName}-{taskSlot}";
logger.LogInformation("BacktestComputeWorker is enabled and will be started.");
logger.LogInformation("Backtest Worker ID: {WorkerId} (Task Slot: {TaskSlot})", backtestWorkerId, taskSlot);
}
else
{
logger.LogWarning("BacktestComputeWorker is disabled via configuration. No backtest jobs will be processed.");
}
if (isGeneticWorkerEnabled)
{
var taskSlot = Environment.GetEnvironmentVariable("TASK_SLOT") ??
Environment.GetEnvironmentVariable("CAPROVER_TASK_SLOT") ??
"0";
var geneticWorkerId = Environment.GetEnvironmentVariable("GENETIC_WORKER_ID") ??
config["GeneticComputeWorker:WorkerId"] ??
$"{Environment.MachineName}-genetic-{taskSlot}";
logger.LogInformation("GeneticComputeWorker is enabled and will be started.");
logger.LogInformation("Genetic Worker ID: {WorkerId} (Task Slot: {TaskSlot})", geneticWorkerId, taskSlot);
}
else
{
logger.LogWarning("GeneticComputeWorker is disabled via configuration. No genetic jobs will be processed.");
}
try
{
await host.RunAsync();
}
catch (Exception ex)
{
logger.LogCritical(ex, "Application terminated unexpectedly");
SentrySdk.CaptureException(ex);
throw;
}
finally
{
SentrySdk.FlushAsync(TimeSpan.FromSeconds(2)).Wait();
}

View File

@@ -0,0 +1,24 @@
namespace Managing.Workers;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
if (_logger.IsEnabled(LogLevel.Information))
{
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
}
await Task.Delay(1000, stoppingToken);
}
}
}

View File

@@ -0,0 +1,34 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WorkerBacktestCompute": true,
"BacktestComputeWorker": {
"WorkerId": "Oda-backtest-0",
"MaxConcurrentBacktests": 6,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 5
},
"WorkerGeneticCompute": true,
"GeneticComputeWorker": {
"MaxConcurrentGenetics": 2,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 10
},
"PostgreSql": {
"ConnectionString": "Host=localhost;Port=5432;Database=managing;Username=postgres;Password=postgres"
},
"InfluxDb": {
"Url": "http://localhost:8086/",
"Organization": "managing-org",
"Token": "Fw2FPL2OwTzDHzSbR2Sd5xs0EKQYy00Q-hYKYAhr9cC1_q5YySONpxuf_Ck0PTjyUiF13xXmi__bu_pXH-H9zA=="
},
"Sentry": {
"Dsn": ""
}
}

View File

@@ -0,0 +1,45 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WorkerBacktestCompute": true,
"BacktestComputeWorker": {
"MaxConcurrentBacktests": 6,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 5
},
"WorkerGeneticCompute": true,
"GeneticComputeWorker": {
"MaxConcurrentGenetics": 2,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 10
},
"PostgreSql": {
"ConnectionString": "Host=kaigen-db.kaigen.managing.live;Port=5432;Database=managing;Username=postgres;Password=2ab5423dcca4aa2d"
},
"InfluxDb": {
"Url": "https://influx-db.kaigen.managing.live",
"Organization": "managing-org",
"Token": "ROvQoZ1Dg5jiKDFxB0saEGqHC3rsLkUNlPL6_AFbOcpNjMieIv8v58yA4v5tFU9sX9LLvXEToPvUrxqQEMaWDw=="
},
"Sentry": {
"Dsn": "https://fe12add48c56419bbdfa86227c188e7a@glitch.kai.managing.live/1"
},
"N8n": {
"WebhookUrl": "https://n8n.kai.managing.live/webhook/fa9308b6-983b-42ec-b085-71599d655951",
"IndicatorRequestWebhookUrl": "https://n8n.kai.managing.live/webhook/3aa07b66-1e64-46a7-8618-af300914cb11",
"Username": "managing-api",
"Password": "T259836*PdiV2@%!eR%Qf4"
},
"Kaigen": {
"BaseUrl": "https://kaigen-back-kaigen-stage.up.railway.app",
"DebitEndpoint": "/api/credits/debit",
"RefundEndpoint": "/api/credits/refund"
}
}

View File

@@ -0,0 +1,45 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WorkerBacktestCompute": true,
"BacktestComputeWorker": {
"MaxConcurrentBacktests": 6,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 5
},
"WorkerGeneticCompute": true,
"GeneticComputeWorker": {
"MaxConcurrentGenetics": 2,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 10
},
"PostgreSql": {
"ConnectionString": "Host=managing-postgre.apps.managing.live;Port=5432;Database=managing;Username=postgres;Password=29032b13a5bc4d37"
},
"InfluxDb": {
"Url": "https://influx-db.apps.managing.live",
"Organization": "managing-org",
"Token": "zODh8Hn8sN5VwpVJH0HAwDpCJPE4oB5IUg8L4Q0T67KM1Rta6PoM0nATUzf1ddkyWx_VledooZXfFIddahbL9Q=="
},
"Sentry": {
"Dsn": "https://fe12add48c56419bbdfa86227c188e7a@glitch.kai.managing.live/1"
},
"N8n": {
"WebhookUrl": "https://n8n.kai.managing.live/webhook/fa9308b6-983b-42ec-b085-71599d655951",
"IndicatorRequestWebhookUrl": "https://n8n.kai.managing.live/webhook/3aa07b66-1e64-46a7-8618-af300914cb11",
"Username": "managing-api",
"Password": "T259836*PdiV2@%!eR%Qf4"
},
"Kaigen": {
"BaseUrl": "https://kaigen-back-kaigen-stage.up.railway.app",
"DebitEndpoint": "/api/credits/debit",
"RefundEndpoint": "/api/credits/refund"
}
}

View File

@@ -0,0 +1,36 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WorkerBacktestCompute": true,
"BacktestComputeWorker": {
"MaxConcurrentBacktests": 6,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 5
},
"WorkerGeneticCompute": true,
"GeneticComputeWorker": {
"MaxConcurrentGenetics": 2,
"JobPollIntervalSeconds": 5,
"HeartbeatIntervalSeconds": 30,
"StaleJobTimeoutMinutes": 10
},
"Sentry": {
"Dsn": "https://fe12add48c56419bbdfa86227c188e7a@glitch.kai.managing.live/1"
},
"N8n": {
"WebhookUrl": "https://n8n.kai.managing.live/webhook/fa9308b6-983b-42ec-b085-71599d655951",
"IndicatorRequestWebhookUrl": "https://n8n.kai.managing.live/webhook/3aa07b66-1e64-46a7-8618-af300914cb11",
"Username": "managing-api",
"Password": "T259836*PdiV2@%!eR%Qf4"
},
"Kaigen": {
"BaseUrl": "https://kaigen-back-kaigen-stage.up.railway.app",
"DebitEndpoint": "/api/credits/debit",
"RefundEndpoint": "/api/credits/refund"
}
}

View File

@@ -0,0 +1,5 @@
{
"schemaVersion": 2,
"dockerfilePath": "../Dockerfile-worker-api-dev"
}