diff --git a/src/NCronJob/Scheduler/QueueWorker.LogMessage.cs b/src/NCronJob/Scheduler/QueueWorker.LogMessage.cs index ceaaab1..6b57660 100644 --- a/src/NCronJob/Scheduler/QueueWorker.LogMessage.cs +++ b/src/NCronJob/Scheduler/QueueWorker.LogMessage.cs @@ -39,7 +39,4 @@ internal sealed partial class QueueWorker [LoggerMessage(LogLevel.Information, "Job removed from queue: {JobType} at {RunAt}")] private partial void LogJobRemovedFromQueue(string jobType, DateTime? runAt); - - [LoggerMessage(LogLevel.Warning, "The UseNCronJob(Async) method was not called. Startup jobs might not have been executed.")] - private partial void LogUseNCronJobNotCalled(); } diff --git a/src/NCronJob/Scheduler/QueueWorker.cs b/src/NCronJob/Scheduler/QueueWorker.cs index 265e27a..0ffd092 100644 --- a/src/NCronJob/Scheduler/QueueWorker.cs +++ b/src/NCronJob/Scheduler/QueueWorker.cs @@ -122,10 +122,20 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) private void AssertUseNCronJobWasCalled() { - if (!missingMethodCalledHandler.UseWasCalled) + if (missingMethodCalledHandler.UseWasCalled) { - LogUseNCronJobNotCalled(); + return; + } + + if (jobRegistry.GetAllOneTimeJobs().Count == 0) + { + return; } + + throw new InvalidOperationException( + $""" + Startup jobs have been registered. However, neither IHost.UseNCronJobAsync(), nor IHost.UseNCronJob() have been been called. + """); } private void CreateWorkerQueues(CancellationToken stopToken) diff --git a/tests/NCronJob.Tests/RunAtStartupJobTests.cs b/tests/NCronJob.Tests/RunAtStartupJobTests.cs index 6321893..d56c9cb 100644 --- a/tests/NCronJob.Tests/RunAtStartupJobTests.cs +++ b/tests/NCronJob.Tests/RunAtStartupJobTests.cs @@ -7,6 +7,26 @@ namespace NCronJob.Tests; public class RunAtStartupJobTests : JobIntegrationBase { + [Fact] + public async Task UseNCronJobIsMandatoryWhenStartupJobsAreDefined() + { + var builder = Host.CreateDefaultBuilder(); + var storage = new Storage(); + builder.ConfigureServices(services => + { + services.AddNCronJob(s => s.AddJob().RunAtStartup()); + services.AddSingleton(_ => storage); + }); + + using var app = builder.Build(); + +#pragma warning disable IDISP013 // Await in using + Func act = () => RunApp(app); +#pragma warning restore IDISP013 // Await in using + + await act.ShouldThrowAsync(); + } + [Fact] public async Task UseNCronJobShouldTriggerStartupJobs() {