From 13dcea454a2cd1aab79fa2e86d79dd2c09be5890 Mon Sep 17 00:00:00 2001 From: John Korsnes Date: Tue, 29 Jun 2021 14:31:39 +0200 Subject: [PATCH] dotnet 6 support (#2) * Makes recurring actions singletons instead of transient * dotnet 6 support +semver:major --- .editorconfig | 2 +- .github/workflows/CI.yml | 12 +++++++++--- .github/workflows/PreRelease.yml | 12 +++++++++--- .github/workflows/Release.yml | 12 +++++++++--- Samples/ConsoleApp/ConsoleApp.csproj | 5 +++-- Samples/ConsoleApp/Program.cs | 12 +++++++++--- src/CronBackgroundServices/CronBackgroundService.cs | 8 ++++---- .../CronBackgroundServices.csproj | 8 +++++++- .../Hosting/RecurringActionsBuilder.cs | 12 ++++++------ 9 files changed, 57 insertions(+), 26 deletions(-) diff --git a/.editorconfig b/.editorconfig index 3298f9b..e98711d 100644 --- a/.editorconfig +++ b/.editorconfig @@ -12,7 +12,7 @@ indent_style = space tab_width = 4 # New line preferences -end_of_line = crlf +end_of_line = lf insert_final_newline = false #### .NET Coding Conventions #### diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index b8d0b76..4fd3ae1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -14,10 +14,16 @@ jobs: os: [ubuntu-latest, windows-latest] steps: - uses: actions/checkout@v2 - - name: Setup .NET - uses: actions/setup-dotnet@v1 + - uses: actions/setup-dotnet@v1 with: - dotnet-version: 5.0.x + dotnet-version: "3.1.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "5.0.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "6.0.x" + include-prerelease: true - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/PreRelease.yml b/.github/workflows/PreRelease.yml index b4ca9fd..e815030 100644 --- a/.github/workflows/PreRelease.yml +++ b/.github/workflows/PreRelease.yml @@ -11,10 +11,16 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Setup .NET - uses: actions/setup-dotnet@v1 + - uses: actions/setup-dotnet@v1 with: - dotnet-version: 5.0.x + dotnet-version: "3.1.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "5.0.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "6.0.x" + include-prerelease: true - name: Restore dependencies run: dotnet restore - name: Build diff --git a/.github/workflows/Release.yml b/.github/workflows/Release.yml index 16280d6..f90c2ae 100644 --- a/.github/workflows/Release.yml +++ b/.github/workflows/Release.yml @@ -11,10 +11,16 @@ jobs: - uses: actions/checkout@v2 with: fetch-depth: 0 - - name: Setup .NET - uses: actions/setup-dotnet@v1 + - uses: actions/setup-dotnet@v1 with: - dotnet-version: 5.0.x + dotnet-version: "3.1.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "5.0.x" + - uses: actions/setup-dotnet@v1 + with: + dotnet-version: "6.0.x" + include-prerelease: true - name: Restore dependencies run: dotnet restore - name: Build diff --git a/Samples/ConsoleApp/ConsoleApp.csproj b/Samples/ConsoleApp/ConsoleApp.csproj index 31ec18c..85b6807 100644 --- a/Samples/ConsoleApp/ConsoleApp.csproj +++ b/Samples/ConsoleApp/ConsoleApp.csproj @@ -1,11 +1,12 @@ - net5.0 + net6.0 - + + diff --git a/Samples/ConsoleApp/Program.cs b/Samples/ConsoleApp/Program.cs index 80a7b59..5b67ba3 100644 --- a/Samples/ConsoleApp/Program.cs +++ b/Samples/ConsoleApp/Program.cs @@ -5,7 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; - + Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Trace)) .ConfigureServices((_, services) => services.AddRecurringActions().AddRecurrer().Build()) @@ -14,12 +14,18 @@ public class MyCustomRecurringJob : IRecurringAction { + private readonly ILogger _logger; + + public MyCustomRecurringJob(ILogger logger) + { + _logger = logger; + } public Task Process(CancellationToken stoppingToken) { - Console.WriteLine("Tick"); + _logger.LogInformation("Tick"); return Task.CompletedTask; } - public string Cron => "*/30 0 */1 * * *"; // Every 30 seconds, in the zero-th minute, every hour, https://github.com/HangfireIO/Cronos#usage + public string Cron => "* * * * * *"; // Every 30 seconds, in the zero-th minute, every hour, https://github.com/HangfireIO/Cronos#usage } diff --git a/src/CronBackgroundServices/CronBackgroundService.cs b/src/CronBackgroundServices/CronBackgroundService.cs index 7d03c5e..0808be6 100644 --- a/src/CronBackgroundServices/CronBackgroundService.cs +++ b/src/CronBackgroundServices/CronBackgroundService.cs @@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { next = _timing.GetNextOccurenceInRelativeTime(Cron); var uText = _timing.Get10NextOccurrences(Cron); - var logText = $"Ten next occurrences :\n{uText.Aggregate((x,y) => x + "\n" + y)}"; + var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}"; _logger.LogTrace(logText); } @@ -50,7 +50,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogError(e, e.Message); } - + next = _timing.GetNextOccurenceInRelativeTime(Cron); _logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}"); } @@ -59,11 +59,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) // 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, stoppingToken); + await Task.Delay(100); } } while (!stoppingToken.IsCancellationRequested); } } -} \ No newline at end of file +} diff --git a/src/CronBackgroundServices/CronBackgroundServices.csproj b/src/CronBackgroundServices/CronBackgroundServices.csproj index fe18b94..f8c474e 100644 --- a/src/CronBackgroundServices/CronBackgroundServices.csproj +++ b/src/CronBackgroundServices/CronBackgroundServices.csproj @@ -1,7 +1,7 @@  - netcoreapp3.1;net5.0 + netcoreapp3.1;net5.0;net6.0 CronBackgroundServices CronBackgroundServices John Korsnes @@ -16,11 +16,16 @@ images/cron.png cron.png git + 6.0.0-preview.4.21253.7 + + + + @@ -31,6 +36,7 @@ + diff --git a/src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs b/src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs index 275e832..f35f47d 100644 --- a/src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs +++ b/src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs @@ -24,24 +24,24 @@ public IRecurringActionsBuilder Build() if(!recurrers.Any()) throw new Exception("No recurrers added. Missing"); - foreach (var recurrer in recurrers) + foreach(var recurrer in recurrers) { - Services.AddTransient(s => + Services.AddSingleton(s => { var allRecurrers = s.GetServices(); var single = allRecurrers.First(r => r.GetType() == recurrer.ImplementationType); var loggerFactory = s.GetService(); var logger = loggerFactory.CreateLogger(single.GetType()); - return new CronBackgroundService(single,logger); - }); + return new CronBackgroundService(single, logger); + }); } return this; } - + public IRecurringActionsBuilder AddRecurrer() where T : class, IRecurringAction { - Services.AddSingleton(); + Services.AddSingleton(); return this; } }