feat(core,dal,domain,snapshotWorker): Воркер по управлению снапшотами. Создание сгапшотов для таблицы RobotConfigurations

This commit is contained in:
Mikhail Trubnikov
2026-07-13 15:29:48 +10:00
parent 9194d127d2
commit 50556ef605
28 changed files with 4953 additions and 26 deletions

View File

@@ -20,6 +20,7 @@ variables:
PROD_TEMPLATE_UPDATER: "parr/parr-template-updater"
PROD_WORKLOAD_BUILDER: "parr/parr-workload-builder"
PROD_TASK_RECONCILIATION: "parr/parr-task-reconciliation"
PROD_SNAPSHOTS: "parr/snapshots"
stages:
@@ -1010,3 +1011,62 @@ prod_task_reconciliation_deploy:
- RUNNER: shell-api-swarm-01
tags:
- ${RUNNER}
### SNAPSHOTS PROD ###
prod_snapshots_build:
stage: build
only:
- /^sn[0-9]+\.[0-9]+\.[0-9]+$/
except:
- branches
services:
- name: docker:20.10.21-dind
command: [
"--insecure-registry=10.99.253.167:8090",
"--registry-mirror=http://10.99.253.167:8090",
"--insecure-registry=10.99.253.167:8088",
"--registry-mirror=http://10.99.253.167:8088",
"--insecure-registry=harbor.dvgd.rzd",
"--tls=false"
]
variables:
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay2
DOCKER_TLS_CERTDIR: ""
script:
- IMAGE_VERSION=$(echo $CI_COMMIT_TAG | tr -d sn)
- AUTHOR=$CI_COMMIT_AUTHOR
- |
docker build \
-t $REPO/$PROD_SNAPSHOTS:$CI_COMMIT_TAG \
-t $REPO/$PROD_SNAPSHOTS:latest \
-t $PROD_SNAPSHOTS:$CI_COMMIT_TAG \
-t $PROD_SNAPSHOTS:latest \
--build-arg app_version=$IMAGE_VERSION \
--build-arg commit_author="$AUTHOR" \
-f PARR.API/Dockerfile .
- docker login -u $HARBOR_PUSH_USER -p $HARBOR_PUSH_PASS $REPO
- docker push --all-tags $REPO/$PROD_SNAPSHOTS
tags:
- docker
prod_snapshots_deploy:
stage: deploy
environment:
name: prod
url: http://10.99.253.216:8082/api/v1/version
only:
- /^sn[0-9]+\.[0-9]+\.[0-9]+$/
except:
- branches
script:
- docker login -u $HARBOR_PULL_USER -p $HARBOR_PULL_PASS $REPO
#- tag=$CI_COMMIT_TAG docker compose up -d
- tag=$CI_COMMIT_TAG docker stack deploy -c docker-compose.snapshots.yml parr-snapshots --with-registry-auth
parallel:
matrix:
- RUNNER: shell-api-swarm-01
tags:
- ${RUNNER}

View File

