arnoldasgudas / Hangfire.MySqlStorage

MySql storage for Hangfire - fire-and-forget, delayed and recurring tasks runner
GNU Lesser General Public License v3.0
174 stars 114 forks source link

A lot of dbconnection created #94

Open mehmet-erdogdu opened 2 years ago

mehmet-erdogdu commented 2 years ago

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();
bcourtneyri commented 1 year ago

I'm getting something similar. The IOPS after updating to .net 6 went up 10x compared to net core 2.2 (according to AWS RDS monitoring). Did you find a solution?

mehmet-erdogdu commented 1 year ago

I was using .net 6 while affecting. now i didnt implemented yet