-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Startup Jobs are run before anything else is running
- Loading branch information
1 parent
5b58171
commit c6066d9
Showing
6 changed files
with
185 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,6 @@ | |
|
||
var app = builder.Build(); | ||
|
||
await app.UseNCronJobAsync(); | ||
|
||
await app.RunAsync(); |
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 |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Shouldly; | ||
|
||
namespace NCronJob.Tests; | ||
|
||
public class RunAtStartupJobTests : JobIntegrationBase | ||
{ | ||
[Fact] | ||
public async Task UseNCronJobShouldTriggerStartupJobs() | ||
{ | ||
var builder = WebApplication.CreateSlimBuilder(); | ||
var storage = new Storage(); | ||
builder.Services.AddNCronJob(s => s.AddJob<SimpleJob>().RunAtStartup()); | ||
builder.Services.AddSingleton(_ => storage); | ||
await using var app = builder.Build(); | ||
|
||
await app.UseNCronJobAsync(); | ||
|
||
storage.Content.Count.ShouldBe(1); | ||
storage.Content[0].ShouldBe("SimpleJob"); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldStartStartupJobsBeforeApplicationIsSpunUp() | ||
{ | ||
var builder = WebApplication.CreateSlimBuilder(); | ||
var storage = new Storage(); | ||
builder.Services.AddNCronJob(s => s.AddJob<SimpleJob>().RunAtStartup()); | ||
builder.Services.AddSingleton(_ => storage); | ||
builder.Services.AddHostedService<StartingService>(); | ||
await using var app = builder.Build(); | ||
|
||
await app.UseNCronJobAsync(); | ||
await RunApp(app); | ||
|
||
storage.Content.Count.ShouldBe(2); | ||
storage.Content[0].ShouldBe("SimpleJob"); | ||
storage.Content[1].ShouldBe("StartingService"); | ||
} | ||
|
||
[Fact] | ||
public async Task StartupJobThatThrowsShouldNotPreventHostFromStarting() | ||
{ | ||
var builder = WebApplication.CreateSlimBuilder(); | ||
var storage = new Storage(); | ||
builder.Services.AddNCronJob(s => | ||
{ | ||
s.AddJob<FailingJob>().RunAtStartup(); | ||
s.AddExceptionHandler<ExceptionHandler>(); | ||
}); | ||
builder.Services.AddSingleton(_ => storage); | ||
await using var app = builder.Build(); | ||
|
||
await app.UseNCronJobAsync(); | ||
await RunApp(app); | ||
|
||
storage.Content.Count.ShouldBe(1); | ||
storage.Content[0].ShouldBe("ExceptionHandler"); | ||
} | ||
|
||
[SuppressMessage("Major Code Smell", "S108:Nested blocks of code should not be left empty", Justification = "On purpose")] | ||
private static async Task RunApp(IHost app, TimeSpan? runtime = null) | ||
{ | ||
using var cts = new CancellationTokenSource(runtime ?? TimeSpan.FromSeconds(1)); | ||
try | ||
{ | ||
await app.RunAsync(cts.Token); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
} | ||
} | ||
|
||
private sealed class Storage | ||
{ | ||
#if NET8_0 | ||
private readonly object locker = new(); | ||
#else | ||
private readonly Lock locker = new(); | ||
#endif | ||
public List<string> Content { get; private set; } = []; | ||
|
||
public void Add(string content) | ||
{ | ||
lock (locker) | ||
{ | ||
Content.Add(content); | ||
} | ||
} | ||
} | ||
|
||
private sealed class StartingService : IHostedService | ||
{ | ||
private readonly Storage storage; | ||
|
||
public StartingService(Storage storage) => this.storage = storage; | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
storage.Add("StartingService"); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
} | ||
|
||
private sealed class SimpleJob: IJob | ||
{ | ||
private readonly Storage storage; | ||
|
||
public SimpleJob(Storage storage) => this.storage = storage; | ||
|
||
public Task RunAsync(IJobExecutionContext context, CancellationToken token) | ||
{ | ||
storage.Add("SimpleJob"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
private sealed class FailingJob: IJob | ||
{ | ||
public Task RunAsync(IJobExecutionContext context, CancellationToken token) => throw new InvalidOperationException("Failed"); | ||
} | ||
|
||
private sealed class ExceptionHandler : IExceptionHandler | ||
{ | ||
private readonly Storage storage; | ||
|
||
public ExceptionHandler(Storage storage) => this.storage = storage; | ||
|
||
|
||
public Task<bool> TryHandleAsync(IJobExecutionContext jobExecutionContext, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
storage.Add("ExceptionHandler"); | ||
return Task.FromResult(true); | ||
} | ||
} | ||
} |