Match TFS Changeset 303361
This commit is contained in:
168
APC Viewer/HostedService/TimedHostedService.cs
Normal file
168
APC Viewer/HostedService/TimedHostedService.cs
Normal file
@ -0,0 +1,168 @@
|
||||
using APCViewer.Singleton;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace APCViewer.HostedService
|
||||
{
|
||||
|
||||
public class TimedHostedService : IHostedService, IDisposable
|
||||
{
|
||||
|
||||
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)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
if (milliSeconds == 0)
|
||||
{ }
|
||||
}
|
||||
else if (_Background.IsEnvironment.Staging)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
_APCDataTimer = new Timer(APCDataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_APCDataTimer);
|
||||
milliSeconds += 2000;
|
||||
_EAFLogDataTimer = new Timer(EAFLogDataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EAFLogDataTimer);
|
||||
milliSeconds += 2000;
|
||||
_EDADataTimer = new Timer(EDADataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EDADataTimer);
|
||||
milliSeconds += 2000;
|
||||
}
|
||||
else if (_Background.IsEnvironment.Production)
|
||||
{
|
||||
int milliSeconds = 3000;
|
||||
_APCDataTimer = new Timer(APCDataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_APCDataTimer);
|
||||
milliSeconds += 2000;
|
||||
_EAFLogDataTimer = new Timer(EAFLogDataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.Timers.Add(_EAFLogDataTimer);
|
||||
milliSeconds += 2000;
|
||||
_EDADataTimer = new Timer(EDADataCallback, null, milliSeconds, Timeout.Infinite);
|
||||
_Background.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;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
_Log.LogInformation(string.Concat("Timed Hosted Service: ", nameof(Background), ":", _Background.IsEnvironment.Profile, ":", Environment.ProcessId, " is stopping."));
|
||||
_Background.Stop(immediate: true);
|
||||
for (short i = 0; i < short.MaxValue; i++)
|
||||
{
|
||||
Thread.Sleep(500);
|
||||
if (_ExecutionCount == 0)
|
||||
break;
|
||||
}
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_Background.Dispose();
|
||||
}
|
||||
|
||||
private void APCDataCallback(object state)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.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)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.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)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_Background.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); }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user