Files
Saxonite/SaxoniteCoreWeb/Base/Consoles/SaxHostedServiceConsole_Web.cs
T
2024-06-04 16:15:09 +01:00

99 lines
3.1 KiB
C#

using Microsoft.Extensions.Hosting;
using Sarand.Net.Standard.Base.App;
using Sarand.Net.Standard.Base.Core;
using Sarand.Net.Standard.Base.Tasks;
using Sarand.Net.Standard.Base.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Saxonite.Net.Core.Web.Base.Consoles
{
//=======================================================================
public abstract class SaxHostedServiceConsole_Web : SacsDisposable, IHostedService
{
//-------------------------------------------------------------------
// Definitions.
//-------------------------------------------------------------------
// Data members.
private Timer processTimer;
//-------------------------------------------------------------------
// Construction.
protected SaxHostedServiceConsole_Web ()
{
}
//-------------------------------------------------------------------
// Clean-up.
protected override void Dispose(bool disposing)
{
DisposeTimer();
if (ProcessTaskData != null) ProcessTaskData.Dispose();
ProcessTaskData = null;
}
private void DisposeTimer()
{
if (processTimer != null) processTimer.Dispose();
processTimer = null;
}
//-------------------------------------------------------------------
// Properties.
// Task data - allows cancellation to be triggered for executing processes, eg. when application is stopping.
protected SacsTaskData ProcessTaskData { get; private set; }
//-------------------------------------------------------------------
/// <summary>
/// Periodic processing to execute application processes as required.
/// </summary>
protected abstract void DoWork(object state);
//-------------------------------------------------------------------
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
/// <returns></returns>
public virtual Task StartAsync(CancellationToken cancellationToken)
{
SaLogger.Info($"{SysUtil.Fn} Timed Background Service started.");
// Initialise the background task cancellation token.
ProcessTaskData = new SacsTaskData();
// Initialise the scheduled task timer.
processTimer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
return Task.CompletedTask;
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
/// <returns></returns>
public virtual Task StopAsync(CancellationToken cancellationToken)
{
SaLogger.Info($"{SysUtil.Fn} Timed Background Service stopped.");
// Signal cancellation to the background tasks.
ProcessTaskData.TriggerCancellation();
// Stop the timer.
processTimer?.Change(Timeout.Infinite, 0);
return Task.CompletedTask;
}
//-------------------------------------------------------------------
}
//=======================================================================
}