NCronJob 4.0.2
dotnet add package NCronJob --version 4.0.2
NuGet\Install-Package NCronJob -Version 4.0.2
<PackageReference Include="NCronJob" Version="4.0.2" />
paket add NCronJob --version 4.0.2
#r "nuget: NCronJob, 4.0.2"
// Install NCronJob as a Cake Addin #addin nuget:?package=NCronJob&version=4.0.2 // Install NCronJob as a Cake Tool #tool nuget:?package=NCronJob&version=4.0.2
<h1 align="center">NCronJob</h1>
<p align="center"> <img src="/assets/logo_small.png" alt="logo" width="120px" height="120px"/> <br> <em>Scheduling made easy</em> <br> </p>
NCronJob
A Job Scheduler sitting on top of IHostedService
in .NET.
Often, one finds oneself between the simplicity of BackgroundService
/IHostedService
and the complexity of
a full-blown scheduler like Hangfire
or Quartz
.
This library aims to fill that gap by providing a simple and easy-to-use job scheduler that can be used in any .NET
application and feels "native".
There's no need to set up a database—just schedule your tasks right away! The library provides two ways of scheduling jobs:
- Instant jobs - Run a job immediately (or with a small delay, or at a specific date and time).
- Cron jobs - Schedule a job using a cron expression.
The whole documentation can be found here: NCronJob Documentation
Features
- The ability to schedule jobs using a cron expression.
- The ability to instantly run a job.
- Parameterized jobs - Instant as well as cron jobs!
- Integration with ASP.NET - Access your DI container like you would in any other service.
- Get notified when a job is done (either successfully or with an error).
- Retries - If a job fails, it will be retried.
- The job scheduler supports TimeZones. Defaults to UTC time.
- Minimal API for Jobs - Implement jobs in a one-liner.
- Startup jobs - Run a job when the application starts.
- Define job dependencies - trigger another job if one was successful or faulted!
- Add, remove, and update jobs at runtime.
Not features
As this is a simple scheduler, some features are not included by design. If you need these features, you might want to
look into a more advanced scheduler like Hangfire
or Quartz
.
- Job persistence - Jobs are not persisted between restarts of the application.
- Job history - There is no history of jobs that have been run.
- Progress state - There is no way to track the progress of a job. The library supports notifying when a job is done, but not the progress of the job itself.
Short example
There are two ways to define a job.
Minimal Job API
You can use this library in a simple one-liner:
builder.Services.AddNCronJob((ILoggerFactory factory, TimeProvider timeProvider) =>
{
var logger = factory.CreateLogger("My Anonymous Job");
logger.LogInformation("Hello World - The current date and time is {Time}", timeProvider.GetLocalNow());
}, "*/5 * * * * *");
var app = builder.Build();
await app.UseNCronJobAsync();
app.Run();
With this simple lambda, you can define a job that runs every 5 seconds. Pass in all dependencies, just like you would with a Minimal API.
Via the IJob
interface
- Import the namespace (or let your IDE do the dirty work)
using NCronJob;
- Create a job
public class PrintHelloWorld : IJob
{
private readonly ILogger<PrintHelloWorld> logger;
public PrintHelloWorld(ILogger<PrintHelloWorld> logger)
{
this.logger = logger;
}
public Task RunAsync(IJobExecutionContext context, CancellationToken token)
{
logger.LogInformation("Hello World");
logger.LogInformation("Parameter: {Parameter}", context.Parameter);
return Task.CompletedTask;
}
}
- Register the NCronJob and the job in your
Program.cs
builder.Services.AddNCronJob(options =>
options.AddJob<PrintHelloWorld>(j =>
{
// Every minute and optional parameter
j.WithCronExpression("* * * * *")
.WithParameter("Hello World");
}));
- Run your application and see the magic happen!
Triggering an instant job
If the need arises and you want to trigger a job instantly, you can do so:
public class MyService
{
private readonly IInstantJobRegistry jobRegistry;
public MyService(IInstantJobRegistry jobRegistry) => this.jobRegistry = jobRegistry;
public void MyMethod() => jobRegistry.RunInstantJob<MyJob>("I am an optional parameter");
// Alternatively, you can also run an anonymous job
public void MyOtherMethod() => jobRegistry.RunInstantJob((MyOtherService service) => service.Do());
}
Running a Job at Startup
If you want a job to run when the application starts, you can configure it to run at startup using the RunAtStartup
method. Here is an example:
builder.Services.AddNCronJob(options =>
{
options.AddJob<MyJob>()
.RunAtStartup();
});
var app = builder.Build();
// Here the startup jobs will be executed
await app.UseNCronJobAsync();
app.Run();
In this example, the job of type 'MyJob' will be executed as soon as the application starts. This is useful for tasks that need to run immediately upon application startup, such as initial data loading or cleanup tasks.
Defining Job Dependencies
First you need to import data and then transform it? Well, but how do you make sure that the data is imported before you transform it? Sure, you could just give a delay, but what if the import takes longer than expected? This is where job dependencies come in handy!
builder.Services.AddNCronJob(options =>
{
options.AddJob<ImportData>(p => p.WithCronExpression("0 0 * * *")
.ExecuteWhen(
success: s => s.RunJob<TransformData>("Optional Parameter"),
faulted: s => s.RunJob<Notify>("Another Optional Parameter"));
});
You just want to trigger a service and don't want to define a whole new job? No problem! The Minimal API is available here as well:
builder.Services.AddNCronJob(options =>
{
options.AddJob<ImportData>(p => p.WithCronExpression("0 0 * * *")
.ExecuteWhen(
success: s => s.RunJob(async (ITransformer transformer) => await transformer.TransformDataAsync()),
faulted: s => s.RunJob(async (INotificationService notifier) => await notifier.NotifyAsync())
});
Support & Contributing
Thanks to all contributors and people who are creating bug reports and valuable input:
<a href="https://github.com/NCronJob-Dev/NCronJob/graphs/contributors"> <img src="https://contrib.rocks/image?repo=NCronJob-Dev/NCronJob" alt="Supporters" /> </a>
If you have any questions or suggestions, feel free to open a new issue or pull request. Do you want to contribute? Great! We have a predefined codespace for you to get started right away! With that you don't need any local setup and can start right away! Either coding or updating the documentation - everything is set and done for you!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. |
-
net8.0
- Cronos (>= 0.9.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Polly (>= 8.5.0)
-
net9.0
- Cronos (>= 0.9.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 9.0.0)
- Polly (>= 8.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (1)
Showing the top 1 popular GitHub repositories that depend on NCronJob:
Repository | Stars |
---|---|
linkdotnet/Blog
A blog (engine) completely written in C# and Blazor. It aims to be a simple use and easy to extend platform. Blogposts are written in Markdown and are rendered to HTML. This gives all the flexibility needed to express yourself but also have an easy way of creating posts in the first place.
|
Version | Downloads | Last updated |
---|---|---|
4.0.2 | 54 | 12/28/2024 |
4.0.1-preview | 36 | 12/28/2024 |
4.0.0-preview | 58 | 12/24/2024 |
3.3.8 | 1,925 | 11/16/2024 |
3.3.7-preview | 77 | 11/16/2024 |
3.3.6-preview | 73 | 11/16/2024 |
3.3.5 | 587 | 11/4/2024 |
3.3.4 | 103 | 11/3/2024 |
3.3.3 | 191 | 10/31/2024 |
3.3.2 | 208 | 10/27/2024 |
3.3.1-preview | 113 | 10/26/2024 |
3.3.0-preview | 84 | 10/20/2024 |
3.2.0 | 312 | 10/19/2024 |
3.1.3 | 122 | 10/17/2024 |
3.1.2-preview | 122 | 10/15/2024 |
3.1.1-preview | 76 | 10/14/2024 |
3.1.0-preview | 71 | 10/14/2024 |
3.0.3 | 458 | 9/15/2024 |
3.0.2-preview | 172 | 8/29/2024 |
3.0.1-preview | 122 | 8/18/2024 |
3.0.0-preview | 112 | 7/21/2024 |
2.8.6 | 583 | 8/29/2024 |
2.8.5 | 138 | 8/18/2024 |
2.8.4 | 1,312 | 6/23/2024 |
2.8.3 | 120 | 6/20/2024 |
2.8.2 | 163 | 6/18/2024 |
2.8.1 | 113 | 6/18/2024 |
2.8.0 | 108 | 6/18/2024 |
2.7.10-preview | 102 | 6/18/2024 |
2.7.9-preview | 101 | 6/17/2024 |
2.7.8-preview | 89 | 6/8/2024 |
2.7.7-preview | 85 | 6/8/2024 |
2.7.6-preview | 91 | 6/6/2024 |
2.7.5-preview | 72 | 6/4/2024 |
2.7.4 | 157 | 6/3/2024 |
2.7.3 | 122 | 6/1/2024 |
2.7.1-preview | 65 | 6/1/2024 |
2.7.0-preview | 62 | 6/1/2024 |
2.6.5-preview | 65 | 5/30/2024 |
2.6.4-preview | 61 | 5/29/2024 |
2.6.3-preview | 99 | 5/29/2024 |
2.6.2-preview | 90 | 5/28/2024 |
2.6.1 | 322 | 5/25/2024 |
2.5.0 | 892 | 5/21/2024 |
Changes in NCronJob
See the full changelog at https://github.com/NCronJob-Dev/NCronJob/releases