NCronJob - Big Updates

5/13/2024
4 minute read

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.

UPDATE and Announcement: We moved the library to its own organization and package. Read the announcement: https://github.com/NCronJob-Dev/NCronJob/discussions/66

NCronJob

NCronJob is a simple and lightweight library for creating and managing cron jobs in .NET. Here the link to the library: https://github.com/NCronJob-Dev/NCronJob Of course giving it a star or any feedback is always appreciated.

The basic setup is very very simple:

builder.Services.AddNCronJob(options =>
    options.AddJob<PrintHelloWorld>(j => 
    {
        // Run every hour one time with "Foo" and one time with "Bar"
        j.WithCronExpression("0 * * * *").WithParameter("Foo")
         .And
         .WithCronExpression("0 * * * *").WithParameter("Bar");
    }));

For folks that already used NCronJob before, you might have noticed that the API has changed a bit and is (IMHO) more fluent and easier to use. Besides the API, also a big shoutout to @falvarez1 and the others that contributed to the project! It is really appreciated.

New Features

Concurrency

In earlier version, before v2, jobs can run in parallel as much as they want. With v2 we flipped that behavior. So by default, jobs are not running in parallel. This means that if a job is running and the next run is due, the next run will be skipped. This is a breaking change, but we think it is the right way to go. If you want to run jobs in parallel, just decorate your job with SupportsConcurrency attribute.

[SupportsConcurrency(10)]
public class ConcurrentJob : IJob

This allows up to 10 instances of the job to run in parallel. By the way, this is also allowed:

[SupportsConcurrency]
public class ConcurrentJob : IJob

If no degree of parallelism is specified, we will take the CPU count as the default value.

Retry Support

Retry support is a new feature that allows you to retry a job if it fails. This is especially useful if you have a job that is dependent on an external service that might be down for a short period of time. You can specify the number of retries and the delay between the retries.

[RetryPolicy(retryCount: 4)]
public class RetryJob(ILogger<RetryJob> logger) : IJob

We allow an optional parameter that defines the delay strategy:

[RetryPolicy(4, PolicyType.FixedInterval)]
public class FixedIntervalRetryJob(ILogger<FixedIntervalRetryJob> logger) : IJob

There are two strategies available:

  • ExponentialBackoff: Increases the delay between retry attempts exponentially.
  • FixedInterval: Keeps the delay between retry attempts consistent.

Of course you can also define your custom retry policy if you need this.

Time Zone Support

Time zone support is another new feature that allows you to specify the time zone in which the cron expression should be evaluated. This is especially useful if you have jobs that need to run in different time zones and/or need stability even with leap years or daylight saving time.

The basic ideas is as follows:

var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Europe Standard Time");
j.WithCronExpression("0 0 * * *", timeZoneInfo: timeZone);

What we are planning to do

Right now, those are very convenient features - but we want bigger things. Mainly two:

  1. Allowing to define dependencies so that automatically a job is run after success or failure
  2. Dashboard and monitoring capabilities

Dependencies can be easily modeled in code like this:

builder.Services.AddNCronJob(n => n
    .AddJob<MyJob>(p =>
        p.WithCronExpression("*/20 * * * * *"))
    .When(success: d => d.RunJob<SuccessJob>("Success"), faulted: d => d.RunJob<FaultedJob>("Faulted"))

That allows a dependency tree as such: flowchart

Right now, we have to iron out some stuff (currently, you are allowed to define cycles - not sure if one really wants that).

Here is a very early alpha picture of the dashboard (of course, in Blazor): dashboard

So stay tuned!

Documentation

Until now, we lacked documentation and proper examples (everything was in the README.md), but here we are: https://ncronjob-dev.github.io/NCronJob/

There you can find all the information and more!

.NET 8 and Blazor United / Server-side rendering

New .NET and new Blazor features. In this blog post, I want to highlight the new features that are hitting us with .NET 8 in the Blazor world. So let's see what's new.

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!

NCronJob - Scheduling made easy

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!

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