Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scheduler Background Service Scope Injection
using System;
using System.Threading;
using System.Threading.Tasks;
using Cronos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace SchedulerJobSample.Worker
{
public class SchedulerService : BackgroundService
{
private readonly ILogger<SchedulerService> _logger;
private readonly IServiceProvider _serviceProvider;
public SchedulerService(ILogger<SchedulerService> logger, IServiceProvider serviceProvider)
{
_logger = logger;
_serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Schedule the job every minute.
await WaitForNextSchedule("* * * * *");
using var scope = _serviceProvider.CreateScope();
var scopedSchedulerService = scope.ServiceProvider.GetRequiredService<IScopedSchedulerService>();
await scopedSchedulerService.ExecuteAsync(stoppingToken);
}
}
private async Task WaitForNextSchedule(string cronExpression)
{
var parsedExp = CronExpression.Parse(cronExpression);
var currentUtcTime = DateTimeOffset.UtcNow.UtcDateTime;
var occurenceTime = parsedExp.GetNextOccurrence(currentUtcTime);
var delay = occurenceTime.GetValueOrDefault() - currentUtcTime;
_logger.LogInformation("The run is delayed for {delay}. Current time: {time}", delay, DateTimeOffset.Now);
await Task.Delay(delay);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment