Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hangfire.PostgreSql configuration specified in appsettings is not applied #2650

Closed
Grosam opened this issue May 1, 2023 · 3 comments
Closed
Assignees

Comments

@Grosam
Copy link

Grosam commented May 1, 2023

Describe the bug

The Hangfire configuration for PostgreSQL connector is not applied so when the platform starts, we always get Hangfire configuration with default values.
That can easily be noticed if you enable hangfire information logs.

Steps to reproduce
Steps to reproduce the behavior:

  1. Set "PostgreSql" as DB provider and specify a DB connection string in appsettings.
  2. Set any values in the PostgreSqlStorageOptions appsetting section e.g.
  "VirtoCommerce": {
    ...
    "Hangfire": {
      ...
      "PostgreSqlStorageOptions": {
        "InvisibilityTimeout": "03:00:00",
        "QueuePollInterval": "00:00:05",
        "UseRecommendedIsolationLevel": true,
        "UsePageLocksOnDequeue": true,
        "DisableGlobalLocks": true
      }
    }
  1. Enable Hangfire logging by adding the following appsetting:
  "Logging": {
    ...
    "LogLevel": {
      ...
      "Hangfire": "Information"
    }
  }
  1. Run the platform and note that despite setting specific values in appsettings we always get default values for InvisibilityTimeout and QueuePollInterval options like these:
info: Hangfire.PostgreSql.PostgreSqlStorage[0]
      Start installing Hangfire SQL objects...
info: Hangfire.PostgreSql.PostgreSqlStorage[0]
      Hangfire SQL objects installed.
info: Hangfire.BackgroundJobServer[0]
      Starting Hangfire Server using job storage: 'PostgreSQL Server: Host: localhost, DB: virtocommerce, Schema: hangfire'
info: Hangfire.BackgroundJobServer[0]
      Using the following options for SQL Server job storage:
info: Hangfire.BackgroundJobServer[0]
          Queue poll interval: 00:00:15.
info: Hangfire.BackgroundJobServer[0]
          Invisibility timeout: 00:30:00.
info: Hangfire.BackgroundJobServer[0]
      Using the following options for Hangfire Server:
          Worker count: 20
          Listening queues: 'mt-message-queue'
          Shutdown timeout: 00:00:15
          Schedule polling interval: 00:00:15

Expected behavior
I would expect the Hanfire configuration in logs to be the same as the one specified in appsettings.

Version info (please complete the following information):

  • Browser version: Firefox 112.0.2
  • Platform version: 3.270.0

Additional context (optional)
Probably the cause of the issue is that Hangfire settings are set in two places and the second attempt to set them is not successful due to a bug in Hangfire.PostgreSql connector. I looked through the release changes of the connector and it doesn't seem that they already fixed the issue.
It seems that the issue can be fixed if we modify the first place where Hangfire settings are set the following way to set appsetting values there (that's worked for me):

namespace VirtoCommerce.Platform.Hangfire.Extensions
{
    public static class ServiceCollectionExtensions
    {
        public static IGlobalConfiguration AddHangfireStorage(this IGlobalConfiguration globalConfiguration, IConfiguration configuration, HangfireOptions hangfireOptions)
        {
            var databaseProvider = configuration.GetValue("DatabaseProvider", "SqlServer");

            hangfireOptions.SqlServerStorageOptions.PrepareSchemaIfNecessary = false;
            hangfireOptions.PostgreSqlStorageOptions.PrepareSchemaIfNecessary = false;
            hangfireOptions.MySqlStorageOptions.PrepareSchemaIfNecessary = false;

            switch (databaseProvider)
            {
                case "PostgreSql":
                    globalConfiguration.UsePostgreSqlStorage(configuration.GetConnectionString("VirtoCommerce"),
                        hangfireOptions.PostgreSqlStorageOptions);
                    break;
                case "MySql":
                    globalConfiguration.UseStorage(new MySqlStorage(configuration.GetConnectionString("VirtoCommerce"),
                        hangfireOptions.MySqlStorageOptions));
                    break;
                default:
                    // Call UseSqlServerStorage with fake SqlServerStorageOptions to avoid Hangfire tries to apply its migrations because these never do in case of database absence.
                    // Real options provided in ApplicationBuilderExtensions.UseHangfire where migrations forced to apply.
                    globalConfiguration.UseSqlServerStorage(configuration.GetConnectionString("VirtoCommerce"),
                        hangfireOptions.SqlServerStorageOptions);
                    break;
            }

            return globalConfiguration;
        }

        public static object AddHangfire(this IServiceCollection services, IConfiguration configuration)
        {
            var section = configuration.GetSection("VirtoCommerce:Hangfire");
            var hangfireOptions = new HangfireOptions();
            section.Bind(hangfireOptions);
            services.AddOptions<HangfireOptions>().Bind(section).ValidateDataAnnotations();

            GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = hangfireOptions.AutomaticRetryCount });

            if (hangfireOptions.JobStorageType == HangfireJobStorageType.SqlServer ||
                hangfireOptions.JobStorageType == HangfireJobStorageType.Database )
            {
                services.AddHangfire(c => c.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                    .UseSimpleAssemblyNameTypeSerializer()
                    .UseRecommendedSerializerSettings()
                    .AddHangfireStorage(configuration, hangfireOptions));
            }
            else
            {
                services.AddHangfire(config => config.UseMemoryStorage());
            }

            return services;
        }
    }
}
@OlegoO OlegoO self-assigned this May 2, 2023
@mvktsk
Copy link
Contributor

mvktsk commented Aug 23, 2023

Task https://virtocommerce.atlassian.net/browse/VP-8416 has been created

@OlegoO
Copy link
Contributor

OlegoO commented Sep 21, 2023

Added PR with fix #2697

@OlegoO OlegoO closed this as completed Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants