Hangfire Server In .Net Core Worker Service Not Completing BatchJob after all BackgroundJobs are completed

dashboard
Tags: #<Tag:0x00007f64b62cf598>

#1

Using BackgroundJobServer in a .Net Core Worker BackgroundService but the Hangfire BatchJobs are not completing after all BackGroundJobs are done and marked as completed for the Batch not status bar is gray and all jobs are complete but it just hangs there and doesn’t start the next BatchJob


using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Hangfire;
using HangFireProBatch.Service;

using Hangfire.SqlServer;
using Microsoft.Extensions.DependencyInjection;

namespace HangfireWindowServer
{
public class Worker : BackgroundService
{
private readonly ILogger _logger;
private BackgroundJobServer _server;
private IServiceScopeFactory _serviceFactory;
private IServiceProvider _serviceProvider;

    public Worker(ILogger<Worker> logger, IServiceScopeFactory serviceFactory, IServiceProvider serviceProvider )
    {

        var options = new SqlServerStorageOptions {

            CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
            SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
            QueuePollInterval = TimeSpan.Zero,
            UseRecommendedIsolationLevel = true,
            UsePageLocksOnDequeue = true,
            DisableGlobalLocks = true,


            

        };
        _serviceProvider = serviceProvider;
        _logger = logger;
        GlobalConfiguration.Configuration.UseSqlServerStorage("Data Source=.;Initial Catalog=HangfireR3;Integrated Security=True", options)
            .UseActivator(new ServiceJobActivator(serviceProvider.GetService<IServiceScopeFactory>()));
          
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {

        GlobalConfiguration.Configuration.UseActivator(new ServiceJobActivator(_serviceProvider.GetService<IServiceScopeFactory>()));
        _server = new BackgroundJobServer();


      
    }
}

public class ServiceJobActivator : JobActivator
{
    readonly IServiceScopeFactory _serviceScopeFactory;
    public ServiceJobActivator(IServiceScopeFactory serviceScopeFactory)
    {
        if (serviceScopeFactory == null) throw new ArgumentNullException(nameof(serviceScopeFactory));
        _serviceScopeFactory = serviceScopeFactory;
    }

    public override JobActivatorScope BeginScope(JobActivatorContext context)
    {
        return new ServiceJobActivatorScope(_serviceScopeFactory.CreateScope());
    }
}

public class ServiceJobActivatorScope : JobActivatorScope
{
    readonly IServiceScope _serviceScope;
    public ServiceJobActivatorScope(IServiceScope serviceScope)
    {
        if (serviceScope == null) throw new ArgumentNullException(nameof(serviceScope));
        _serviceScope = serviceScope;
    }
    public override object Resolve(Type type)
    {
        return _serviceScope.ServiceProvider.GetService(type);
    }
}

}


#2

Installed Hangfire Pro Nuget Package and added below to the Configuration:

GlobalConfiguration.Configuration.UseSqlServerStorage(“ConnectionStringGoesHere”, options)
.UseActivator(new ServiceJobActivator(serviceProvider.GetService()))
.UseBatches();