Skip to content

Commit 211e274

Browse files
authored
Merge pull request #50 from kdcllc/dev
Deps and IServiceCollection for UnobservedTaskExceptionHandler
2 parents 2e11249 + ab3a67c commit 211e274

File tree

70 files changed

+1980
-2057
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1980
-2057
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ BenchmarkDotNet.Artifacts/
5555
project.lock.json
5656
project.fragment.lock.json
5757
artifacts/
58-
**/Properties/launchSettings.json
5958

6059
# StyleCop
6160
StyleCopReport.xml

CHANGELOG.md

+22
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22
Change Log
33
===============================================================================
44

5+
Version 3.1.0 (6/12/2022)
6+
7+
* `SchedulerBuilder.UnobservedTaskExceptionHandler` marked as `Obsolete`;
8+
9+
* Adds a function to create an event handler that handles unobserved task exceptions during the lifetime of the CRON job. Thanks to @stijnmoreels
10+
11+
```csharp
12+
builder.AddUnobservedTaskExceptionHandler(sp =>
13+
{
14+
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("CronJobs");
15+
16+
return
17+
(sender, args) =>
18+
{
19+
logger?.LogError(args.Exception?.Message);
20+
args.SetObserved();
21+
};
22+
});
23+
```
24+
525
Version 3.0.1
26+
627
* Fixed issue with Scheduled jobs that are added on the fly to the execution engine issue #43
728
* Upgraded to the latest nuget packages
829

@@ -30,6 +51,7 @@ Version 1.0.9 (2019-02-18)
3051
* Removed `HostedServiceBase` class in favor of built-in `BackgroundService` class.
3152

3253
Version 1.0.7 (2018-01-16)
54+
3355
* Resolved issue #5 "Add support for SourceLink", to make use of this feature in Visual Studio.NET please deselect `Enable Just My Code` and select `Enable Source Link support` as shown per this image:
3456
![enable](img/source_link_enable.JPG)
3557

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 King David Consulting LLC
3+
Copyright (c) 2017-2022 King David Consulting LLC
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+26-20
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
The goal of this library was to design a simple Cron Scheduling engine that could be used with DotNetCore `IHost` or with AspNetCore `IWebHost`.
1313

14-
It is much lighter than Quartz schedular or its alternatives. In the heart of its design was `KISS` principle.
14+
It is much lighter than Quartz schedular or its alternatives. The `KISS` principle was at the heart of the development of this library.
1515

1616
The `CronScheduler` can operate inside of any .NET Core GenericHost `IHost` thus makes it simpler to setup and configure but it always allow to be run inside of Kubernetes.
1717

@@ -162,8 +162,19 @@ The sample uses `Microsoft.Extensions.Http.Polly` extension library to make http
162162
builder.Services.AddHttpClient<TorahService>()
163163
.AddTransientHttpErrorPolicy(p => p.RetryAsync());
164164
builder.AddJob<TorahQuoteJob, TorahQuoteJobOptions>();
165+
166+
// register a custom error processing for internal errors
167+
builder.AddUnobservedTaskExceptionHandler(sp =>
168+
{
169+
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("CronJobs");
165170

166-
builder.UnobservedTaskExceptionHandler = UnobservedHandler;
171+
return
172+
(sender, args) =>
173+
{
174+
logger?.LogError(args.Exception?.Message);
175+
args.SetObserved();
176+
};
177+
});
167178
});
168179
```
169180

@@ -209,7 +220,18 @@ Then register this service within the `Startup.cs`
209220
builder.Services.AddScoped<UserService>();
210221
builder.AddJob<UserJob, UserJobOptions>();
211222

212-
builder.UnobservedTaskExceptionHandler = UnobservedHandler;
223+
// register a custom error processing for internal errors
224+
builder.AddUnobservedTaskExceptionHandler(sp =>
225+
{
226+
var logger = sp.GetRequiredService<ILoggerFactory>().CreateLogger("CronJobs");
227+
228+
return
229+
(sender, args) =>
230+
{
231+
logger?.LogError(args.Exception?.Message);
232+
args.SetObserved();
233+
};
234+
});
213235
});
214236
```
215237

