net6.0 Ready to test
This commit is contained in:
@ -1,174 +1,171 @@
|
||||
using EDAViewer.Models.Methods;
|
||||
using EDAViewer.Models.Stateless;
|
||||
using EDAViewer.Models.Stateless.Methods;
|
||||
using EDAViewer.Singleton;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Shared;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using IFX.Shared;
|
||||
using Serilog.Context;
|
||||
|
||||
namespace EDAViewer.HostedService
|
||||
namespace EDAViewer.HostedService;
|
||||
|
||||
public class TimedHostedService : IHostedService, IDisposable
|
||||
{
|
||||
|
||||
public class TimedHostedService : IHostedService, IDisposable
|
||||
private readonly Timer _EDAOutputArchiveTimer;
|
||||
private readonly Timer _LogPathCleanUpByWeekTimer;
|
||||
private readonly Timer _EdaDataCollectionPlansTimer;
|
||||
|
||||
private readonly int _ExecutionCount;
|
||||
private readonly Serilog.ILogger _Log;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly IBackground _BackgroundMethods;
|
||||
private readonly Models.Properties.IBackground _Background;
|
||||
|
||||
public TimedHostedService(IsEnvironment isEnvironment, Background background)
|
||||
{
|
||||
_ExecutionCount = 0;
|
||||
_Background = background;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_BackgroundMethods = background;
|
||||
_Log = Serilog.Log.ForContext<TimedHostedService>();
|
||||
_EDAOutputArchiveTimer = new Timer(EDAOutputArchiveCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
_LogPathCleanUpByWeekTimer = new Timer(LogPathCleanUpByWeekCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
_EdaDataCollectionPlansTimer = new Timer(EdaDataCollectionPlansCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
}
|
||||
|
||||
private readonly int _ExecutionCount;
|
||||
private readonly Background _Background;
|
||||
private readonly IsEnvironment _IsEnvironment;
|
||||
private readonly ILogger<TimedHostedService> _Log;
|
||||
|
||||
private Timer _EDAOutputArchiveTimer;
|
||||
private Timer _LogPathCleanUpByWeekTimer;
|
||||
private Timer _EdaDataCollectionPlansTimer;
|
||||
|
||||
public TimedHostedService(IsEnvironment isEnvironment, Background background, IServiceProvider serviceProvider)
|
||||
public Task StartAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
string? methodName = IMethodName.GetActualAsyncMethodName();
|
||||
using (LogContext.PushProperty("MethodName", methodName))
|
||||
{
|
||||
_ExecutionCount = 0;
|
||||
_Background = background;
|
||||
_IsEnvironment = isEnvironment;
|
||||
_Log = serviceProvider.GetRequiredService<ILogger<TimedHostedService>>();
|
||||
_EDAOutputArchiveTimer = new Timer(EDAOutputArchiveCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
_LogPathCleanUpByWeekTimer = new Timer(LogPathCleanUpByWeekCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
_EdaDataCollectionPlansTimer = new Timer(EdaDataCollectionPlansCallback, null, Timeout.Infinite, Timeout.Infinite);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_Log.LogInformation(string.Concat("Timed Hosted Service: ", nameof(Background), ":", _IsEnvironment.Profile, ":", Environment.ProcessId, " running."));
|
||||
_Background.Update(_Log);
|
||||
_Log.Info(string.Concat("Timed Hosted Service: ", _IsEnvironment.Profile, ":", Environment.ProcessId, " running."));
|
||||
int milliSeconds = 3000;
|
||||
if (_IsEnvironment.Development)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
_EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_BackgroundMethods.Update(milliSeconds);
|
||||
_ = _EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EdaDataCollectionPlansTimer);
|
||||
milliSeconds += 2000;
|
||||
}
|
||||
else if (_IsEnvironment.Staging)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
_LogPathCleanUpByWeekTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_Background.Timers.Add(_LogPathCleanUpByWeekTimer);
|
||||
milliSeconds += 2000;
|
||||
_EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_BackgroundMethods.Update(milliSeconds);
|
||||
_ = _EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EdaDataCollectionPlansTimer);
|
||||
milliSeconds += 2000;
|
||||
_EDAOutputArchiveTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_ = _EDAOutputArchiveTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EDAOutputArchiveTimer);
|
||||
milliSeconds += 2000;
|
||||
_ = _LogPathCleanUpByWeekTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_LogPathCleanUpByWeekTimer);
|
||||
milliSeconds += 2000;
|
||||
}
|
||||
else if (_IsEnvironment.Production)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
_LogPathCleanUpByWeekTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_Background.Timers.Add(_LogPathCleanUpByWeekTimer);
|
||||
milliSeconds += 2000;
|
||||
_EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_BackgroundMethods.Update(milliSeconds);
|
||||
_ = _EdaDataCollectionPlansTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EdaDataCollectionPlansTimer);
|
||||
milliSeconds += 2000;
|
||||
_EDAOutputArchiveTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
;
|
||||
_ = _EDAOutputArchiveTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EDAOutputArchiveTimer);
|
||||
milliSeconds += 2000;
|
||||
_ = _LogPathCleanUpByWeekTimer.Change(milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_LogPathCleanUpByWeekTimer);
|
||||
milliSeconds += 2000;
|
||||
}
|
||||
else
|
||||
throw new Exception();
|
||||
if (_IsEnvironment.Staging || _IsEnvironment.Production)
|
||||
{
|
||||
string countDirectory = _Background.GetCountDirectory("Start");
|
||||
string checkDirectory = Path.GetPathRoot(countDirectory);
|
||||
string countDirectory = _BackgroundMethods.GetCountDirectory("Start");
|
||||
string? checkDirectory = Path.GetPathRoot(countDirectory);
|
||||
if (Directory.Exists(checkDirectory))
|
||||
Directory.CreateDirectory(countDirectory);
|
||||
_ = 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), ":", _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()
|
||||
public void Dispose()
|
||||
{
|
||||
_BackgroundMethods.Dispose();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
private void LogPathCleanUpByWeekCallback(object? state)
|
||||
{
|
||||
try
|
||||
{
|
||||
_Background.Dispose();
|
||||
if (!_BackgroundMethods.IsPrimaryInstance())
|
||||
_BackgroundMethods.LogPathCleanUpByWeekCallback();
|
||||
}
|
||||
|
||||
private void LogPathCleanUpByWeekCallback(object state)
|
||||
catch (Exception e) { _Log.Error(e, "Error: "); }
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.IsPrimaryInstance())
|
||||
_Background.LogPathCleanUpByWeekCallback();
|
||||
}
|
||||
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.AddHours(6).Ticks - DateTime.Now.Ticks);
|
||||
_LogPathCleanUpByWeekTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Background.Catch(e); }
|
||||
TimeSpan timeSpan;
|
||||
if (!_BackgroundMethods.IsPrimaryInstance())
|
||||
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
|
||||
else
|
||||
timeSpan = new TimeSpan(DateTime.Now.AddHours(6).Ticks - DateTime.Now.Ticks);
|
||||
_ = _LogPathCleanUpByWeekTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Log.Error(e, "Error: "); }
|
||||
}
|
||||
|
||||
private void EDAOutputArchiveCallback(object state)
|
||||
private void EDAOutputArchiveCallback(object? state)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.IsPrimaryInstance())
|
||||
_Background.EDAOutputArchiveCallback();
|
||||
}
|
||||
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.AddHours(6).Ticks - DateTime.Now.Ticks);
|
||||
_EDAOutputArchiveTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Background.Catch(e); }
|
||||
if (!_BackgroundMethods.IsPrimaryInstance())
|
||||
_BackgroundMethods.EDAOutputArchiveCallback();
|
||||
}
|
||||
|
||||
private void EdaDataCollectionPlansCallback(object state)
|
||||
catch (Exception e) { _Log.Error(e, "Error: "); }
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.IsPrimaryInstance())
|
||||
_Background.EdaDataCollectionPlansCallback();
|
||||
}
|
||||
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.AddHours(2).Ticks - DateTime.Now.Ticks);
|
||||
_EdaDataCollectionPlansTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Background.Catch(e); }
|
||||
TimeSpan timeSpan;
|
||||
if (!_BackgroundMethods.IsPrimaryInstance())
|
||||
timeSpan = new TimeSpan(DateTime.Now.AddSeconds(15).Ticks - DateTime.Now.Ticks);
|
||||
else
|
||||
timeSpan = new TimeSpan(DateTime.Now.AddHours(6).Ticks - DateTime.Now.Ticks);
|
||||
_ = _EDAOutputArchiveTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Log.Error(e, "Error: "); }
|
||||
}
|
||||
|
||||
|
||||
private void EdaDataCollectionPlansCallback(object? state)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!_BackgroundMethods.IsPrimaryInstance())
|
||||
_BackgroundMethods.EdaDataCollectionPlansCallback();
|
||||
}
|
||||
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.AddHours(2).Ticks - DateTime.Now.Ticks);
|
||||
_ = _EdaDataCollectionPlansTimer.Change((int)timeSpan.TotalMilliseconds, Timeout.Infinite);
|
||||
}
|
||||
catch (Exception e) { _Log.Error(e, "Error: "); }
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user