From 49ca27ea0aab93e34df17141c910b3ad6959c64e Mon Sep 17 00:00:00 2001 From: Frank Alvarez Date: Thu, 23 May 2024 14:58:54 -0400 Subject: [PATCH] add startup-jobs documentation --- docs/features/minimal-api.md | 7 ++-- docs/features/startup-jobs.md | 72 +++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + 3 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 docs/features/startup-jobs.md diff --git a/docs/features/minimal-api.md b/docs/features/minimal-api.md index 09a1e279..3f320f4c 100644 --- a/docs/features/minimal-api.md +++ b/docs/features/minimal-api.md @@ -1,4 +1,5 @@ -# Minimal API +# Minimal API + The minimal job API offers another way of defining a cron job. It favors simplicity over feature richness. A job can be defined as such: ```csharp @@ -64,7 +65,7 @@ builder.Services.AddNCronJob([RetryPolicy(retryCount: 3)] (JobExecutionContext c }, "0 * * * *"); ``` -## Concurrency-Support +## Concurrency-Support In the same way, the concurrency level can be controlled (see [**Concurrency**](concurrency-control.md)): ```csharp @@ -73,7 +74,7 @@ builder.Services.AddNCronJob([SupportsConcurrency(2)] () => { }, "0 * * * *"); Now, the job can only be executed by two instances at the same time. -## Restrictions +## Restrictions The minimal API has some restrictions over the "full approach": diff --git a/docs/features/startup-jobs.md b/docs/features/startup-jobs.md new file mode 100644 index 00000000..d3d7cb8f --- /dev/null +++ b/docs/features/startup-jobs.md @@ -0,0 +1,72 @@ +# Running Startup Jobs + +**NCronJob** allows you to configure jobs to run at application startup. This is useful for tasks that need to be executed immediately when the application starts, such as initial data loading, cleanup tasks, or other setup procedures. + +Startup jobs are defined like regular CRON jobs and inherit from `IJob`. The only difference is that they are configured to run at startup. **All** startup jobs must be completed prior to the start of any other CRON (or instant) jobs. + +```csharp +public class MyStartupJob : IJob +{ + public Task RunAsync(JobExecutionContext context, CancellationToken token) + { + // Perform startup task + return Task.CompletedTask; + } +} + +``` + +As with CRON jobs, they must be registered in the `AddNCronJob` method. + +```csharp +Services.AddNCronJob(options => +{ + options.AddJob() + .RunAtStartup(); // Configure the job to run at startup +}); +``` + +The `RunAtStartup` method ensures that the job is executed as soon as the application starts. This method is useful for scenarios where certain tasks need to be performed immediately upon application launch. + +## Example Use Case + +Consider an application that needs to load initial data from a database or perform some cleanup tasks whenever it starts. You can define and configure a startup job to handle this: + +### Job Definition +```csharp +public class InitialDataLoader : IJob +{ + private readonly IDataService _dataService; + + public InitialDataLoader(IDataService dataService) + { + _dataService = dataService; + } + + public async Task RunAsync(JobExecutionContext context, CancellationToken token) + { + await _dataService.LoadInitialDataAsync(); + } +} +``` + +### Registering the Job + +In your `Program.cs` or `Startup.cs` file, register the job and configure it to run at startup: + +```csharp +builder.Services.AddNCronJob(options => +{ + options.AddJob() + .RunAtStartup(); +}); +``` + +This setup ensures that the `InitialDataLoader` job will be executed as soon as the application starts, loading the necessary initial data. + + +## Summary + +Startup jobs are a powerful feature of **NCronJob** that enable you to execute critical tasks immediately upon application startup. By using the `RunAtStartup` method, you can ensure that your application performs necessary setup procedures, data loading, or cleanup tasks right at the beginning of its lifecycle. + +This feature is particularly useful for applications that require certain operations to be completed before they are fully functional. By configuring startup jobs, you can streamline your application's initialization process and improve its overall reliability and performance. diff --git a/mkdocs.yml b/mkdocs.yml index 713adfc0..9f4cf303 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -37,6 +37,7 @@ nav: - Concurrency control: features/concurrency-control.md - Retry support: features/retry-support.md - Minimal API: features/minimal-api.md + - Startup Jobs: features/startup-jobs.md - Migration: - v2 Migration Guide: migration/v2.md - Advanced: