125 lines
4.3 KiB
C#
125 lines
4.3 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Bus;
|
|
using Sarand.Net.Standard.Base.Core;
|
|
using Sarand.Net.Standard.Base.Dbas;
|
|
using Sarand.Net.Standard.Base.Tasks;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using Saxonite.Net.Core.Web.Admin.App;
|
|
using Saxonite.Net.Core.Web.Base.Consoles;
|
|
using Saxonite.Net.Core.Web.Base.Procs;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Saxonite.Net.Core.Web.Admin.Consoles
|
|
{
|
|
//=======================================================================
|
|
public class SaxHostedServiceConsole_Admin : SaxHostedServiceConsole_Web
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Definitions.
|
|
private enum ProcessType { Unknown = 0, GolfUsgaReferenceLookup }
|
|
|
|
/// <summary>
|
|
/// Process tracking data to control when process is run.
|
|
/// </summary>
|
|
private class ProcessControlData
|
|
{
|
|
// Construction.
|
|
public ProcessControlData(ProcessType processType, TimeSpan minTimeBetweenRuns)
|
|
{
|
|
this.ProcessType = processType;
|
|
this.MinTimeBetweenRuns = minTimeBetweenRuns;
|
|
ResetRunTime();
|
|
}
|
|
// Properties.
|
|
public ProcessType ProcessType { get; }
|
|
public TimeSpan MinTimeBetweenRuns { get; }
|
|
public DateTime LastRunAt { get; private set; }
|
|
// Utilities.
|
|
public bool ReadyToRun()
|
|
{
|
|
//SaLogger.Info($"{SysUtil.Fn} Process: {ProcessType}. Secs since last run: {(DateTime.Now - LastRunAt).TotalSeconds}. Seconds between runs: {MinTimeBetweenRuns.TotalSeconds}.");
|
|
return (DateTime.Now - LastRunAt) > MinTimeBetweenRuns;
|
|
}
|
|
public void ResetRunTime()
|
|
{
|
|
LastRunAt = DateTime.Now;
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
private Dictionary<ProcessType, ProcessControlData> processes { get; } = new Dictionary<ProcessType, ProcessControlData>();
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
public SaxHostedServiceConsole_Admin()
|
|
{
|
|
InitialiseProcesses();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Clean-up.
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// Periodic processing to execute application processes as required.
|
|
/// <para>- See parent class for timer logic.</para>
|
|
/// </summary>
|
|
protected override void DoWork(object state)
|
|
{
|
|
//SaLogger.Info($"{SysUtil.Fn} Timed Background Service is working. State: {state}");
|
|
SaxSessionContext sessionContext = SaxApplication_Admin.AppInstance.SessionContext;
|
|
DbUpdateInfo dbUpdateInfo = new DbUpdateInfo(new SysId(), "AdminScheduled", SacsSystem.ComputerName);
|
|
|
|
// Check for cancellation.
|
|
var token = ProcessTaskData.CancellationToken;
|
|
if (token.IsCancellationRequested) return;
|
|
|
|
// Identify processes that are ready to run.
|
|
foreach (ProcessControlData process in processes.Where(item => item.Value.ReadyToRun()).Select(item => item.Value))
|
|
{
|
|
//SaLogger.Info($"{SysUtil.Fn} Executing process: {process.ProcessType}.");
|
|
process.ResetRunTime();
|
|
switch (process.ProcessType)
|
|
{
|
|
case ProcessType.GolfUsgaReferenceLookup:
|
|
// Run on separate thread.
|
|
Task task = new Task(() =>
|
|
{
|
|
SaxGolfUsgaDataLookup.ExecuteScheduledUsgaDataLookup(sessionContext, dbUpdateInfo, ProcessTaskData);
|
|
}, ProcessTaskData.CancellationToken);
|
|
task.Start();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Initialisation.
|
|
|
|
private void InitialiseProcesses()
|
|
{
|
|
if (Sax.Config.Admin.Processes.Scheduled.UsgaGolfDataLookup.Enabled)
|
|
InitialiseProcess(ProcessType.GolfUsgaReferenceLookup, TimeSpan.FromMinutes(MathUtil.Constrain(Sax.Config.Admin.Processes.Scheduled.UsgaGolfDataLookup.MinsBetweenExecution, 2, 120)));
|
|
}
|
|
|
|
private void InitialiseProcess(ProcessType processType, TimeSpan minTimeBetweenRuns)
|
|
{
|
|
if (!processes.ContainsKey(processType))
|
|
processes[processType] = new ProcessControlData(processType, minTimeBetweenRuns);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|