@@ -99,6 +99,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.WorkloadBuilderWorker"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.TaskReconciliationWorker", "PARR.TaskReconciliationWorker\PARR.TaskReconciliationWorker.csproj", "{DB701295-1696-4E1A-9088-D3AECF823BA6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PARR.SnapshotWorker", "PARR.SnapshotWorker\PARR.SnapshotWorker.csproj", "{F5318808-942F-4EFB-9BC9-00B9F4704EB5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -275,6 +277,10 @@ Global
{DB701295-1696-4E1A-9088-D3AECF823BA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB701295-1696-4E1A-9088-D3AECF823BA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB701295-1696-4E1A-9088-D3AECF823BA6}.Release|Any CPU.Build.0 = Release|Any CPU
{F5318808-942F-4EFB-9BC9-00B9F4704EB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F5318808-942F-4EFB-9BC9-00B9F4704EB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F5318808-942F-4EFB-9BC9-00B9F4704EB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F5318808-942F-4EFB-9BC9-00B9F4704EB5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -1,6 +1,7 @@
using FluentValidation;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using PARR.Core.Common.Helpers;
using PARR.Core.Common.Implementations;
using PARR.Core.Common.Interfaces;
@@ -12,6 +13,8 @@ using PARR.Core.Services.RobotTask.Implementations;
using PARR.Core.Services.RobotTask.Interfaces;
using PARR.Core.Services.Shortcodes;
using PARR.Core.Services.Shortcodes.Handlers;
using PARR.Core.Services.Snapshots.Implementations;
using PARR.Core.Services.Snapshots.Interfaces;
using PARR.Core.Services.TaskServices.Handlers;
using PARR.Core.Services.TaskServices.Handlers.Factory;
using PARR.Core.Services.TaskServices.Implementations;
@@ -25,6 +28,7 @@ using PARR.Core.Services.UnitService.Implementations;
using PARR.Core.Services.UnitService.Interfaces;
using PARR.Core.Services.Workload.Implementations;
using PARR.Core.Services.Workload.Interfaces;
using PARR.Domain.Entities.RobotEntities;
using PARR.Domain.Enums;
using PARR.Domain.Settings;
@@ -115,6 +119,16 @@ namespace PARR.Core
#endregion
#region Сервисы сбора снапшотов
services.TryAddSingleton<ISnapshotSettings, DefaultSnapshotSettings>();
services.AddScoped<ISnapshotProvider, RobotConfigurationSnapshotService>();
services.AddScoped<ISnapshotProvider, RobotSnapshotService>();
// тут другие сервисы, реализующие ISnapshotProvider
#endregion
#region Workload
services.AddScoped<WorkloadCacheService>();

View File

@@ -0,0 +1,9 @@
using PARR.Core.Repositories.Base;
using PARR.Domain.Entities.RobotEntities;
namespace PARR.Core.Repositories.Interfaces.RobotRepositories
{
public interface IRobotConfigurationSnapshotRepository : IBaseRepository<RobotConfigurationSnapshot>
{
}
}

View File

@@ -1,7 +1,9 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.RobotRepositories;
using PARR.Core.Services.Snapshots.Interfaces;
using PARR.Domain.DTOs.RobotSnapshotDTO;
using PARR.Domain.DTOs.User;
using PARR.Domain.Entities.RobotEntities;
@@ -9,21 +11,32 @@ using PARR.Domain.Exceptions;
namespace PARR.Core.Services.RobotSnapshotServices
{
internal class RobotSnapshotService : IRobotSnapshotService
internal class RobotSnapshotService : IRobotSnapshotService, ISnapshotProvider
{
private readonly IRobotSnapshotRepository _robotSnapshotRepository;
private readonly IUserRepository _userRepository;
private readonly IMapper _mapper;
private readonly ILogger<RobotSnapshotService> _logger;
private readonly ISnapshotSettings _snapshotSettings;
// Тут любое значение, не используем метод создания снапшота
public TimeSpan Interval => TimeSpan.FromHours(1);
public TimeSpan RetentionPeriod => _snapshotSettings.RobotSnapshotRetentionPeriod;
public RobotSnapshotService(
IRobotSnapshotRepository robotSnapshotRepository,
IUserRepository userRepository,
IMapper mapper
IMapper mapper,
ILogger<RobotSnapshotService> logger,
ISnapshotSettings snapshotSettings
)
{
_robotSnapshotRepository = robotSnapshotRepository;
_userRepository = userRepository;
_mapper = mapper;
_logger = logger;
_snapshotSettings = snapshotSettings;
}
@@ -348,5 +361,26 @@ namespace PARR.Core.Services.RobotSnapshotServices
}
public Task TakeSnapshotAsync(CancellationToken cancellationToken)
{
// Метод пустой! Нам не нужно собирать данные по таймеру,
// так как они и так пишутся сюда через контроллер API.
return Task.CompletedTask;
}
public async Task CleanUpOldSnapshotsAsync(CancellationToken cancellationToken)
{
var thresholdDate = DateTimeOffset.UtcNow - RetentionPeriod;
_logger.LogInformation("[{ServiceName}] Запуск очистки старых снапшотов. Удаление данных старше {ThresholdDate}", GetType().Name, thresholdDate);
// Удаляем старые записи напрямую в PostgreSQL
var deletedCount = await _robotSnapshotRepository.Get()
.Where(s => s.DateCreated < thresholdDate)
.ExecuteDeleteAsync(cancellationToken);
_logger.LogInformation("[{ServiceName}] Очистка завершена. Удалено устаревших строк снапшотов: {Count}", GetType().Name, deletedCount);
}
}
}

View File

