HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.43k stars 1.7k forks source link

Hangfire BackgroundJob keeps processing and every 30 minutes a new worker is created (PostgreSqlStore) #2433

Open MrMM1981 opened 3 months ago

MrMM1981 commented 3 months ago

I am running a .NET 8 application with

Hangfire.Core version 1.8.14 Hangfire.AspNetCore version 1.8.14 Hangfire.PostgreSql version 1.20.9

The job is scheduled as expected with background job.enqueue method, it shows up in the dashboard under processing jobs. This job can have a processing duration of multiple hours. What happens is that every 30 minutes a new worker is created and the job is never finished.

I know that the hangfire server keeps running (I see the heartbeat in the logs).

I am using the following configuration for the service.

            var connectionString = configuration.GetValue<string>("Database:ConnectionString");
            var connectionFactory = new NpgsqlConnectionFactory(connectionString, new PostgreSqlStorageOptions()
            {
                PrepareSchemaIfNecessary = true,
                UseSlidingInvisibilityTimeout = true,
                InvisibilityTimeout = TimeSpan.FromHours(12),
                DistributedLockTimeout = TimeSpan.FromHours(12),
            });

            //JobStorage.Current = new PostgreSqlStorage(connectionFactory);

            return services.AddHangfire(config =>
            {
                config.UsePostgreSqlStorage(options => options.UseConnectionFactory(connectionFactory));
            });

Desired situation would be that the job runs to completion (multiple hours of processing needed most of the time). I'm at a loss.

MrMM1981 commented 3 months ago

Fixed it by using config.UsePostgreSqlStorage(options => options.UseConnectionFactory(connectionFactory), new PostgreSqlStorageOptions() { PrepareSchemaIfNecessary = true, UseSlidingInvisibilityTimeout = true, InvisibilityTimeout = TimeSpan.FromHours(12), DistributedLockTimeout = TimeSpan.FromHours(12), }); });

when using

... return services.AddHangfire(config => { config.UsePostgreSqlStorage(options => options.UseConnectionFactory(connectionFactory)); }); the storage options are not respected.