75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using Microsoft.Extensions.Hosting;
|
|
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Sarand.BusinessMgr.Net.Core.Web.App
|
|
{
|
|
internal class SbmHousekeepingService : BackgroundService
|
|
{
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
Task housekeepingTask = null;
|
|
CancellationTokenSource cts = null;
|
|
int housekeepingInProgressIterationNum = 0;
|
|
int housekeepingInProgressIterationMax = 3;
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
// Wait for a period before invoking housekeeping.
|
|
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
|
|
|
|
try
|
|
{
|
|
// Check if previous housekeeping is still in progress.
|
|
if (housekeepingTask != null && !housekeepingTask.IsCompleted)
|
|
{
|
|
++housekeepingInProgressIterationNum;
|
|
|
|
if (housekeepingInProgressIterationNum >= housekeepingInProgressIterationMax)
|
|
{
|
|
SaLogger.Warn($"{SysUtil.Fn} Previous housekeeping processing still in progress after {housekeepingInProgressIterationNum} iterations - cancelling and starting new processing.");
|
|
cts.Cancel();
|
|
housekeepingInProgressIterationNum = 0;
|
|
}
|
|
else
|
|
{
|
|
// Previous housekeeping still in progress.
|
|
SaLogger.Warn($"{SysUtil.Fn} Previous housekeeping processing still in progress. Counter: {housekeepingInProgressIterationNum}.");
|
|
continue;
|
|
}
|
|
}
|
|
|
|
housekeepingTask = Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
cts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
|
|
DoHousekeeping(cts.Token);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error($"{SysUtil.Fn} {exc.GetType().Name} performing application housekeeping.\n{ExceptionUtil.GetMessage(exc)}");
|
|
}
|
|
}, stoppingToken);
|
|
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
SaLogger.Error($"{SysUtil.Fn} {exc.GetType().Name} invoking application housekeeping.\n{ExceptionUtil.GetMessage(exc)}");
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
}
|
|
|
|
private void DoHousekeeping(CancellationToken ct)
|
|
{
|
|
SbmGlobals_Web.Application.DoHousekeeping(ct);
|
|
}
|
|
}
|
|
}
|