@@ -284,22 +306,6 @@ Then add sample async task to be executed by the Queued Hosted Service.
284306
}
285307
```
286308

287-
## Docker build
288-
289-
Utilizes [King David Consulting LLC DotNet Docker Image](https://github.com/kdcllc/docker/tree/master/dotnet)
290-
291-
```bash
292-
docker-compose -f "docker-compose.yml" -f "docker-compose.override.yml" up -d --build
293-
```
294-
295-
### Note
296-
297-
Workaround for `Retrying 'FindPackagesByIdAsync' for source` in Docker containers restore.
298-
299-
```bash
300-
dotnet restore --disable-parallel
301-
```
302-
303309
## License
304310

305-
[MIT License Copyright (c) 2017 King David Consulting LLC](./LICENSE)
311+
[MIT License Copyright (c) 2017-2022 King David Consulting LLC](./LICENSE)

appveyor.yml

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
version: 3.0.{build}
1+
version: 3.1.{build}
22
branches:
33
only:
44
- master
55
pull_requests:
66
do_not_increment_build_number: true
7-
image: Visual Studio 2017
8-
## temporary until 5.0.100 sdk is installed
7+
image: Visual Studio 2022
8+
## temporary until 6.0.300 sdk is installed
99
install:
10-
- ps: $urlCurrent = "https://dotnetcli.blob.core.windows.net/dotnet/Sdk/5.0.100/dotnet-sdk-5.0.100-win-x64.zip"
10+
- ps: $urlCurrent = "https://dotnetcli.blob.core.windows.net/dotnet/Sdk/6.0.300/dotnet-sdk-6.0.300-win-x64.zip"
1111
- ps: $env:DOTNET_INSTALL_DIR = "$pwd\.dotnetsdk"
1212
- ps: mkdir $env:DOTNET_INSTALL_DIR -Force | Out-Null
1313
- ps: $tempFileCurrent = [System.IO.Path]::GetTempFileName()
@@ -33,6 +33,6 @@ deploy:
3333
- provider: NuGet
3434
artifact: /NuGet/
3535
api_key:
36-
secure: hs4f+3xdpI1ANqvOB7J9BZx+aBdbZYzHmoYymDFA7YCt5AWLJSdNyv2nkrBn1V9q
36+
secure: a8sCawSwgb2kYDJAN+xTUvy+MH5jdJR+DmKakUmc/Xom1c+uxyvV+yvpSTJs+ypF
3737
on:
3838
branch: master

build/dependencies.props

+53-70
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,67 @@
11
<Project>
22

3-
<PropertyGroup>
4-
<AspNetCoreVersion>2.1.1</AspNetCoreVersion>
5-
<ExtensionsVersion>2.1.1</ExtensionsVersion>
6-
<EFCommonVersion>$(AspNetCoreVersion)</EFCommonVersion>
7-
<BetCommon>3.1.11</BetCommon>
8-
</PropertyGroup>
3+
<PropertyGroup>
4+
<AspNetCoreVersion>[6.*, )</AspNetCoreVersion>
5+
<ExtensionsVersion>[6.*, )</ExtensionsVersion>
6+
<EFCommonVersion>$(AspNetCoreVersion)</EFCommonVersion>
7+
<BetCommon>[4.*, )</BetCommon>
8+
</PropertyGroup>
99

10-
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'net5.0'">
11-
<AspNetCoreVersion>5.0.*</AspNetCoreVersion>
12-
<ExtensionsVersion>5.0.*</ExtensionsVersion>
13-
<EFCommonVersion>$(AspNetCoreVersion)</EFCommonVersion>
14-
</PropertyGroup>
10+
<ItemGroup Label="CronScheduler">
11+
<PackageReference Update="Cronos" Version="0.7.1" />
12+
<PackageReference Update="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
13+
<PackageReference Update="Microsoft.Extensions.Hosting.Abstractions" Version="$(ExtensionsVersion)" />
14+
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(ExtensionsVersion)" />
15+
<PackageReference Update="Microsoft.Extensions.Logging.Abstractions" Version="$(ExtensionsVersion)" />
16+
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="$(ExtensionsVersion)" />
17+
<PackageReference Update="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="$(ExtensionsVersion)" />
18+
<PackageReference Update="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="[1.*, )" />
19+
</ItemGroup>
1520

16-
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.1' Or '$(TargetFramework)' == 'netstandard2.0'">
17-
<AspNetCoreVersion>2.1.1</AspNetCoreVersion>
18-
<ExtensionsVersion>2.1.1</ExtensionsVersion>
19-
<EFCommonVersion>$(AspNetCoreVersion)</EFCommonVersion>
20-
</PropertyGroup>
21+
<ItemGroup Label="Websiste">
22+
<PackageReference Update="Microsoft.Extensions.Http.Polly" Version="$(ExtensionsVersion)" />
23+
<PackageReference Update="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="$(AspNetCoreVersion)"/>
24+
</ItemGroup>
2125

22-
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp2.2'">
23-
<AspNetCoreVersion>[2.2.*, )</AspNetCoreVersion>
24-
<EFCommonVersion>[2.2.*, )</EFCommonVersion>
25-
<ExtensionsVersion>[2.2.*, )</ExtensionsVersion>
26-
</PropertyGroup>
26+
<ItemGroup Label="EF">
27+
<PackageReference Update="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(EFCommonVersion)" />
28+
<PackageReference Update="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(EFCommonVersion)" />
29+
<PackageReference Update="Microsoft.AspNetCore.Identity.UI" Version="$(EFCommonVersion)" />
2730

28-
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp3.1' Or '$(TargetFramework)' == 'netcoreapp3.0' Or '$(TargetFramework)' == 'netstandard2.1'">
29-
<AspNetCoreVersion>3.1.10</AspNetCoreVersion>
30-
<ExtensionsVersion>3.1.10</ExtensionsVersion>
31-
<EFCommonVersion>$(AspNetCoreVersion)</EFCommonVersion>
32-
</PropertyGroup>
31+
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EFCommonVersion)" />
32+
<PackageReference Update="Microsoft.EntityFrameworkCore.Sqlite" Version="$(EFCommonVersion)" />
33+
<PackageReference Update="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EFCommonVersion)" />
34+
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="$(EFCommonVersion)"/>
35+
</ItemGroup>
3336

34-
<ItemGroup Label="CronScheduler">
35-
<PackageReference Update="Cronos" Version="0.7.0" />
36-
<PackageReference Update="Microsoft.AspNetCore.Hosting" Version="$(AspNetCoreVersion)" />
37-
<PackageReference Update="Microsoft.Extensions.Hosting.Abstractions" Version="$(ExtensionsVersion)" />
38-
<PackageReference Update="Microsoft.Extensions.Options.ConfigurationExtensions" Version="$(ExtensionsVersion)" />
39-
<PackageReference Update="Microsoft.Extensions.DependencyInjection" Version="$(ExtensionsVersion)" />
40-
</ItemGroup>
37+
<ItemGroup Label="Worker">
38+
<PackageReference Update="Microsoft.Extensions.Hosting" Version="$(ExtensionsVersion)" />
39+
</ItemGroup>
4140

42-
<ItemGroup Label="Websiste">
43-
<PackageReference Update="Microsoft.Extensions.Http.Polly" Version="$(ExtensionsVersion)" />
44-
<PackageReference Update="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="$(AspNetCoreVersion)"/>
45-
</ItemGroup>
41+
<ItemGroup Label="Unit Tests">
42+
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.*" />
43+
<PackageReference Update="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
44+
<PackageReference Update="Moq" Version="4.*" />
45+
<PackageReference Update="xunit" Version="2.*" />
46+
<PackageReference Update="xunit.runner.visualstudio" Version="2.*" PrivateAssets="All" />
47+
</ItemGroup>
4648

47-
<ItemGroup Label="EF">
48-
<PackageReference Update="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="$(EFCommonVersion)" />
49-
<PackageReference Update="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="$(EFCommonVersion)" />
50-
<PackageReference Update="Microsoft.AspNetCore.Identity.UI" Version="$(EFCommonVersion)" />
49+
<ItemGroup Label="Bet">
50+
<PackageReference Update="Bet.Extensions.Options" Version="$(BetCommon)" />
51+
<PackageReference Update="Bet.Extensions.Testing" Version="$(BetCommon)" />
52+
</ItemGroup>
5153

52-
<PackageReference Update="Microsoft.EntityFrameworkCore.Relational" Version="$(EFCommonVersion)" />
53-
<PackageReference Update="Microsoft.EntityFrameworkCore.Sqlite" Version="$(EFCommonVersion)" />
54-
<PackageReference Update="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EFCommonVersion)" />
55-
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="$(EFCommonVersion)"/>
56-
</ItemGroup>
54+
<ItemGroup Label="SourceLink">
55+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.*" PrivateAssets="All" />
56+
<PackageReference Include="Bet.CodeAnalyzers" Version="1.0.12" PrivateAssets="All" />
57+
</ItemGroup>
5758

58-
<ItemGroup Label="Worker">
59-
<PackageReference Update="Microsoft.Extensions.Hosting" Version="$(ExtensionsVersion)" />
60-
</ItemGroup>
59+
<ItemGroup>
60+
<None Include="../../img/icon.png" Pack="true" Visible="false" PackagePath="" />
61+
</ItemGroup>
6162

62-
<ItemGroup Label="Unit Tests">
63-
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="16.0.1" />
64-
<PackageReference Update="Microsoft.AspNetCore.TestHost" Version="$(AspNetCoreVersion)" />
65-
<PackageReference Update="Moq" Version="4.15.2" />
66-
<PackageReference Update="xunit" Version="2.4.1" />
67-
<PackageReference Update="xunit.runner.visualstudio" Version="2.4.3" PrivateAssets="All" />
68-
</ItemGroup>
69-
70-
<ItemGroup Label="Bet">
71-
<PackageReference Update="Bet.Extensions.Options" Version="$(BetCommon)" />
72-
<PackageReference Update="Bet.Extensions.Testing" Version="$(BetCommon)" />
73-
</ItemGroup>
74-
75-
<ItemGroup Label="SourceLink">
76-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
77-
<PackageReference Include="Bet.CodeAnalyzers" Version="1.0.10" PrivateAssets="All" />
78-
</ItemGroup>
79-
80-
<ItemGroup>
81-
<None Include="../../img/icon.png" Pack="true" Visible="false" PackagePath="" />
82-
</ItemGroup>
63+
<ItemGroup>
64+
<None Include="../../README.md" Pack="true" PackagePath="\" />
65+
</ItemGroup>
8366

8467
</Project>

build/settings.props

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
<Project>
22

33
<PropertyGroup Label="Basic Settings">
4-
<VersionPrefix>3.0.1-preview1</VersionPrefix>
4+
<VersionPrefix>3.1.0-preview1</VersionPrefix>
55
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
66
<SuppressNETCoreSdkPreviewMessage>true</SuppressNETCoreSdkPreviewMessage>
77
<NoWarn>$(NoWarn);CS1591;NU1605;</NoWarn>
8-
<TieredCompilation>true</TieredCompilation>
9-
<AspNetCoreHostingModel>inprocess</AspNetCoreHostingModel>
108
</PropertyGroup>
119

1210
<PropertyGroup>
@@ -28,7 +26,7 @@
2826
<RepositoryType>git</RepositoryType>
2927
<PackageLicense>https://github.com/kdcllc/CronScheduler.AspNetCore/blob/master/LICENSE</PackageLicense>
3028
<PackageIcon>icon.png</PackageIcon>
31-
<PackageTags>Cron, Quatz, HangFire, AspNetCore, DotNet, DotNetCore CronNET, Scheduler, Worker, Hosting, Kubernetes, Docker</PackageTags>
29+
<PackageTags>CronScheduler.AspNetCore, CronScheduler.Extensions, Cron, Quatz, HangFire, AspNetCore, DotNet, DotNetCore CronNET, Scheduler, Worker, Hosting, Kubernetes, Docker</PackageTags>
3230
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
3331

3432
</PropertyGroup>

build/sources.props

-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@
55
<RestoreSources>$(DotNetRestoreSources)</RestoreSources>
66
<RestoreSources Condition="'$(DotNetBuildOffline)' != 'false'">
77
$(RestoreSources);
8-
https://dotnetfeed.blob.core.windows.net/aspnet-aspnetcore/index.json;
9-
https://dotnetfeed.blob.core.windows.net/aspnet-blazor/index.json;
10-
https://dotnetfeed.blob.core.windows.net/aspnet-entityframeworkcore/index.json;
11-
https://dotnetfeed.blob.core.windows.net/aspnet-extensions/index.json;
12-
https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json;
13-
https://www.myget.org/F/kdcllc/api/v3/index.json;
148
https://api.nuget.org/v3/index.json;
159
</RestoreSources>
1610
</PropertyGroup>

clean.ps1

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

src/CronScheduler.AspNetCore/CronScheduler.AspNetCore.csproj

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp3.1;net5.0</TargetFrameworks>
3+
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
44
</PropertyGroup>
55

66
<PropertyGroup>
77
<Description>
8-
The Cron based Scheduler for AspNetCore 2.x/3.x Applications in Kubernetes/Docker.
8+
The Cron based Scheduler for AspNetCore 2.x/3.x/5.x/6.x Applications in Kubernetes/Docker.
99
This is a lightweight alternative to Quarts Scheduler or HangFire.
1010
</Description>
1111
</PropertyGroup>
@@ -15,8 +15,9 @@
1515
</ItemGroup>
1616

1717
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
18-
<PackageReference Include="Microsoft.AspNetCore.Hosting" />
18+
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.*"/>
1919
</ItemGroup>
20+
2021
<ItemGroup>
2122
<ProjectReference Include="..\CronScheduler.Extensions\CronScheduler.Extensions.csproj" />
2223
</ItemGroup>

src/CronScheduler.AspNetCore/DependencyInjection/StartupJobWebHostExtensions.cs

+13-16
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,20 @@
55

66
using Microsoft.Extensions.DependencyInjection;
77

8-
namespace Microsoft.AspNetCore.Hosting
8+
namespace Microsoft.AspNetCore.Hosting;
9+
10+
public static class StartupJobWebHostExtensions
911
{
10-
public static class StartupJobWebHostExtensions
12+
/// <summary>
13+
/// Runs async all of the registered <see cref="IStartupJob"/> jobs.
14+
/// </summary>
15+
/// <param name="host"></param>
16+
/// <param name="cancellationToken"></param>
17+
/// <returns></returns>
18+
public static async Task RunStartupJobsAync(this IWebHost host, CancellationToken cancellationToken = default)
1119
{
12-
/// <summary>
13-
/// Runs async all of the registered <see cref="IStartupJob"/> jobs.
14-
/// </summary>
15-
/// <param name="host"></param>
16-
/// <param name="cancellationToken"></param>
17-
/// <returns></returns>
18-
public static async Task RunStartupJobsAync(this IWebHost host, CancellationToken cancellationToken = default)
19-
{
20-
using (var scope = host.Services.CreateScope())
21-
{
22-
var jobInitializer = scope.ServiceProvider.GetRequiredService<StartupJobInitializer>();
23-
await jobInitializer.StartJobsAsync(cancellationToken);
24-
}
25-
}
20+
using var scope = host.Services.CreateScope();
21+
var jobInitializer = scope.ServiceProvider.GetRequiredService<StartupJobInitializer>();
22+
await jobInitializer.StartJobsAsync(cancellationToken);
2623
}
2724
}

0 commit comments

Comments
 (0)