Hangfire.MySqlStorage icon indicating copy to clipboard operation
Hangfire.MySqlStorage copied to clipboard

A lot of dbconnection created

Open mehmet-erdogdu opened this issue 1 year ago • 2 comments

I'm using mysql and hangfire creates a lot of dbconnections. (40+ dbconnection) My configuration settings are as follows.

.Net 6

<PackageReference Include="Hangfire.AspNetCore" Version="1.8.0-beta4" />
<PackageReference Include="Hangfire.Core" Version="1.8.0-beta4" />
<PackageReference Include="Hangfire.Dashboard.Basic.Authentication" Version="5.0.0" />
<PackageReference Include="Hangfire.MySqlStorage" Version="2.0.3" />
services.AddEntityFrameworkMySql();
services.AddDbContextPool<EfCoreContext>((serviceProvider, options) =>
{
    options.UseInternalServiceProvider(serviceProvider);
    options.UseMySql(PcSystemMemory.Keys.DbConnection, ServerVersion.AutoDetect(PcSystemMemory.Keys.DbConnection));
    options.UseLazyLoadingProxies(false);
    if (Debugger.IsAttached)
        options.EnableSensitiveDataLogging()
            .EnableDetailedErrors();
});
services.AddHangfire(config =>
{
    config.UseDarkModeSupportForDashboard();
    switch (dbType)
    {
        case EnumDbType.MSSQL:
            {
                var option = new SqlServerStorageOptions
                {
                    PrepareSchemaIfNecessary = true,
                    QueuePollInterval = TimeSpan.FromMinutes(5),
                    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    UseRecommendedIsolationLevel = true,
                    DisableGlobalLocks = true
                };

                config.UseSqlServerStorage(connectionString, option).WithJobExpirationTimeout(new TimeSpan(60, 0, 0, 0));
            }
            break;
        case EnumDbType.MYSQL:
            config.UseStorage(new MySqlStorage(connectionString + ";Allow User Variables=True", new MySqlStorageOptions()));
            break;
        default:
            throw new ArgumentOutOfRangeException(nameof(dbType), dbType, null);
    }
});
services.AddHangfireServer();
app.UseHangfireDashboard("/tasks", new DashboardOptions
{
    DashboardTitle = "Project Jobs", 
    AppPath = "/",                    
    Authorization = new[] { new MyAuthorizationFilter() },
    //IgnoreAntiforgeryToken = true
});
GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 0 });
if (!Debugger.IsAttached)
    RecurringJobs.TaskOperations();

mehmet-erdogdu avatar Sep 12 '22 11:09 mehmet-erdogdu