@@ -0,0 +1,94 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces;
using PARR.Core.Repositories.Interfaces.RobotRepositories;
using PARR.Core.Services.Snapshots.Interfaces;
using PARR.Domain.Entities.RobotEntities;
using PARR.Domain.Exceptions;
namespace PARR.Core.Services.Snapshots.Implementations
{
/// <summary>
/// Снапшоты для таблицы RobotConfiguration
/// </summary>
internal class RobotConfigurationSnapshotService : ISnapshotProvider
{
private readonly IRobotConfigurationRepository _robotConfigurationRepository;
private readonly IRobotConfigurationSnapshotRepository _robotConfigurationSnapshotRepository;
private readonly ILogger<RobotConfigurationSnapshotService> _logger;
private readonly ISnapshotSettings _snapshotSettings;
public TimeSpan Interval => _snapshotSettings.RobotConfigurationSnapshotInterval;// TimeSpan.FromMinutes(2);
public TimeSpan RetentionPeriod => _snapshotSettings.RobotConfigurationSnapshotRetentionPeriod;//TimeSpan.FromDays(60);
public RobotConfigurationSnapshotService(
IRobotConfigurationRepository robotConfigurationRepository,
IRobotConfigurationSnapshotRepository robotConfigurationSnapshotRepository,
ILogger<RobotConfigurationSnapshotService> logger,
ISnapshotSettings snapshotSettings
)
{
_robotConfigurationRepository = robotConfigurationRepository;
_robotConfigurationSnapshotRepository = robotConfigurationSnapshotRepository;
_logger = logger;
_snapshotSettings = snapshotSettings;
}
public async Task TakeSnapshotAsync(CancellationToken cancellationToken)
{
var now = DateTimeOffset.UtcNow;
_logger.LogDebug("Сбор метрик для снапшота RobotConfigurations...");
var stats = await _robotConfigurationRepository.Get()
.GroupBy(t => new { t.RobotCode, t.RobotStatusCode, t.TaskStatusCode })
.Select(g => new
{
g.Key.RobotCode,
g.Key.RobotStatusCode,
g.Key.TaskStatusCode,
Count = g.Count()
})
.ToListAsync(cancellationToken);
if (!stats.Any())
{
_logger.LogDebug("Нет активных заданий роботов для создания снапшота.");
return;
}
var snapshots = stats.Select(s => new RobotConfigurationSnapshot
{
Id = Guid.NewGuid(),
DateCreated = now,
RobotCode = s.RobotCode,
RobotStatusCode = s.RobotStatusCode,
TaskStatusCode = s.TaskStatusCode,
Count = s.Count
}).ToList();
if (!await _robotConfigurationSnapshotRepository.AddRangeAsync(snapshots) || !await _robotConfigurationSnapshotRepository.CommitAsync())
throw new DbErrorException("Ошибка при сохранении в БД");
_logger.LogInformation("Успешно сохранен снапшот RobotConfigurations. Записано строк: {Count}. Периодичность: {Interval}", snapshots.Count, Interval);
}
public async Task CleanUpOldSnapshotsAsync(CancellationToken cancellationToken)
{
// Вычисляем граничную дату (всё, что было ДО нее — удаляем)
var thresholdDate = DateTimeOffset.UtcNow.Subtract(RetentionPeriod);
_logger.LogInformation("[{ServiceName}] Запуск очистки старых снапшотов. Удаление данных старше {ThresholdDate}", GetType().Name, thresholdDate);
// Фильтруем старые записи и вызываем ExecuteDeleteAsync для удаления прямо в базе данных
var deletedCount = await _robotConfigurationSnapshotRepository.Get()
.Where(s => s.DateCreated < thresholdDate)
.ExecuteDeleteAsync(cancellationToken);
_logger.LogInformation("[{ServiceName}] Очистка завершена. Удалено устаревших строк снапшотов: {Count}", GetType().Name, deletedCount);
}
}
}

View File

@@ -0,0 +1,31 @@
namespace PARR.Core.Services.Snapshots.Interfaces
{
/// <summary>
/// Общий снапшот провайдер.
/// Используется для реализации паттерна "Стратегия"
/// </summary>
public interface ISnapshotProvider
{
/// <summary>
/// Периодичность создания снапшота (не меньше 1 минуты).
/// </summary>
TimeSpan Interval { get; }
/// <summary>
/// Срок хранения снапшотов (например, 30 дней). Всё, что старше — удаляется.
/// </summary>
TimeSpan RetentionPeriod { get; }
/// <summary>
/// Метод сбора метрик и записи их в базу данных.
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task TakeSnapshotAsync(CancellationToken cancellationToken);
/// <summary>
/// Метод очистки устаревших снапшотов.
/// </summary>
Task CleanUpOldSnapshotsAsync(CancellationToken cancellationToken);
}
}

View File

@@ -0,0 +1,35 @@
namespace PARR.Core.Services.Snapshots.Interfaces
{
/// <summary>
/// Настройки снапшотов
/// </summary>
public interface ISnapshotSettings
{
/// <summary>
/// Интервал создания снапшотов для таблицы RobotConfiguration
/// </summary>
TimeSpan RobotConfigurationSnapshotInterval { get; }
/// <summary>
/// Длительность хранения снапшотов для RobotConfiguration в таблице ConfigurationShapshots
/// </summary>
TimeSpan RobotConfigurationSnapshotRetentionPeriod { get; }
/// <summary>
/// Длительность хранения снапшотов работы роботов в таблице Shapshots
/// </summary>
TimeSpan RobotSnapshotRetentionPeriod { get; }
}
/// <summary>
/// Настройки по умолчанию для ISnapshotSettings
/// </summary>
internal record DefaultSnapshotSettings : ISnapshotSettings
{
public TimeSpan RobotConfigurationSnapshotInterval => TimeSpan.FromMinutes(2);
public TimeSpan RobotConfigurationSnapshotRetentionPeriod => TimeSpan.FromDays(60);
public TimeSpan RobotSnapshotRetentionPeriod => TimeSpan.FromDays(60);
}
}

