2022-12-05 10:53:24 +05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2022-12-02 14:45:20 +05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Background
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сервис для фонового выполнения работы
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BackgroundWorker : BackgroundService
|
|
|
|
|
{
|
|
|
|
|
private static readonly TimeSpan executePeriod = TimeSpan.FromSeconds(10);
|
|
|
|
|
private static readonly TimeSpan minDelay = TimeSpan.FromSeconds(2);
|
|
|
|
|
private static readonly TimeSpan exceptionHandleTimeout = TimeSpan.FromSeconds(2);
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
private readonly WorkQueue workQueue = new WorkQueue();
|
2022-12-02 17:18:16 +05:00
|
|
|
|
public string? CurrentWorkId;
|
2022-12-02 14:45:20 +05:00
|
|
|
|
public BackgroundWorker(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление задачи в очередь.
|
|
|
|
|
/// Не периодические задачи будут выполняться вперед.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="work"></param>
|
|
|
|
|
/// <exception cref="ArgumentException">Id mast be unique</exception>
|
|
|
|
|
public void Push(WorkBase work)
|
|
|
|
|
{
|
|
|
|
|
workQueue.Push(work);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверяет наличие работы с указанным Id
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Contains(string id)
|
|
|
|
|
{
|
|
|
|
|
return workQueue.Contains(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление работы по ID
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Delete(string id)
|
|
|
|
|
{
|
|
|
|
|
return workQueue.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
while (!token.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
var dateStart = DateTime.Now;
|
|
|
|
|
var work = workQueue.Pop();
|
|
|
|
|
if (work is null)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(executePeriod, token);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-12-02 17:18:16 +05:00
|
|
|
|
CurrentWorkId = work.Id;
|
2022-12-02 14:45:20 +05:00
|
|
|
|
using var scope = serviceProvider.CreateScope();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-02 17:18:16 +05:00
|
|
|
|
Trace.TraceInformation($"Backgroud work:\"{work.Id}\" start.");
|
2022-12-02 14:45:20 +05:00
|
|
|
|
var task = work.ActionAsync(work.Id, scope.ServiceProvider, token);
|
|
|
|
|
await task.WaitAsync(work.Timeout, token);
|
|
|
|
|
|
|
|
|
|
work.ExecutionTime = DateTime.Now - dateStart;
|
|
|
|
|
Trace.TraceInformation($"Backgroud work:\"{work.Id}\" done. ExecutionTime: {work.ExecutionTime:hh\\:mm\\:ss\\.fff}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
Trace.TraceError($"Backgroud work:\"{work.Id}\" throw exception: {exception.Message}");
|
|
|
|
|
if (work.OnErrorAsync is not null)
|
|
|
|
|
{
|
|
|
|
|
using var task = Task.Run(
|
|
|
|
|
async () => await work.OnErrorAsync(work.Id, exception, token),
|
|
|
|
|
token);
|
|
|
|
|
await task.WaitAsync(exceptionHandleTimeout, token);
|
|
|
|
|
}
|
2022-12-02 17:18:16 +05:00
|
|
|
|
}
|
|
|
|
|
CurrentWorkId = null;
|
2022-12-02 14:45:20 +05:00
|
|
|
|
await Task.Delay(minDelay, token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|