DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/BackgroundWorkerService.cs
2021-05-24 16:30:35 +05:00

41 lines
1.1 KiB
C#

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services
{
public class BackgroundWorkerService : BackgroundService
{
private readonly IBackgroundQueue tasksQueue;
public BackgroundWorkerService(IBackgroundQueue 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);
else
await Task.Delay(100, token);
}
catch
{
//logger ?
}
}
}
public override async Task StopAsync(CancellationToken token)
{
await base.StopAsync(token);
}
}
}