Skip to content

Commit 13dcea4

Browse files
authored
dotnet 6 support (#2)
* Makes recurring actions singletons instead of transient * dotnet 6 support +semver:major
1 parent 377190e commit 13dcea4

9 files changed

Lines changed: 57 additions & 26 deletions

File tree

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ indent_style = space
1212
tab_width = 4
1313

1414
# New line preferences
15-
end_of_line = crlf
15+
end_of_line = lf
1616
insert_final_newline = false
1717

1818
#### .NET Coding Conventions ####

.github/workflows/CI.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ jobs:
1414
os: [ubuntu-latest, windows-latest]
1515
steps:
1616
- uses: actions/checkout@v2
17-
- name: Setup .NET
18-
uses: actions/setup-dotnet@v1
17+
- uses: actions/setup-dotnet@v1
1918
with:
20-
dotnet-version: 5.0.x
19+
dotnet-version: "3.1.x"
20+
- uses: actions/setup-dotnet@v1
21+
with:
22+
dotnet-version: "5.0.x"
23+
- uses: actions/setup-dotnet@v1
24+
with:
25+
dotnet-version: "6.0.x"
26+
include-prerelease: true
2127
- name: Restore dependencies
2228
run: dotnet restore
2329
- name: Build

.github/workflows/PreRelease.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ jobs:
1111
- uses: actions/checkout@v2
1212
with:
1313
fetch-depth: 0
14-
- name: Setup .NET
15-
uses: actions/setup-dotnet@v1
14+
- uses: actions/setup-dotnet@v1
1615
with:
17-
dotnet-version: 5.0.x
16+
dotnet-version: "3.1.x"
17+
- uses: actions/setup-dotnet@v1
18+
with:
19+
dotnet-version: "5.0.x"
20+
- uses: actions/setup-dotnet@v1
21+
with:
22+
dotnet-version: "6.0.x"
23+
include-prerelease: true
1824
- name: Restore dependencies
1925
run: dotnet restore
2026
- name: Build

.github/workflows/Release.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ jobs:
1111
- uses: actions/checkout@v2
1212
with:
1313
fetch-depth: 0
14-
- name: Setup .NET
15-
uses: actions/setup-dotnet@v1
14+
- uses: actions/setup-dotnet@v1
1615
with:
17-
dotnet-version: 5.0.x
16+
dotnet-version: "3.1.x"
17+
- uses: actions/setup-dotnet@v1
18+
with:
19+
dotnet-version: "5.0.x"
20+
- uses: actions/setup-dotnet@v1
21+
with:
22+
dotnet-version: "6.0.x"
23+
include-prerelease: true
1824
- name: Restore dependencies
1925
run: dotnet restore
2026
- name: Build

Samples/ConsoleApp/ConsoleApp.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<Project Sdk="Microsoft.NET.Sdk.Worker">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
8-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
8+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0-*" />
9+
910
</ItemGroup>
1011

1112
<ItemGroup>

Samples/ConsoleApp/Program.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Hosting;
77
using Microsoft.Extensions.Logging;
8-
8+
99
Host.CreateDefaultBuilder(args)
1010
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Trace))
1111
.ConfigureServices((_, services) => services.AddRecurringActions().AddRecurrer<MyCustomRecurringJob>().Build())
@@ -14,12 +14,18 @@
1414

1515
public class MyCustomRecurringJob : IRecurringAction
1616
{
17+
private readonly ILogger<MyCustomRecurringJob> _logger;
18+
19+
public MyCustomRecurringJob(ILogger<MyCustomRecurringJob> logger)
20+
{
21+
_logger = logger;
22+
}
1723
public Task Process(CancellationToken stoppingToken)
1824
{
19-
Console.WriteLine("Tick");
25+
_logger.LogInformation("Tick");
2026
return Task.CompletedTask;
2127
}
2228

23-
public string Cron => "*/30 0 */1 * * *"; // Every 30 seconds, in the zero-th minute, every hour, https://github.com/HangfireIO/Cronos#usage
29+
public string Cron => "* * * * * *"; // Every 30 seconds, in the zero-th minute, every hour, https://github.com/HangfireIO/Cronos#usage
2430
}
2531