View File

@@ -143,6 +143,7 @@ namespace PARR.DAL.Context
#region Robot
public DbSet<RobotSnapshot> RobotSnapshots { get; set; }
public DbSet<RobotConfigurationSnapshot> RobotConfigurationSnapshots { get; set; }
#endregion

View File

@@ -84,8 +84,12 @@ namespace PARR.DAL
services.AddTransient<IParrComponentRepository, ParrComponentRepository>();
services.AddTransient<ITemplateStatusTypeRepository, TemplateStatusTypeRepository>();
services.AddScoped<IRobotSnapshotRepository, RobotSnapshotRepository>();
#region Robot
services.AddScoped<IRobotSnapshotRepository, RobotSnapshotRepository>();
services.AddScoped<IRobotConfigurationSnapshotRepository, RobotConfigurationSnapshotRepository>();
#endregion
#region Schedule

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,77 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PARR.DAL.Migrations
{
/// <inheritdoc />
public partial class tblRobotConfigurationSnapshots : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "ConfigurationSnapshots",
schema: "robot",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
DateCreated = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
RobotCode = table.Column<int>(type: "integer", nullable: false),
RobotStatusCode = table.Column<int>(type: "integer", nullable: false),
TaskStatusCode = table.Column<int>(type: "integer", nullable: false),
Count = table.Column<int>(type: "integer", nullable: false, comment: "Количество заданий в этой комбинации статусов")
},
constraints: table =>
{
table.PrimaryKey("PK_ConfigurationSnapshots", x => x.Id);
table.ForeignKey(
name: "FK_ConfigurationSnapshots_RobotStatuses_RobotStatusCode",
column: x => x.RobotStatusCode,
principalTable: "RobotStatuses",
principalColumn: "Code",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ConfigurationSnapshots_Robots_RobotCode",
column: x => x.RobotCode,
principalTable: "Robots",
principalColumn: "Code",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ConfigurationSnapshots_TaskStatuses_TaskStatusCode",
column: x => x.TaskStatusCode,
principalTable: "TaskStatuses",
principalColumn: "Code",
onDelete: ReferentialAction.Cascade);
},
comment: "Снимки заданий роботам");
migrationBuilder.CreateIndex(
name: "IX_ConfigurationSnapshots_RobotCode_DateCreated",
schema: "robot",
table: "ConfigurationSnapshots",
columns: new[] { "RobotCode", "DateCreated" });
migrationBuilder.CreateIndex(
name: "IX_ConfigurationSnapshots_RobotStatusCode",
schema: "robot",
table: "ConfigurationSnapshots",
column: "RobotStatusCode");
migrationBuilder.CreateIndex(
name: "IX_ConfigurationSnapshots_TaskStatusCode",
schema: "robot",
table: "ConfigurationSnapshots",
column: "TaskStatusCode");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ConfigurationSnapshots",
schema: "robot");
}
}
}

View File

