2021-07-21 15:29:19 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-09-23 17:59:31 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
2021-07-21 15:29:19 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
2021-09-27 11:47:39 +05:00
|
|
|
|
public class ReportsBackgroundService : BackgroundService
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
2021-09-27 11:47:39 +05:00
|
|
|
|
private readonly IReportsBackgroundQueue tasksQueue;
|
2021-05-19 14:41:27 +05:00
|
|
|
|
|
2021-09-27 11:47:39 +05:00
|
|
|
|
public ReportsBackgroundService(IReportsBackgroundQueue tasksQueue)
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
|
|
|
|
this.tasksQueue = tasksQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken token)
|
|
|
|
|
{
|
2021-05-24 16:30:35 +05:00
|
|
|
|
while (!token.IsCancellationRequested)
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
2021-05-24 16:30:35 +05:00
|
|
|
|
try
|
2021-05-19 14:41:27 +05:00
|
|
|
|
{
|
|
|
|
|
if (tasksQueue.TryDequeue(out var item))
|
2021-08-11 17:26:02 +05:00
|
|
|
|
await Task.Run(() => item.action(item.id), token).ConfigureAwait(false);
|
2021-05-19 14:41:27 +05:00
|
|
|
|
else
|
2021-08-11 17:26:02 +05:00
|
|
|
|
await Task.Delay(100, token).ConfigureAwait(false);
|
2021-05-19 14:41:27 +05:00
|
|
|
|
}
|
2021-09-23 17:59:31 +05:00
|
|
|
|
catch(Exception ex)
|
2021-05-24 16:30:35 +05:00
|
|
|
|
{
|
2021-09-23 17:59:31 +05:00
|
|
|
|
Trace.TraceError(ex.Message);
|
|
|
|
|
Console.WriteLine(ex.Message);
|
2021-05-24 16:30:35 +05:00
|
|
|
|
}
|
2021-05-19 14:41:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override async Task StopAsync(CancellationToken token)
|
|
|
|
|
{
|
2021-08-11 17:26:02 +05:00
|
|
|
|
await base.StopAsync(token).ConfigureAwait(false);
|
2021-05-19 14:41:27 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|