2022-01-17 15:33:00 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2022-12-02 17:18:16 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.DetectOperations;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Subsystems;
|
|
|
|
|
using AsbCloudInfrastructure.Services;
|
2022-01-17 15:33:00 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
2022-12-02 17:18:16 +05:00
|
|
|
|
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;
|
2022-01-17 15:33:00 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure
|
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
public class Startup
|
2022-01-17 15:33:00 +05:00
|
|
|
|
{
|
2022-12-02 14:49:00 +05:00
|
|
|
|
public static void BeforeRunHandler(IHost host)
|
2022-04-11 18:00:34 +05:00
|
|
|
|
{
|
2022-01-17 15:33:00 +05:00
|
|
|
|
using var scope = host.Services.CreateScope();
|
2022-12-02 17:18:16 +05:00
|
|
|
|
var provider = scope.ServiceProvider;
|
2022-01-17 15:33:00 +05:00
|
|
|
|
|
2023-04-14 10:11:15 +05:00
|
|
|
|
var context = provider.GetRequiredService<IAsbCloudDbContext>();
|
2022-12-02 17:18:16 +05:00
|
|
|
|
context.Database.SetCommandTimeout(TimeSpan.FromSeconds(2 * 60));
|
2022-01-17 15:33:00 +05:00
|
|
|
|
context.Database.Migrate();
|
|
|
|
|
|
2023-08-08 12:24:44 +05:00
|
|
|
|
// TODO: Сделать инициализацию кеша телеметрии более явной.
|
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());
|
2022-12-02 17:18:16 +05:00
|
|
|
|
backgroundWorker.Push(OperationDetectionWorkFactory.MakeWork());
|
|
|
|
|
backgroundWorker.Push(SubsystemOperationTimeCalcWorkFactory.MakeWork());
|
|
|
|
|
backgroundWorker.Push(LimitingParameterCalcWorkFactory.MakeWork());
|
2022-12-02 17:44:47 +05:00
|
|
|
|
backgroundWorker.Push(MakeMemoryMonitoringWork());
|
2022-12-02 17:18:16 +05:00
|
|
|
|
|
2023-07-26 15:41:51 +05:00
|
|
|
|
var notificationBackgroundWorker = provider.GetRequiredService<NotificationBackgroundWorker>();
|
|
|
|
|
|
2022-12-02 17:18:16 +05:00
|
|
|
|
Task.Delay(1_000)
|
2023-07-26 15:41:51 +05:00
|
|
|
|
.ContinueWith(async (_) =>
|
|
|
|
|
{
|
|
|
|
|
await backgroundWorker.StartAsync(CancellationToken.None);
|
|
|
|
|
await notificationBackgroundWorker.StartAsync(CancellationToken.None);
|
|
|
|
|
});
|
2022-01-17 15:33:00 +05:00
|
|
|
|
}
|
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("### ### ###");
|
|
|
|
|
}
|
2022-01-17 15:33:00 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2022-01-17 15:33:00 +05:00
|
|
|
|
}
|