Skip to content

Commit

Permalink
dotnet 6 support (#2)
Browse files Browse the repository at this point in the history
* Makes recurring actions singletons instead of transient

* dotnet 6 support

+semver:major
  • Loading branch information
johnkors authored Jun 29, 2021
1 parent 377190e commit 13dcea4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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 ####
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/PreRelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions Samples/ConsoleApp/ConsoleApp.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Worker">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0-*" />

</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 9 additions & 3 deletions Samples/ConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyCustomRecurringJob>().Build())
Expand All @@ -14,12 +14,18 @@

public class MyCustomRecurringJob : IRecurringAction
{
private readonly ILogger<MyCustomRecurringJob> _logger;

public MyCustomRecurringJob(ILogger<MyCustomRecurringJob> 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
}

8 changes: 4 additions & 4 deletions src/CronBackgroundServices/CronBackgroundService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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()}");
}
Expand All @@ -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);
}

}
}
}
8 changes: 7 additions & 1 deletion src/CronBackgroundServices/CronBackgroundServices.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<RootNamespace>CronBackgroundServices</RootNamespace>
<PackageId>CronBackgroundServices</PackageId>
<Authors>John Korsnes</Authors>
Expand All @@ -16,11 +16,16 @@
<PackageIconUrl>images/cron.png</PackageIconUrl>
<PackageIcon>cron.png</PackageIcon>
<RepositoryType>git</RepositoryType>
<DotNet6Version>6.0.0-preview.4.21253.7</DotNet6Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Cronos" Version="0.7.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(DotNet6Version)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(DotNet6Version)" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
Expand All @@ -31,6 +36,7 @@
</ItemGroup>



<ItemGroup>
<None Include="images/cron.png" Pack="true" PackagePath="" />
</ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/CronBackgroundServices/Hosting/RecurringActionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IHostedService>(s =>
Services.AddSingleton<IHostedService>(s =>
{
var allRecurrers = s.GetServices<IRecurringAction>();
var single = allRecurrers.First(r => r.GetType() == recurrer.ImplementationType);
var loggerFactory = s.GetService<ILoggerFactory>();
var logger = loggerFactory.CreateLogger(single.GetType());
return new CronBackgroundService(single,logger);
});
return new CronBackgroundService(single, logger);
});
}

return this;
}

public IRecurringActionsBuilder AddRecurrer<T>() where T : class, IRecurringAction
{
Services.AddSingleton<IRecurringAction,T>();
Services.AddSingleton<IRecurringAction, T>();
return this;
}
}
Expand Down

0 comments on commit 13dcea4

Please sign in to comment.