mirror of
https://git.teknik.io/Teknikode/Teknik.git
synced 2023-08-02 14:16:22 +02:00
49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
|
using Microsoft.Extensions.Hosting;
|
|||
|
using Microsoft.Extensions.Logging;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Teknik.Logging;
|
|||
|
using Teknik.Utilities;
|
|||
|
|
|||
|
namespace Teknik.Tracking
|
|||
|
{
|
|||
|
public class TrackingService : BackgroundService
|
|||
|
{
|
|||
|
private readonly ILogger<Logger> _logger;
|
|||
|
|
|||
|
public TrackingService(IBackgroundTaskQueue taskQueue,
|
|||
|
ILogger<Logger> logger)
|
|||
|
{
|
|||
|
TaskQueue = taskQueue;
|
|||
|
_logger = logger;
|
|||
|
}
|
|||
|
|
|||
|
public IBackgroundTaskQueue TaskQueue { get; }
|
|||
|
|
|||
|
protected async override Task ExecuteAsync(CancellationToken cancellationToken)
|
|||
|
{
|
|||
|
_logger.LogInformation("Queued Hosted Service is starting.");
|
|||
|
|
|||
|
while (!cancellationToken.IsCancellationRequested)
|
|||
|
{
|
|||
|
var workItem = await TaskQueue.DequeueAsync(cancellationToken);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
await workItem(cancellationToken);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
_logger.LogError(ex,
|
|||
|
$"Error occurred executing {nameof(workItem)}.");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
_logger.LogInformation("Queued Hosted Service is stopping.");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|