-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify servicecollection extensions (#3)
- Breaking change: removes DI builder pattern - Breaking change: drop netcoreapp3.1 support Other: - Update to C# 10 features and use of .NET 6 SDK +semver:major
- Loading branch information
Showing
14 changed files
with
197 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using CronBackgroundServices; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
using CronBackgroundServices; | ||
|
||
Host.CreateDefaultBuilder(args) | ||
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Trace)) | ||
.ConfigureServices((_, services) => services.AddRecurringActions().AddRecurrer<MyCustomRecurringJob>().Build()) | ||
.ConfigureServices(services => services | ||
.AddRecurrer<EveryThreeSeconds>() | ||
.AddRecurrer<EveryFiveSeconds>()) | ||
.Build() | ||
.Run(); | ||
|
||
public class MyCustomRecurringJob : IRecurringAction | ||
public class EveryFiveSeconds : IRecurringAction | ||
{ | ||
private readonly ILogger<MyCustomRecurringJob> _logger; | ||
public string Cron => "*/5 * * * * *"; | ||
|
||
public MyCustomRecurringJob(ILogger<MyCustomRecurringJob> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
public Task Process(CancellationToken stoppingToken) | ||
public Task Process(CancellationToken stoppingToken) => Logger.Log("🕔 Tick 5th second 5️⃣ 🖐"); | ||
} | ||
|
||
public class EveryThreeSeconds : IRecurringAction | ||
{ | ||
public string Cron => "*/3 * * * * *"; | ||
|
||
public Task Process(CancellationToken stoppingToken) => Logger.Log("🕒 Tick 3rd second 3️⃣ 🥉"); | ||
} | ||
|
||
static class Logger | ||
{ | ||
public static Task Log(string msg) | ||
{ | ||
_logger.LogInformation("Tick"); | ||
Console.WriteLine(msg); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public string Cron => "* * * * * *"; // Every 30 seconds, in the zero-th minute, every hour, https://github.com/HangfireIO/Cronos#usage | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,63 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace CronBackgroundServices | ||
namespace CronBackgroundServices; | ||
|
||
internal class CronBackgroundService : BackgroundService | ||
{ | ||
internal class CronBackgroundService : BackgroundService | ||
protected readonly IRecurringAction Action; | ||
private readonly ILogger _logger; | ||
private readonly Timing _timing; | ||
|
||
public CronBackgroundService(IRecurringAction action, ILogger logger) | ||
{ | ||
protected readonly IRecurringAction Action; | ||
private readonly ILogger _logger; | ||
private readonly Timing _timing; | ||
_timing = new Timing(action.GetTimeZoneId()); | ||
Action = action; | ||
_logger = logger; | ||
Cron = action.Cron; | ||
_logger.LogTrace($"Using {Cron} and timezone '{_timing.TimeZoneInfo.Id}. The time in this timezone: {_timing.RelativeNow()}'"); | ||
} | ||
|
||
public CronBackgroundService(IRecurringAction action, ILogger logger) | ||
{ | ||
_timing = new Timing(action.GetTimeZoneId()); | ||
Action = action; | ||
_logger = logger; | ||
Cron = action.Cron; | ||
_logger.LogTrace($"Using {Cron} and timezone '{_timing.TimeZoneInfo.Id}. The time in this timezone: {_timing.RelativeNow()}'"); | ||
} | ||
private string Cron { get; } | ||
|
||
private string Cron { get; } | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
DateTimeOffset? next = null; | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
do | ||
{ | ||
DateTimeOffset? next = null; | ||
var now = _timing.RelativeNow(); | ||
|
||
do | ||
if (next == null) | ||
{ | ||
var now = _timing.RelativeNow(); | ||
next = _timing.GetNextOccurenceInRelativeTime(Cron); | ||
var uText = _timing.Get10NextOccurrences(Cron); | ||
var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}"; | ||
_logger.LogTrace(logText); | ||
} | ||
|
||
if (next == null) | ||
{ | ||
next = _timing.GetNextOccurenceInRelativeTime(Cron); | ||
var uText = _timing.Get10NextOccurrences(Cron); | ||
var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}"; | ||
_logger.LogTrace(logText); | ||
} | ||
|
||
if (now > next) | ||
if (now > next) | ||
{ | ||
try | ||
{ | ||
try | ||
{ | ||
await Action.Process(stoppingToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, e.Message); | ||
} | ||
|
||
next = _timing.GetNextOccurenceInRelativeTime(Cron); | ||
_logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}"); | ||
await Action.Process(stoppingToken); | ||
} | ||
else | ||
catch (Exception e) | ||
{ | ||
// needed for graceful shutdown for some reason. | ||
// 100ms chosen so it doesn't affect calculating the next | ||
// cron occurence (lowest possible: every second) | ||
await Task.Delay(100); | ||
_logger.LogError(e, e.Message); | ||
} | ||
|
||
} while (!stoppingToken.IsCancellationRequested); | ||
} | ||
next = _timing.GetNextOccurenceInRelativeTime(Cron); | ||
_logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}"); | ||
} | ||
else | ||
{ | ||
// needed for graceful shutdown for some reason. | ||
// 100ms chosen so it doesn't affect calculating the next | ||
// cron occurence (lowest possible: every second) | ||
await Task.Delay(100); | ||
} | ||
|
||
} while (!stoppingToken.IsCancellationRequested); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.