@@ -175,7 +175,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.Job", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.Job", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -234,7 +234,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobAutoControl", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobAutoControl", b =>
{
b.Property<Guid>("JobId")
.HasColumnType("uuid");
@@ -256,7 +256,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobFieldFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobFieldFilter", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -290,7 +290,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobRelationshipFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobRelationshipFilter", b =>
{
b.Property<Guid>("UnitFilterId")
.HasColumnType("uuid");
@@ -321,7 +321,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobUnitFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobUnitFilter", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
@@ -347,7 +347,7 @@ namespace PARR.DAL.Migrations
});
});
modelBuilder.Entity("PARR.Domain.Entities.Job.UnitsInTemplate", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.UnitsInTemplate", b =>
{
b.Property<Guid>("TemplateId")
.HasColumnType("uuid");
@@ -1004,6 +1004,42 @@ namespace PARR.DAL.Migrations
b.ToTable("RobotConfigurations");
});
modelBuilder.Entity("PARR.Domain.Entities.RobotEntities.RobotConfigurationSnapshot", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");
b.Property<int>("Count")
.HasColumnType("integer")
.HasComment("Количество заданий в этой комбинации статусов");
b.Property<DateTimeOffset>("DateCreated")
.HasColumnType("timestamp with time zone");
b.Property<int>("RobotCode")
.HasColumnType("integer");
b.Property<int>("RobotStatusCode")
.HasColumnType("integer");
b.Property<int>("TaskStatusCode")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("RobotStatusCode");
b.HasIndex("TaskStatusCode");
b.HasIndex("RobotCode", "DateCreated");
b.ToTable("ConfigurationSnapshots", "robot", t =>
{
t.HasComment("Снимки заданий роботам");
});
});
modelBuilder.Entity("PARR.Domain.Entities.RobotEntities.RobotSnapshot", b =>
{
b.Property<Guid>("Id")
@@ -3232,7 +3268,7 @@ namespace PARR.DAL.Migrations
b.Navigation("DistributionPeriodType");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.Job", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.Job", b =>
{
b.HasOne("PARR.Domain.Entities.JobGroupEntities.JobGroup", "Group")
.WithMany("Jobs")
@@ -3251,18 +3287,18 @@ namespace PARR.DAL.Migrations
b.Navigation("Tnk");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobAutoControl", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobAutoControl", b =>
{
b.HasOne("PARR.Domain.Entities.Job.Job", "Job")
b.HasOne("PARR.Domain.Entities.JobEntities.Job", "Job")
.WithOne("AutoControl")
.HasForeignKey("PARR.Domain.Entities.Job.JobAutoControl", "JobId")
.HasForeignKey("PARR.Domain.Entities.JobEntities.JobAutoControl", "JobId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Job");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobFieldFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobFieldFilter", b =>
{
b.HasOne("PARR.Domain.Entities.Unit.UnitField", "UnitField")
.WithMany("JobFieldFilters")
@@ -3270,7 +3306,7 @@ namespace PARR.DAL.Migrations
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PARR.Domain.Entities.Job.JobUnitFilter", "UnitFilter")
b.HasOne("PARR.Domain.Entities.JobEntities.JobUnitFilter", "UnitFilter")
.WithMany("FieldFilters")
.HasForeignKey("UnitFilterId")
.OnDelete(DeleteBehavior.Cascade)
@@ -3281,7 +3317,7 @@ namespace PARR.DAL.Migrations
b.Navigation("UnitFilter");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobRelationshipFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobRelationshipFilter", b =>
{
b.HasOne("PARR.Domain.Entities.Unit.UnitField", "UnitField")
.WithMany("RelationshipFilters")
@@ -3289,7 +3325,7 @@ namespace PARR.DAL.Migrations
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PARR.Domain.Entities.Job.JobUnitFilter", "UnitFilter")
b.HasOne("PARR.Domain.Entities.JobEntities.JobUnitFilter", "UnitFilter")
.WithMany("RelationshipFilters")
.HasForeignKey("UnitFilterId")
.OnDelete(DeleteBehavior.Cascade)
@@ -3300,9 +3336,9 @@ namespace PARR.DAL.Migrations
b.Navigation("UnitFilter");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobUnitFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobUnitFilter", b =>
{
b.HasOne("PARR.Domain.Entities.Job.Job", "Job")
b.HasOne("PARR.Domain.Entities.JobEntities.Job", "Job")
.WithMany("UnitFilters")
.HasForeignKey("JobId")
.OnDelete(DeleteBehavior.Cascade)
@@ -3311,7 +3347,7 @@ namespace PARR.DAL.Migrations
b.Navigation("Job");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.UnitsInTemplate", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.UnitsInTemplate", b =>
{
b.HasOne("PARR.Domain.Entities.Template", "Template")
.WithMany("UnitsInTemplate")
@@ -3507,6 +3543,33 @@ namespace PARR.DAL.Migrations
b.Navigation("Template");
});
modelBuilder.Entity("PARR.Domain.Entities.RobotEntities.RobotConfigurationSnapshot", b =>
{
b.HasOne("PARR.Domain.Entities.Robot", "Robot")
.WithMany("ConfigurationSnapshots")
.HasForeignKey("RobotCode")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PARR.Domain.Entities.RobotStatus", "RobotStatus")
.WithMany("ConfigurationSnapshots")
.HasForeignKey("RobotStatusCode")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PARR.Domain.Entities.TaskStatus", "TaskStatus")
.WithMany("ConfigurationSnapshots")
.HasForeignKey("TaskStatusCode")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Robot");
b.Navigation("RobotStatus");
b.Navigation("TaskStatus");
});
modelBuilder.Entity("PARR.Domain.Entities.RobotHistory", b =>
{
b.HasOne("PARR.Domain.Entities.RobotHistoryLevel", "RobotHistoryLevel")
@@ -3634,7 +3697,7 @@ namespace PARR.DAL.Migrations
modelBuilder.Entity("PARR.Domain.Entities.Template", b =>
{
b.HasOne("PARR.Domain.Entities.Job.Job", "Job")
b.HasOne("PARR.Domain.Entities.JobEntities.Job", "Job")
.WithMany("Templates")
.HasForeignKey("JobId")
.OnDelete(DeleteBehavior.Cascade)
@@ -3802,7 +3865,7 @@ namespace PARR.DAL.Migrations
b.Navigation("Periods");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.Job", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.Job", b =>
{
b.Navigation("AutoControl");
@@ -3811,7 +3874,7 @@ namespace PARR.DAL.Migrations
b.Navigation("UnitFilters");
});
modelBuilder.Entity("PARR.Domain.Entities.Job.JobUnitFilter", b =>
modelBuilder.Entity("PARR.Domain.Entities.JobEntities.JobUnitFilter", b =>
{
b.Navigation("FieldFilters");
@@ -3860,6 +3923,8 @@ namespace PARR.DAL.Migrations
modelBuilder.Entity("PARR.Domain.Entities.Robot", b =>
{
b.Navigation("ConfigurationSnapshots");
b.Navigation("RobotConfigurations");
});
@@ -3875,6 +3940,8 @@ namespace PARR.DAL.Migrations
modelBuilder.Entity("PARR.Domain.Entities.RobotStatus", b =>
{
b.Navigation("ConfigurationSnapshots");
b.Navigation("RobotConfigurations");
});
@@ -3937,6 +4004,8 @@ namespace PARR.DAL.Migrations
modelBuilder.Entity("PARR.Domain.Entities.TaskStatus", b =>
{
b.Navigation("ConfigurationSnapshots");
b.Navigation("RobotConfigurations");
b.Navigation("RobotHistories");

View File

@@ -0,0 +1,13 @@
using Microsoft.Extensions.Logging;
using PARR.Core.Repositories.Interfaces.RobotRepositories;
using PARR.DAL.Context;
using PARR.DAL.Repositories.Base;
using PARR.Domain.Entities.RobotEntities;
namespace PARR.DAL.Repositories.RobotRepositories
{
internal class RobotConfigurationSnapshotRepository : BaseRepository<RobotConfigurationSnapshot>, IRobotConfigurationSnapshotRepository
{
public RobotConfigurationSnapshotRepository(ILogger<RobotConfigurationSnapshotRepository> logger, DataContext dataContext) : base(logger, dataContext) { }
}
}

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using PARR.Domain.Entities.RobotEntities;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@@ -17,5 +18,7 @@ namespace PARR.Domain.Entities
public ICollection<RobotConfiguration> RobotConfigurations { get; set; } = new HashSet<RobotConfiguration>();
public ICollection<RobotConfigurationSnapshot> ConfigurationSnapshots { get; set; } = new HashSet<RobotConfigurationSnapshot>();
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using PARR.Domain.Constants;
using PARR.Domain.Entities.Base;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PARR.Domain.Entities.RobotEntities
{
/// <summary>
/// Снимки заданий роботам
/// </summary>
[Table("ConfigurationSnapshots", Schema = DatabaseSchemas.Robot)]
[Index(nameof(RobotCode), nameof(DateCreated))]
[Comment("Снимки заданий роботам")]
public class RobotConfigurationSnapshot : IBaseEntity
{
[Key]
public Guid Id { get; set; }
public DateTimeOffset DateCreated { get; set; }
[NotMapped]
public DateTimeOffset? DateModified { get; set; }
public int RobotCode { get; set; }
public int RobotStatusCode { get; set; }
public int TaskStatusCode { get; set; }
[Comment("Количество заданий в этой комбинации статусов")]
public int Count { get; set; }
[ForeignKey(nameof(RobotCode))]
public Robot? Robot { get; set; }
[ForeignKey(nameof(RobotStatusCode))]
public RobotStatus? RobotStatus { get; set; }
[ForeignKey(nameof(TaskStatusCode))]
public TaskStatus? TaskStatus { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using PARR.Domain.Entities.RobotEntities;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PARR.Domain.Entities
@@ -19,5 +20,7 @@ namespace PARR.Domain.Entities
public string Description { get; set; } = string.Empty;
public ICollection<RobotConfiguration> RobotConfigurations { get; set; } = new HashSet<RobotConfiguration>();
public ICollection<RobotConfigurationSnapshot> ConfigurationSnapshots { get; set; } = new HashSet<RobotConfigurationSnapshot>();
}
}

View File

@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using PARR.Domain.Entities.RobotEntities;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace PARR.Domain.Entities
@@ -21,5 +22,7 @@ namespace PARR.Domain.Entities
public ICollection<RobotConfiguration> RobotConfigurations { get; set; } = new HashSet<RobotConfiguration>();
public ICollection<RobotHistory> RobotHistories { get; set; } = new HashSet<RobotHistory>();
public ICollection<RobotConfigurationSnapshot> ConfigurationSnapshots { get; set; } = new HashSet<RobotConfigurationSnapshot>();
}
}

View File

@@ -0,0 +1,39 @@
# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
# This stage is used when running from VS in fast mode (Default for Debug configuration)
FROM 10.99.253.167:8090/dotnet/runtime:9.0 AS base
USER $APP_UID
WORKDIR /app
# This stage is used to build the service project
FROM 10.99.253.167:8090/dotnet/sdk:9.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["NuGet.config", "."]
COPY ["PARR.SnapshotWorker/PARR.SnapshotWorker.csproj", "PARR.SnapshotWorker/"]
COPY ["PARR.Core/PARR.Core.csproj", "PARR.Core/"]
COPY ["PARR.Domain/PARR.Domain.csproj", "PARR.Domain/"]
COPY ["PARR.DAL/PARR.DAL.csproj", "PARR.DAL/"]
COPY ["PARR.Infrastructure/PARR.Infrastructure.csproj", "PARR.Infrastructure/"]
RUN dotnet restore "./PARR.SnapshotWorker/PARR.SnapshotWorker.csproj"
COPY . .
WORKDIR "/src/PARR.SnapshotWorker"
RUN dotnet build "./PARR.SnapshotWorker.csproj" -c $BUILD_CONFIGURATION -o /app/build
# This stage is used to publish the service project to be copied to the final stage
FROM build AS publish
ARG app_version=0.0.0-default
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./PARR.SnapshotWorker.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false /p:Version=$app_version
# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration)
FROM base AS final
#author
ARG commit_author=unknown
LABEL org.opencontainers.image.authors=$commit_author
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "PARR.SnapshotWorker.dll"]

View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>dotnet-PARR.SnapshotWorker-87e7d52e-d873-4487-b6f4-e27d412ec0c1</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Elastic.CommonSchema.Serilog" Version="8.19.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.16" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.23.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="9.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PARR.Core\PARR.Core.csproj" />
<ProjectReference Include="..\PARR.DAL\PARR.DAL.csproj" />
<ProjectReference Include="..\PARR.Domain\PARR.Domain.csproj" />
<ProjectReference Include="..\PARR.Infrastructure\PARR.Infrastructure.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,43 @@
using Elastic.CommonSchema.Serilog;
using PARR.Core;
using PARR.Core.Services.Snapshots.Interfaces;
using PARR.DAL;
using PARR.Infrastructure;
using PARR.SnapshotWorker;
using PARR.SnapshotWorker.Settings;
using Serilog;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddLogging(config =>
{
config.ClearProviders();
var logger = new LoggerConfiguration();
if (builder.Environment.IsProduction())
logger.WriteTo.Console(new EcsTextFormatter());
else
logger.WriteTo.Console();
logger.ReadFrom.Configuration(builder.Configuration);
config.AddSerilog(logger.CreateLogger());
});
var commonSettings = new CommonSettings();
builder.Configuration.GetSection(nameof(CommonSettings)).Bind(commonSettings);
builder.Services.AddSingleton(commonSettings);
builder.Services.AddSingleton<ISnapshotSettings>(commonSettings);
builder.Services.AddDalServices(builder.Configuration);
builder.Services.AddCoreServices(builder.Configuration);
builder.Services.AddInfrastructureServices(builder.Configuration);
builder.Configuration.AddDalConfigurations(builder.Services);
builder.Services.AddDallSettings(builder.Configuration);
builder.Services.AddHostedService<Worker>();
var host = builder.Build();
host.Run();

View File

@@ -0,0 +1,15 @@
{
"profiles": {
"PARR.SnapshotWorker": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true
},
"Container (Dockerfile)": {
"commandName": "Docker"
}
},
"$schema": "https://json.schemastore.org/launchsettings.json"
}

View File

@@ -0,0 +1,13 @@
using PARR.Core.Services.Snapshots.Interfaces;
namespace PARR.SnapshotWorker.Settings
{
internal record CommonSettings : ISnapshotSettings
{
public TimeSpan RobotConfigurationSnapshotInterval { get; init; }
public TimeSpan RobotConfigurationSnapshotRetentionPeriod { get; init; }
public TimeSpan RobotSnapshotRetentionPeriod { get; init; }
}
}

View File

@@ -0,0 +1,114 @@
using PARR.Core.Common.Interfaces;
using PARR.Core.Services.Snapshots.Interfaces;
namespace PARR.SnapshotWorker
{
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly IIntervalService _intervalService;
private readonly IServiceProvider _serviceProvider;
// Хранилище времени СЛЕДУЮЩЕГО запуска для каждого сервиса
private readonly Dictionary<string, DateTimeOffset> _nextRunTimers = new();
// День последней успешной очистки базы данных
private int _lastCleanupDay = -1;
// Интервал, должен быть меньше минуты, чтобы попадать во все возможные интервалы
TimeSpan tickInterval = TimeSpan.FromSeconds(25);
public Worker(
ILogger<Worker> logger,
IIntervalService intervalService,
IServiceProvider serviceProvider
)
{
_logger = logger;
_intervalService = intervalService;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Запуск фонового сервиса создания системных снапшотов");
await _intervalService.IntervalInitAsync(async () =>
{
var now = DateTimeOffset.UtcNow;
using (var scope = _serviceProvider.CreateScope())
{
// Собираем абсолютно все зарегистрированные сервисы снапшотов
var providers = scope.ServiceProvider.GetServices<ISnapshotProvider>();
#region Параллельный сбор снапшотов
var tasksToRun = new List<Task>();
foreach (var provider in providers)
{
var providerKey = provider.GetType().FullName!;
// Если сервис видим впервые, планируем его первый старт прямо сейчас
if (!_nextRunTimers.ContainsKey(providerKey))
{
_nextRunTimers[providerKey] = now;
}
// Если текущее время добежало до запланированного «будильника»
if (now >= _nextRunTimers[providerKey])
{
// СРАЗУ планируем следующий старт, чтобы сетка времени не съезжала
// из-за времени выполнения самого метода
_nextRunTimers[providerKey] = now.Add(provider.Interval);
// Добавляем таску в список для параллельного выполнения (без await!)
tasksToRun.Add(ExecuteSafelyAsync(provider, () => provider.TakeSnapshotAsync(stoppingToken), "Создание снапшота"));
}
}
// Запускаем все готовые снапшоты ОДНОВРЕМЕННО
if (tasksToRun.Any())
{
await Task.WhenAll(tasksToRun);
}
#endregion
#region Ежедневная очистка БД
// Если наступили новые сутки в формате UTC — запускаем ротацию старых данных
if (now.Day != _lastCleanupDay)
{
_logger.LogInformation("Наступили новые сутки. Запуск процесса очистки устаревших снапшотов...");
foreach (var provider in providers)
{
await ExecuteSafelyAsync(provider, () => provider.CleanUpOldSnapshotsAsync(stoppingToken), "Очистка старых данных");
}
// Запоминаем, что за сегодня очистку уже провели успешно
_lastCleanupDay = now.Day;
}
#endregion
}
}, tickInterval);
}
private async Task ExecuteSafelyAsync(ISnapshotProvider provider, Func<Task> action, string operationName)
{
try
{
await action();
}
catch (Exception ex)
{
_logger.LogError(ex, "Критическая ошибка во время операции '{Operation}' в сервисе {ProviderName}", operationName, provider.GetType().Name);
}
}
}
}

View File

@@ -0,0 +1,29 @@
{
"ConnectionStrings": {
"RedisConnection": "10.99.253.216:6379,password=ParrP@ssPtk202MMdevDvs"
},
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "log/log-.txt",
"rollingInterval": "Day"
}
}
]
}
}

View File

@@ -0,0 +1,30 @@
{
"ConnectionStrings": {
"DefaultConnection": "Server=10.99.253.184;Database=parr;User Id=app_parr; Password=PosdfkhT&)%sdfligL&%5546;",
"RedisConnection": "parr-redis:6379,password=ParrP@ssPtk202MMdevDvs"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.EntityFrameworkCore": "Error",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
"Microsoft.AspNetCore": "Warning"
}
},
"Serilog": {
"MinimumLevel": {
"Default": "Information",
"Override": {
"Microsoft": "Warning",
"Microsoft.EntityFrameworkCore": "Error",
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}
},
"CommonSettings": {
"RobotConfigurationSnapshotInterval": "00:02:00",
"RobotConfigurationSnapshotRetentionPeriod": "60:00:00:00",
"RobotSnapshotRetentionPeriod": "60:00:00:00"
}
}

View File

@@ -9,6 +9,7 @@
<DockerServiceName>parr.api</DockerServiceName>
</PropertyGroup>
<ItemGroup>
<None Include="docker-compose.snapshots.yml" />
<None Include="docker-compose.task-reconciliation.yml" />
<None Include="docker-compose.template-matcher.yml" />
<None Include="docker-compose.template-generator.yml" />

View File

@@ -0,0 +1,26 @@
version: '3.9'
services:
parr-snapshots:
image: harbor.dvgd.rzd/parr/parr-snapshots:${tag:-latest}
environment:
- ASPNETCORE_ENVIRONMENT=Production
- TZ=Europe/Moscow
logging:
driver: fluentd
options:
fluentd-address: dvgd-efk-01.dvgd.oao.rzd:24224
fluentd-retry-wait: '10s'
fluentd-max-retries: '30'
fluentd-async: 'true'
fluentd-buffer-limit: '52428800'
tag: parr.snapshots.serilog
networks:
- parr-network
deploy:
replicas: 1
networks:
parr-network:
driver: overlay
external: true