NCronJob - Scheduling made easy

3/18/2024
3 minute read

Hangfire/Quartz or BackgroundService? Why not something in the middle? Did you ask yourself this question from time to time? Do you want to have a full-blown job scheduler with lots of setups, but more than BackgroundService is needed?

Meet: NCronJob!

What is NCronJob?

A Job Scheduler sitting on top of IHostedService in dotnet.

Often times one finds themself between the simplicisty 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:

  1. Instant jobs - just run a job right away
  2. Cron jobs - schedule a job using a cron expression

Source: https://github.com/linkdotnet/NCronJob

The idea is to have a simple and easy way to schedule jobs - either recurring via cron notation or one-time jobs (triggered via instant jobs). The library is built on top of IHostedService and is therefore a perfect fit for ASP.NET applications. It feels just like ASP.NET.

The major features that distinguish NCronJob from BackgroundService are:

  • Cron Jobs: Schedule jobs using cron expressions
  • Instant Jobs: Run a job right away - trigger them from whereever you want
  • Pass in parameters to your jobs (cron as well as instant jobs)
  • (Soon) Get notified when jobs are done

With the simplicity there are also some trade-offs: There is no database (which is a plus because you don't have to setup anything), but this also means that there is no persistence of jobs. If your application restarts, the history is gone. But that is "on purpose".

How to use it?

The library is available as a NuGet package. Just install it, and you will be ready to go.

dotnet add package LinkDotNet.NCronJob

The usage is very straightforward. First you have to define your job:

public class PrintHelloWorld : IJob
{
    private readonly ILogger<PrintHelloWorld> logger;

    public PrintHelloWorld(ILogger<PrintHelloWorld> logger)
    {
        this.logger = logger;
    }

	public Task Run(JobExecutionContext context, CancellationToken token = default)
    {
    	logger.LogInformation("Hello World");
    	logger.LogInformation("Parameter: {Parameter}", context.Parameter);

        return Task.CompletedTask;
    }
}

As you can see, even parameters are supported. This is true for jobs that are triggered via cron as well as for instant jobs. Now let's register the service:

builder.Services.AddNCronJob();
builder.Services.AddCronJob<PrintHelloWorld>(options => 
{
	// Every minute
	options.CronExpression = "* * * * *";
	// Optional parameter
	options.Parameter = "Hello World";
});

And well - we are done! That is the simplicity of that library. You can also trigger instant jobs from wherever you want:

public class MyService
{
  private readonly IInstantJobRegistry jobRegistry;
  
  public MyService(IInstantJobRegistry jobRegistry) => this.jobRegistry = jobRegistry;

  public void MyMethod() => jobRegistry.AddInstantJob<MyJob>("I am an optional parameter");
}

Docs / Source / Support

Currently we are trying to add more features - so if you are missing a crucial part, we should cover, just let us know on GitHub. Every Feedback/Issue/PR is very welcome!

NCronJob - Big Updates

Since the last blog post a lot has happened and many new features have been added to NCronJob. In this blog post I would like to introduce you to the new features and explain how you can use them.

NCronJob - June Edition

The last update of NCronJob was some time ago - and as always, there are some new features in the meantime. So here we are, let's go through to highlight them!

How to write your own cron Job scheduler in ASP.NET Core (like Quartz, Hangfire, ...)

In this blog post we will discover how to write your own small task scheduler / job scheduler with cron notation in ASP.NET Core. You might know similar approaches under the name of Quartz or Hangfire.

With the help of BackgroundService we will build our own, lightweight version of it.

An error has occurred. This application may no longer respond until reloaded. Reload x