DD.WellWorkover.Cloud/AsbCloudInfrastructure/Startup.cs

72 lines
2.8 KiB
C#
Raw Normal View History

using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.DetectOperations;
using AsbCloudInfrastructure.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;
using System.Threading;
2022-12-02 17:44:47 +05:00
using AsbCloudInfrastructure.Background;
2023-05-19 16:27:24 +05:00
using AsbCloudApp.Data.SAUB;
2023-10-22 19:37:23 +05:00
using AsbCloudDb;
using AsbCloudApp.Repositories;
using AsbCloudInfrastructure.Background.PeriodicWorks;
2024-08-19 10:01:07 +05:00
namespace AsbCloudInfrastructure;
public class Startup
{
2024-08-19 10:01:07 +05:00
public static void BeforeRunHandler(IHost host)
{
2024-08-19 10:01:07 +05:00
using var scope = host.Services.CreateScope();
var provider = scope.ServiceProvider;
2024-08-19 10:01:07 +05:00
var context = provider.GetRequiredService<IAsbCloudDbContext>();
context.Database.EnsureCreatedAndMigrated();
2024-08-19 10:01:07 +05:00
// TODO: Сделать инициализацию кеша телеметрии более явной.
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSpinDto>>();
2023-05-19 16:27:24 +05:00
2024-08-19 10:01:07 +05:00
var backgroundWorker = provider.GetRequiredService<PeriodicBackgroundWorker>();
backgroundWorker.Add<WorkToDeleteOldReports>(TimeSpan.FromDays(1));
backgroundWorker.Add<WellInfoService.WorkWellInfoUpdate>(TimeSpan.FromMinutes(30));
backgroundWorker.Add<WorkOperationDetection>(TimeSpan.FromMinutes(0));
backgroundWorker.Add<WorkDataSaubStat>(TimeSpan.FromMinutes(0));
backgroundWorker.Add<WorkLimitingParameterCalc>(TimeSpan.FromMinutes(0));
backgroundWorker.Add(MakeMemoryMonitoringWork(), TimeSpan.FromMinutes(0));
2024-08-19 10:01:07 +05:00
var notificationBackgroundWorker = provider.GetRequiredService<NotificationBackgroundWorker>();
}
2022-12-02 17:44:47 +05:00
2024-08-19 10:01:07 +05:00
static Work MakeMemoryMonitoringWork()
{
var workAction = (string _, IServiceProvider _, Action<string, double?> _, CancellationToken _) => {
var bytes = GC.GetTotalMemory(false);
var bytesString = FromatBytes(bytes);
System.Diagnostics.Trace.TraceInformation($"Total memory allocated is {bytesString} bytes. DbContext count is:{AsbCloudDbContext.ReferenceCount}");
return Task.CompletedTask;
};
var work = Work.CreateByDelegate("Memory monitoring", workAction);
return work;
}
2022-12-02 17:44:47 +05:00
2024-08-19 10:01:07 +05:00
static string FromatBytes(long bytes)
{
const double gigaByte = 1024 * 1024 * 1024;
const double megaByte = 1024 * 1024;
const double kiloByte = 1024;
if (bytes > 10 * gigaByte)
return (bytes / gigaByte).ToString("### ### ###.## Gb");
2022-12-02 17:44:47 +05:00
2024-08-19 10:01:07 +05:00
if (bytes > 10 * megaByte)
return (bytes / megaByte).ToString("### ### ###.## Mb");
2022-12-02 17:44:47 +05:00
2024-08-19 10:01:07 +05:00
if (bytes > 10 * kiloByte)
return (bytes / megaByte).ToString("### ### ###.## Kb");
2022-12-02 17:44:47 +05:00
2024-08-19 10:01:07 +05:00
return bytes.ToString("### ### ###");
}
}