DD.WellWorkover.Cloud/AsbCloudInfrastructure/Startup.cs

80 lines
3.2 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;
using AsbCloudInfrastructure.Services.Subsystems;
2023-10-22 19:37:23 +05:00
using AsbCloudDb;
using AsbCloudApp.Repositories;
namespace AsbCloudInfrastructure
{
2022-04-11 18:00:34 +05:00
public class Startup
{
2022-12-02 14:49:00 +05:00
public static void BeforeRunHandler(IHost host)
2022-04-11 18:00:34 +05:00
{
using var scope = host.Services.CreateScope();
var provider = scope.ServiceProvider;
2023-04-14 10:11:15 +05:00
var context = provider.GetRequiredService<IAsbCloudDbContext>();
2023-10-22 19:37:23 +05:00
context.Database.EnshureCreatedAndMigrated();
// TODO: Сделать инициализацию кеша телеметрии более явной.
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSpinDto>>();
2023-05-19 16:27:24 +05:00
2023-11-03 17:02:44 +05:00
var backgroundWorker = provider.GetRequiredService<PeriodicBackgroundWorker>();
backgroundWorker.Add<WellInfoService.WorkWellInfoUpdate>(TimeSpan.FromMinutes(30));
backgroundWorker.Add<WorkOperationDetection>(TimeSpan.FromMinutes(15));
backgroundWorker.Add<WorkSubsystemOperationTimeCalc>(TimeSpan.FromMinutes(30));
backgroundWorker.Add<WorkLimitingParameterCalc>(TimeSpan.FromMinutes(30));
backgroundWorker.Add(MakeMemoryMonitoringWork(), TimeSpan.FromMinutes(1));
var notificationBackgroundWorker = provider.GetRequiredService<NotificationBackgroundWorker>();
Task.Delay(1_000)
.ContinueWith(async (_) =>
{
await backgroundWorker.StartAsync(CancellationToken.None);
await notificationBackgroundWorker.StartAsync(CancellationToken.None);
});
}
2022-12-02 17:44:47 +05:00
2023-10-08 21:20:28 +05:00
static Work MakeMemoryMonitoringWork()
2022-12-02 17:44:47 +05:00
{
2023-10-08 19:45:21 +05:00
var workAction = (string _, IServiceProvider _, Action<string, double?> _, CancellationToken _) => {
2022-12-02 17:44:47 +05:00
var bytes = GC.GetTotalMemory(false);
var bytesString = FromatBytes(bytes);
2023-05-19 16:27:24 +05:00
System.Diagnostics.Trace.TraceInformation($"Total memory allocated is {bytesString} bytes. DbContext count is:{AsbCloudDbContext.ReferenceCount}");
2022-12-02 17:44:47 +05:00
return Task.CompletedTask;
};
var work = Work.CreateByDelegate("Memory monitoring", workAction);
2022-12-02 17:44:47 +05:00
return work;
}
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");
if (bytes > 10 * megaByte)
return (bytes / megaByte).ToString("### ### ###.## Mb");
if (bytes > 10 * kiloByte)
return (bytes / megaByte).ToString("### ### ###.## Kb");
return bytes.ToString("### ### ###");
}
}
}