DD.WellWorkover.Cloud/AsbCloudInfrastructure/Startup.cs

81 lines
3.2 KiB
C#
Raw Normal View History

using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.DetectOperations;
using AsbCloudInfrastructure.Services.Subsystems;
using AsbCloudInfrastructure.Services;
using Microsoft.EntityFrameworkCore;
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.SAUB;
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>();
context.Database.SetCommandTimeout(TimeSpan.FromSeconds(2 * 60));
context.Database.Migrate();
var wellService = provider.GetRequiredService<IWellService>();
wellService.EnshureTimezonesIsSetAsync(CancellationToken.None).Wait();// TODO: make this background work
2023-05-19 16:27:24 +05:00
_ = provider.GetRequiredService<TelemetryDataCache<TelemetryDataSaubDto>>();
_ = provider.GetRequiredService<TelemetryDataCache<TelemetryDataSpinDto>>();
2023-02-16 16:27:14 +05:00
var backgroundWorker = provider.GetRequiredService<BackgroundWorker>();
backgroundWorker.Push(WellInfoService.MakeWork());
backgroundWorker.Push(OperationDetectionWorkFactory.MakeWork());
backgroundWorker.Push(SubsystemOperationTimeCalcWorkFactory.MakeWork());
backgroundWorker.Push(LimitingParameterCalcWorkFactory.MakeWork());
2022-12-02 17:44:47 +05:00
backgroundWorker.Push(MakeMemoryMonitoringWork());
Task.Delay(1_000)
.ContinueWith(async (_) => await backgroundWorker.StartAsync(CancellationToken.None));
}
2022-12-02 17:44:47 +05:00
static WorkPeriodic MakeMemoryMonitoringWork()
{
var workId = "Memory monitoring";
var workAction = (string _, IServiceProvider _, CancellationToken _) => {
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 workPeriod = TimeSpan.FromMinutes(1);
var work = new WorkPeriodic(workId, workAction, workPeriod);
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("### ### ###");
}
}
}