2023-11-03 17:02:44 +05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Background;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис для фонового выполнения периодической работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class PeriodicBackgroundWorker : BackgroundService
|
|
|
|
|
{
|
2023-11-07 14:19:13 +05:00
|
|
|
|
private readonly TimeSpan executePeriod = TimeSpan.FromSeconds(10);
|
|
|
|
|
private readonly TimeSpan minDelay = TimeSpan.FromSeconds(1);
|
2023-11-03 17:02:44 +05:00
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
private readonly List<WorkPeriodic> works = new(8);
|
2023-11-28 16:31:31 +05:00
|
|
|
|
private bool isRuning = false;
|
2023-11-03 17:02:44 +05:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Список периодических работ
|
|
|
|
|
/// </summary>
|
|
|
|
|
public IEnumerable<WorkPeriodic> Works => works;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Работа выполняемая в данный момент
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Work? CurrentWork;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ошибка в главном цикле, никогда не должна появляться
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string MainLoopLastException { get; private set; } = string.Empty;
|
|
|
|
|
|
|
|
|
|
public PeriodicBackgroundWorker(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken token)
|
|
|
|
|
{
|
2023-11-28 16:31:31 +05:00
|
|
|
|
if (isRuning)
|
|
|
|
|
return;
|
|
|
|
|
isRuning = true;
|
2023-11-08 10:27:00 +05:00
|
|
|
|
Trace.TraceInformation($"{GetType().Name} started");
|
2023-11-03 17:02:44 +05:00
|
|
|
|
while (!token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var periodicWork = GetNext();
|
|
|
|
|
if (periodicWork is null)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(executePeriod, token);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentWork = periodicWork.Work;
|
|
|
|
|
|
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
|
|
|
|
|
|
var result = await periodicWork.Work.Start(scope.ServiceProvider, token);
|
|
|
|
|
|
|
|
|
|
CurrentWork = null;
|
|
|
|
|
await Task.Delay(minDelay, token);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
MainLoopLastException = $"BackgroundWorker " +
|
|
|
|
|
$"MainLoopLastException: \r\n" +
|
|
|
|
|
$"date: {DateTime.Now:O}\r\n" +
|
|
|
|
|
$"message: {ex.Message}\r\n" +
|
|
|
|
|
$"inner: {ex.InnerException?.Message}\r\n" +
|
|
|
|
|
$"stackTrace: {ex.StackTrace}";
|
|
|
|
|
Trace.TraceError(MainLoopLastException);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-28 16:31:31 +05:00
|
|
|
|
isRuning = false;
|
2023-11-03 17:02:44 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить фоновую работу выполняющуюся с заданным периодом
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="period"></param>
|
|
|
|
|
public void Add<T>(TimeSpan period)
|
|
|
|
|
where T : Work, new()
|
|
|
|
|
{
|
|
|
|
|
var work = new T();
|
|
|
|
|
var periodic = new WorkPeriodic(work, period);
|
|
|
|
|
works.Add(periodic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавить фоновую работу выполняющуюся с заданным периодом
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="work"></param>
|
|
|
|
|
/// <param name="period"></param>
|
|
|
|
|
public void Add(Work work, TimeSpan period)
|
|
|
|
|
{
|
|
|
|
|
var periodic = new WorkPeriodic(work, period);
|
|
|
|
|
works.Add(periodic);
|
2023-11-07 14:19:13 +05:00
|
|
|
|
if (ExecuteTask is null || ExecuteTask.IsCompleted)
|
2023-11-29 09:02:26 +05:00
|
|
|
|
StartAsync(CancellationToken.None);
|
2023-11-03 17:02:44 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private WorkPeriodic? GetNext()
|
|
|
|
|
{
|
|
|
|
|
var work = works
|
|
|
|
|
.OrderBy(w => w.NextStart)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (work is null || work.NextStart > DateTime.Now)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return work;
|
|
|
|
|
}
|
|
|
|
|
}
|