src/CronBackgroundServices/CronBackgroundService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3636
{
3737
next = _timing.GetNextOccurenceInRelativeTime(Cron);
3838
var uText = _timing.Get10NextOccurrences(Cron);
39-
var logText = $"Ten next occurrences :\n{uText.Aggregate((x,y) => x + "\n" + y)}";
39+
var logText = $"Ten next occurrences :\n{uText.Aggregate((x, y) => x + "\n" + y)}";
4040
_logger.LogTrace(logText);
4141
}
4242

@@ -50,7 +50,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5050
{
5151
_logger.LogError(e, e.Message);
5252
}
53-
53+
5454
next = _timing.GetNextOccurenceInRelativeTime(Cron);
5555
_logger.LogTrace($"Next at {next.Value.DateTime.ToLongDateString()} {next.Value.DateTime.ToLongTimeString()}");
5656
}
@@ -59,11 +59,11 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
5959
// needed for graceful shutdown for some reason.
6060
// 100ms chosen so it doesn't affect calculating the next
6161
// cron occurence (lowest possible: every second)
62-
await Task.Delay(100, stoppingToken);
62+
await Task.Delay(100);
6363
}
6464

6565
} while (!stoppingToken.IsCancellationRequested);
6666
}
6767

6868
}
69-
}
69+
}

src/CronBackgroundServices/CronBackgroundServices.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
4+
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
55
<RootNamespace>CronBackgroundServices</RootNamespace>
66
<PackageId>CronBackgroundServices</PackageId>
77
<Authors>John Korsnes</Authors>
@@ -16,11 +16,16 @@
1616
<PackageIconUrl>images/cron.png</PackageIconUrl>
1717
<PackageIcon>cron.png</PackageIcon>
1818
<RepositoryType>git</RepositoryType>
19+
<DotNet6Version>6.0.0-preview.4.21253.7</DotNet6Version>
1920
</PropertyGroup>
2021

2122
<ItemGroup>
2223
<PackageReference Include="Cronos" Version="0.7.0" />
2324
</ItemGroup>
25+
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
26+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(DotNet6Version)" />
27+
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(DotNet6Version)" />
28+
</ItemGroup>
2429
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
2530
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
2631
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
@@ -31,6 +36,7 @@
3136
</ItemGroup>
3237

3338

39+
3440
<ItemGroup>
3541
<None Include="images/cron.png" Pack="true" PackagePath="" />
3642
</ItemGroup>

src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ public IRecurringActionsBuilder Build()
2424
if(!recurrers.Any())
2525
throw new Exception("No recurrers added. Missing");
2626

27-
foreach (var recurrer in recurrers)
27+
foreach(var recurrer in recurrers)
2828
{
29-
Services.AddTransient<IHostedService>(s =>
29+
Services.AddSingleton<IHostedService>(s =>
3030
{
3131
var allRecurrers = s.GetServices<IRecurringAction>();
3232
var single = allRecurrers.First(r => r.GetType() == recurrer.ImplementationType);
3333
var loggerFactory = s.GetService<ILoggerFactory>();
3434
var logger = loggerFactory.CreateLogger(single.GetType());
35-
return new CronBackgroundService(single,logger);
36-
});
35+
return new CronBackgroundService(single, logger);
36+
});
3737
}
3838

3939
return this;
4040
}
41-
41+
4242
public IRecurringActionsBuilder AddRecurrer<T>() where T : class, IRecurringAction
4343
{
44-
Services.AddSingleton<IRecurringAction,T>();
44+
Services.AddSingleton<IRecurringAction, T>();
4545
return this;
4646
}
4747
}

0 commit comments

Comments
 (0)