168 lines
6.3 KiB
C#
168 lines
6.3 KiB
C#
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;
|
|
|
|
namespace APCViewer.HostedService;
|
|
|
|
public class TimedHostedService : IHostedService
|
|
{
|
|
|
|
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);
|
|
}
|
|
|
|
public Task StartAsync(CancellationToken stoppingToken)
|
|
{
|
|
string? methodName = IMethodName.GetActualAsyncMethodName();
|
|
using (LogContext.PushProperty("MethodName", methodName))
|
|
{
|
|
_Log.Info(string.Concat("Timed Hosted Service: ", _IsEnvironment.Profile, ":", Environment.ProcessId, " running."));
|
|
int milliSeconds = 3000;
|
|
if (_IsEnvironment.Development)
|
|
{
|
|
if (milliSeconds == 0)
|
|
{ }
|
|
}
|
|
else if (_IsEnvironment.Staging)
|
|
{
|
|
_ = _APCDataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_APCDataTimer);
|
|
milliSeconds += 2000;
|
|
_ = _EAFLogDataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_EAFLogDataTimer);
|
|
milliSeconds += 2000;
|
|
_ = _EDADataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_EDADataTimer);
|
|
milliSeconds += 2000;
|
|
}
|
|
else if (_IsEnvironment.Production)
|
|
{
|
|
_ = _APCDataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_APCDataTimer);
|
|
milliSeconds += 2000;
|
|
_ = _EAFLogDataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_EAFLogDataTimer);
|
|
milliSeconds += 2000;
|
|
_ = _EDADataTimer.Change(milliSeconds, Timeout.Infinite);
|
|
_Timers.Add(_EDADataTimer);
|
|
milliSeconds += 2000;
|
|
}
|
|
else
|
|
throw new Exception();
|
|
}
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
public Task StopAsync(CancellationToken stoppingToken)
|
|
{
|
|
string? methodName = IMethodName.GetActualAsyncMethodName();
|
|
using (LogContext.PushProperty("MethodName", methodName))
|
|
{
|
|
_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;
|
|
}
|
|
|
|
private void APCDataCallback(object? state)
|
|
{
|
|
try
|
|
{
|
|
string? methodName = IMethodName.GetActualAsyncMethodName();
|
|
using (LogContext.PushProperty("MethodName", methodName))
|
|
{
|
|
if (_BackgroundMethods.IsPrimaryInstance())
|
|
_Background.APCDataCallback();
|
|
}
|
|
}
|
|
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);
|
|
_ = _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 (_BackgroundMethods.IsPrimaryInstance())
|
|
_Background.EDADataCallback();
|
|
}
|
|
}
|
|
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);
|
|
_ = _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 (_BackgroundMethods.IsPrimaryInstance())
|
|
_Background.EAFLogDataCallback();
|
|
}
|
|
}
|
|
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: "); }
|
|
}
|
|
|
|
} |