mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-12 20:01:56 +08:00
29 lines
844 B
C#
29 lines
844 B
C#
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace BuildingBlocks.EventStoreDB.BackgroundWorkers;
|
|
|
|
public class BackgroundWorker : BackgroundService
|
|
{
|
|
private readonly ILogger<BackgroundWorker> logger;
|
|
private readonly Func<CancellationToken, Task> perform;
|
|
|
|
public BackgroundWorker(
|
|
ILogger<BackgroundWorker> logger,
|
|
Func<CancellationToken, Task> perform
|
|
)
|
|
{
|
|
this.logger = logger;
|
|
this.perform = perform;
|
|
}
|
|
|
|
protected override Task ExecuteAsync(CancellationToken stoppingToken) =>
|
|
Task.Run(async () =>
|
|
{
|
|
await Task.Yield();
|
|
logger.LogInformation("Background worker stopped");
|
|
await perform(stoppingToken);
|
|
logger.LogInformation("Background worker stopped");
|
|
}, stoppingToken);
|
|
}
|