LinkDotNet.NCronJob
2.4.6
The repository and package moved to a new organization and name. Use NCronJob instead!
Read more about this in our announcement: https://github.com/NCronJob-Dev/NCronJob/discussions/66
dotnet add package LinkDotNet.NCronJob --version 2.4.6
NuGet\Install-Package LinkDotNet.NCronJob -Version 2.4.6
<PackageReference Include="LinkDotNet.NCronJob" Version="2.4.6" />
paket add LinkDotNet.NCronJob --version 2.4.6
#r "nuget: LinkDotNet.NCronJob, 2.4.6"
// Install LinkDotNet.NCronJob as a Cake Addin #addin nuget:?package=LinkDotNet.NCronJob&version=2.4.6 // Install LinkDotNet.NCronJob as a Cake Tool #tool nuget:?package=LinkDotNet.NCronJob&version=2.4.6
<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 dotnet.
Often times one finds themself between the simplicity of the BackgroundService
/IHostedService
and the complexity of
a full-blown Hangfire
or Quartz
scheduler.
This library aims to fill that gap by providing a simple and easy to use job scheduler that can be used in any dotnet
application and feels "native".
So no need for setting up a database, just schedule your stuff right away! The library gives you two ways of scheduling jobs:
- Instant jobs - just run a job right away (or with a small delay; or with a given 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!
- Integrated in 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 an oneliner
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 will support notifying when a job is done, but not the progress of the job itself.
Short example
There are two ways of defining 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 * * * * *");
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 LinkDotNet.NCronJob;
- Create a job
public class PrintHelloWorld : IJob
{
private readonly ILogger<PrintHelloWorld> logger;
public PrintHelloWorld(ILogger<PrintHelloWorld> logger)
{
this.logger = logger;
}
public Task RunAsync(JobExecutionContext 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");
}
Support & Contributing
Thanks to all contributors and people that are creating bug-reports and valuable input:
<a href="https://github.com/linkdotnet/NCronJob/graphs/contributors"> <img src="https://contrib.rocks/image?repo=linkdotnet/NCronJob" alt="Supporters" /> </a>
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.8.4)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Polly (>= 8.3.1)
-
net9.0
- Cronos (>= 0.8.4)
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Polly (>= 8.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated | |
---|---|---|---|
2.4.6 | 533 | 5/21/2024 | |
2.4.5 | 329 | 5/20/2024 | |
2.4.4 | 110 | 5/20/2024 | |
2.4.3-preview | 96 | 5/20/2024 | |
2.4.1-preview | 94 | 5/18/2024 | |
2.4.0-preview | 109 | 5/8/2024 | |
2.3.2 | 235 | 5/8/2024 | |
2.3.1 | 124 | 5/7/2024 | |
2.3.0-preview | 117 | 5/7/2024 | |
2.2.1 | 136 | 5/5/2024 | |
2.2.0-preview | 125 | 5/4/2024 | |
2.1.4 | 145 | 5/1/2024 | |
2.1.3-preview | 107 | 4/30/2024 | |
2.1.2-preview | 106 | 4/28/2024 | |
2.1.1-preview | 104 | 4/27/2024 | |
2.1.0-preview | 106 | 4/25/2024 | |
2.0.5 | 285 | 4/19/2024 | |
2.0.4 | 145 | 4/16/2024 | |
2.0.3 | 130 | 4/15/2024 | |
2.0.2-preview | 104 | 4/15/2024 | |
2.0.1-preview | 106 | 4/15/2024 | |
2.0.0-preview | 135 | 4/14/2024 | |
1.0.2 | 242 | 4/11/2024 | |
1.0.1-preview | 117 | 4/10/2024 | |
1.0.0-preview | 98 | 4/10/2024 | |
0.13.2 | 152 | 3/29/2024 | |
0.13.1 | 191 | 3/25/2024 | |
0.13.0 | 244 | 3/23/2024 | |
0.12.0 | 155 | 3/22/2024 | |
0.11.5 | 134 | 3/22/2024 | |
0.11.4 | 133 | 3/21/2024 | |
0.11.3 | 120 | 3/21/2024 | |
0.11.2-preview | 102 | 3/21/2024 | |
0.11.1-preview | 97 | 3/21/2024 | |
0.11.0-preview | 111 | 3/21/2024 | |
0.10.1 | 142 | 3/19/2024 | |
0.10.0 | 145 | 3/18/2024 | |
0.9.3 | 127 | 3/18/2024 | |
0.9.2 | 133 | 3/17/2024 | |
0.9.1 | 134 | 3/17/2024 |
Changes in NCronJob
### Fixed
- Retry/Concurrency Attributes where ignored
See the full changelog at https://github.com/linkdotnet/NCronJob/releases