DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/ReportsBackgroundService.cs
2021-09-27 11:47:39 +05:00

44 lines
1.3 KiB
C#

using AsbCloudApp.Services;
using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Services
{
public class ReportsBackgroundService : BackgroundService
{
private readonly IReportsBackgroundQueue tasksQueue;
public ReportsBackgroundService(IReportsBackgroundQueue tasksQueue)
{
this.tasksQueue = tasksQueue;
}
protected override async Task ExecuteAsync(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
try
{
if (tasksQueue.TryDequeue(out var item))
await Task.Run(() => item.action(item.id), token).ConfigureAwait(false);
else
await Task.Delay(100, token).ConfigureAwait(false);
}
catch(Exception ex)
{
Trace.TraceError(ex.Message);
Console.WriteLine(ex.Message);
}
}
}
public override async Task StopAsync(CancellationToken token)
{
await base.StopAsync(token).ConfigureAwait(false);
}
}
}