Builds but needs tested

This commit is contained in:
2022-03-25 18:13:23 -07:00
parent a6abe8fdbd
commit 1f607cbbed
113 changed files with 12922 additions and 5993 deletions

View File

@ -1,168 +1,168 @@
using APCViewer.Singleton;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using APCViewer.Models.Methods;
using APCViewer.Models.Stateless;
using APCViewer.Models.Stateless.Methods;
using APCViewer.Singleton;
using IFX.Shared;
using Serilog.Context;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
namespace APCViewer.HostedService
namespace APCViewer.HostedService;
public class TimedHostedService : IHostedService
{
public class TimedHostedService : IHostedService, IDisposable
private readonly Timer _APCDataTimer;
private readonly Timer _EDADataTimer;
private readonly Timer _EAFLogDataTimer;
private readonly int _ExecutionCount;
private readonly List<Timer> _Timers;
private readonly Serilog.ILogger _Log;
private readonly Background _Background;
private readonly IsEnvironment _IsEnvironment;
private readonly IBackground _BackgroundMethods;
public TimedHostedService(IsEnvironment isEnvironment, Background background)
{
_Timers = new();
_ExecutionCount = 0;
_Background = background;
_IsEnvironment = isEnvironment;
_BackgroundMethods = background;
_Log = Serilog.Log.ForContext<TimedHostedService>();
_APCDataTimer = new Timer(APCDataCallback, null, Timeout.Infinite, Timeout.Infinite);
_EAFLogDataTimer = new Timer(EAFLogDataCallback, null, Timeout.Infinite, Timeout.Infinite);
_EDADataTimer = new Timer(EDADataCallback, null, Timeout.Infinite, Timeout.Infinite);
}
private readonly int _ExecutionCount;
private readonly WebClient _WebClient;
private readonly Background _Background;
private readonly IConfiguration _Configuration;
private readonly ILogger<TimedHostedService> _Log;
private Timer _APCDataTimer;
private Timer _EDADataTimer;
private Timer _EAFLogDataTimer;
public TimedHostedService(Background background, IConfiguration configuration, IServiceProvider serviceProvider)
public Task StartAsync(CancellationToken stoppingToken)
{
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
_ExecutionCount = 0;
_Background = background;
_Configuration = configuration;
_WebClient = serviceProvider.GetRequiredService<WebClient>();
_Log = serviceProvider.GetRequiredService<ILogger<TimedHostedService>>();
//_HttpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
_WebClient = serviceProvider.GetRequiredService<WebClient>();
}
public Task StartAsync(CancellationToken stoppingToken)
{
_Log.LogInformation(string.Concat("Timed Hosted Service: ", nameof(Background), ":", _Background.IsEnvironment.Profile, ":", Environment.ProcessId, " running."));
_Background.Update(_Log, _WebClient);
if (_Background.IsEnvironment.Development)
_Log.Info(string.Concat("Timed Hosted Service: ", _IsEnvironment.Profile, ":", Environment.ProcessId, " running."));
int milliSeconds = 3000;
if (_IsEnvironment.Development)
{
int milliSeconds = 3000;
if (milliSeconds == 0)
{ }
}
else if (_Background.IsEnvironment.Staging)
else if (_IsEnvironment.Staging)
{
int milliSeconds = 3000;
_APCDataTimer = new Timer(APCDataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_APCDataTimer);
_ = _APCDataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_APCDataTimer);
milliSeconds += 2000;
_EAFLogDataTimer = new Timer(EAFLogDataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_EAFLogDataTimer);
_ = _EAFLogDataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_EAFLogDataTimer);
milliSeconds += 2000;
_EDADataTimer = new Timer(EDADataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_EDADataTimer);
_ = _EDADataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_EDADataTimer);
milliSeconds += 2000;
}
else if (_Background.IsEnvironment.Production)
else if (_IsEnvironment.Production)
{
int milliSeconds = 3000;
_APCDataTimer = new Timer(APCDataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_APCDataTimer);
_ = _APCDataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_APCDataTimer);
milliSeconds += 2000;
_EAFLogDataTimer = new Timer(EAFLogDataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_EAFLogDataTimer);
_ = _EAFLogDataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_EAFLogDataTimer);
milliSeconds += 2000;
_EDADataTimer = new Timer(EDADataCallback, null, milliSeconds, Timeout.Infinite);
_Background.Timers.Add(_EDADataTimer);
_ = _EDADataTimer.Change(milliSeconds, Timeout.Infinite);
_Timers.Add(_EDADataTimer);
milliSeconds += 2000;
}
else
throw new Exception();
if (_Background.IsEnvironment.Staging || _Background.IsEnvironment.Production)
{
string countDirectory = _Background.GetCountDirectory("Start");
string checkDirectory = Path.GetPathRoot(countDirectory);
if (Directory.Exists(checkDirectory))
Directory.CreateDirectory(countDirectory);
}
return Task.CompletedTask;
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken stoppingToken)
public Task StopAsync(CancellationToken stoppingToken)
{
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
_Log.LogInformation(string.Concat("Timed Hosted Service: ", nameof(Background), ":", _Background.IsEnvironment.Profile, ":", Environment.ProcessId, " is stopping."));
_Background.Stop(immediate: true);
_Log.Info(string.Concat("Timed Hosted Service: ", _IsEnvironment.Profile, ":", Environment.ProcessId, " is stopping."));
_BackgroundMethods.Stop(immediate: true);
for (short i = 0; i < short.MaxValue; i++)
{
Thread.Sleep(500);
if (_ExecutionCount == 0)
break;
}
return Task.CompletedTask;
}
return Task.CompletedTask;
}
public void Dispose()
private void APCDataCallback(object? state)
{
try
{
_Background.Dispose();
}
private void APCDataCallback(object state)
{
try
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
if (_Background.IsPrimaryInstance())
if (_BackgroundMethods.IsPrimaryInstance())
_Background.APCDataCallback();
}
catch (Exception e) { _Background.Catch(e); }
try
{
TimeSpan timeSpan;
if (!_Background.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_APCDataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Background.Catch(e); }
}
private void EDADataCallback(object state)
catch (Exception e) { _Log.Error(e, "Error: "); }
try
{
try
TimeSpan timeSpan;
if (!_BackgroundMethods.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_ = _APCDataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Log.Error(e, "Error: "); }
}
private void EDADataCallback(object? state)
{
try
{
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
if (_Background.IsPrimaryInstance())
if (_BackgroundMethods.IsPrimaryInstance())
_Background.EDADataCallback();
}
catch (Exception e) { _Background.Catch(e); }
try
{
TimeSpan timeSpan;
if (!_Background.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_EDADataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Background.Catch(e); }
}
private void EAFLogDataCallback(object state)
catch (Exception e) { _Log.Error(e, "Error: "); }
try
{
try
TimeSpan timeSpan;
if (!_BackgroundMethods.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_ = _EDADataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Log.Error(e, "Error: "); }
}
private void EAFLogDataCallback(object? state)
{
try
{
string? methodName = IMethodName.GetActualAsyncMethodName();
using (LogContext.PushProperty("MethodName", methodName))
{
if (_Background.IsPrimaryInstance())
if (_BackgroundMethods.IsPrimaryInstance())
_Background.EAFLogDataCallback();
}
catch (Exception e) { _Background.Catch(e); }
try
{
TimeSpan timeSpan;
if (!_Background.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_EAFLogDataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Background.Catch(e); }
}
catch (Exception e) { _Log.Error(e, "Error: "); }
try
{
TimeSpan timeSpan;
if (!_BackgroundMethods.IsPrimaryInstance())
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
else
timeSpan = new TimeSpan(DateTime.Now.AddMinutes(1).Ticks - DateTime.Now.Ticks);
_ = _EAFLogDataTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
}
catch (Exception e) { _Log.Error(e, "Error: "); }
}
}