DD.WellWorkover.Cloud/AsbCloudInfrastructure/Startup.cs

74 lines
3.0 KiB
C#

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;
using AsbCloudInfrastructure.Background;
using AsbCloudApp.Data.SAUB;
using AsbCloudDb;
using AsbCloudApp.Repositories;
using AsbCloudInfrastructure.Background.PeriodicWorks;
namespace AsbCloudInfrastructure
{
public class Startup
{
public static void BeforeRunHandler(IHost host)
{
using var scope = host.Services.CreateScope();
var provider = scope.ServiceProvider;
var context = provider.GetRequiredService<IAsbCloudDbContext>();
context.Database.EnsureCreatedAndMigrated();
// TODO: Сделать инициализацию кеша телеметрии более явной.
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
_ = provider.GetRequiredService<ITelemetryDataCache<TelemetryDataSpinDto>>();
var backgroundWorker = provider.GetRequiredService<PeriodicBackgroundWorker>();
backgroundWorker.Add<WorkProcessMapDrillingCache>(TimeSpan.FromDays(1));
backgroundWorker.Add<WorkToDeleteOldReports>(TimeSpan.FromDays(1));
backgroundWorker.Add<WellInfoService.WorkWellInfoUpdate>(TimeSpan.FromMinutes(30));
backgroundWorker.Add<WorkOperationDetection>(TimeSpan.FromMinutes(15));
backgroundWorker.Add<WorkLimitingParameterCalc>(TimeSpan.FromMinutes(30));
backgroundWorker.Add(MakeMemoryMonitoringWork(), TimeSpan.FromMinutes(1));
var notificationBackgroundWorker = provider.GetRequiredService<NotificationBackgroundWorker>();
}
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;
}
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("### ### ###");
}
}
}