ASP.NET Core background tasks with NCronJob and SignalR

I was recommended NCronJob for implementing a background worker in ASP.NET Core and so I decided to give it a try, read the docs and learn this. This NuGet package is open source and works great. I implemented two simple jobs, one concurrent and one not concurrent which sends messages via SignalR.

Code: https://github.com/damienbod/AspNetCoreNCronJob

To implement a demo feature, I used a SignalR service to display both concurrent and non-concurrent messages in an ASP.NET Core Razor Pages UI. Messages are sent every five seconds, when possible. In ASP.NET Core, this only requires implementing a Hub. For this purpose, I created two methods.

using Microsoft.AspNetCore.SignalR;

namespace AspNetCoreNCronJob;

public class JobsHub : Hub
{
    public Task SendConcurrentJobsMessage(string message)
    {
        return Clients.All.SendAsync("ConcurrentJobs", message);
    }

    public Task SendNonConcurrentJobsMessage(string message)
    {
        return Clients.All.SendAsync("NonConcurrentJobs", message);
    }
}

The NCronJob is a simple class that implements the IJob interface. The RunAsync methos is run depending on how the interface is setup in the services definitions. This class uses dependency injection and sends messages to registered SignalR clients.

using Microsoft.AspNetCore.SignalR;
using NCronJob;

namespace AspNetCoreNCronJob.NCronJobServices;

[SupportsConcurrency(5)]
public class NonConconcurrentJob : IJob
{
    private readonly ILogger<NonConconcurrentJob> _logger;
    private static int _counter = 0;
    private readonly IHubContext<JobsHub> _hubContext;

    public NonConconcurrentJob(ILogger<NonConconcurrentJob> logger,
           IHubContext<JobsHub> hubContext)
    {
        _logger = logger;
        _hubContext = hubContext;
    }

    public async Task RunAsync(IJobExecutionContext context, CancellationToken token)
    {
        var count = _counter++;

        var beginMessage = $"NonConcurrentJob Job BEGIN {count} {DateTime.UtcNow}";
        await _hubContext.Clients.All.SendAsync("NonConcurrentJobs", beginMessage);
        _logger.LogInformation("{BeginMessage}", beginMessage);

        Thread.Sleep(7000);

        var endMessage = $"NonConcurrentJob Job END {count} {DateTime.UtcNow}";
        await _hubContext.Clients.All.SendAsync("NonConcurrentJobs", endMessage);
        _logger.LogInformation("{EndMessage}", endMessage);
    }
}

The ASP.NET Core UI uses the SignalR Javascript library to to connect to the SignalR server and consume the messages. The messages are displayed in the UI.

This is super simple to use and provides all of the features I need in most of my scheduling requirements.

Links

https://github.com/NCronJob-Dev/NCronJob

https://docs.ncronjob.dev/

https://steven-giesel.com/blogPost/fb1ce2ab-dd27-43ed-aaab-077adf2d15cd

https://docs.microsoft.com/en-us/aspnet/core/signalr/introduction

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.