Skip to content

Commit

Permalink
Allow user to use DbContext pooling with Quartz (#5625)
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVuscanNx authored Jun 18, 2024
1 parent d70451e commit dff5a75
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,20 @@ public static class MySqlQuartzExtensions
/// <summary>
/// Configures the <see cref="QuartzFeature"/> to use the MySQL job store.
/// </summary>
public static QuartzFeature UseMySql(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true)
public static QuartzFeature UseMySql(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true, bool useContextPooling = false)
{
feature.Services.AddDbContextFactory<MySqlQuartzDbContext>(options =>
{
// Use MySQL migrations.
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(MySqlQuartzDbContext).Assembly.GetName().Name); });
});
if (useContextPooling)
feature.Services.AddPooledDbContextFactory<MySqlQuartzDbContext>(options => UseMySql(connectionString, options));
else
feature.Services.AddDbContextFactory<MySqlQuartzDbContext>(options => UseMySql(connectionString, options));

feature.ConfigureQuartz += quartz =>
{
quartz.UsePersistentStore(store =>
{
store.UseNewtonsoftJsonSerializer();
store.UseMySqlConnector(options => options.ConnectionString = connectionString);
if (useClustering)
store.UseClustering();
});
Expand All @@ -43,4 +42,10 @@ public static QuartzFeature UseMySql(this QuartzFeature feature, string connecti

return feature;
}

private static void UseMySql(string connectionString, DbContextOptionsBuilder options)
{
// Use MySQL migrations.
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(MySqlQuartzDbContext).Assembly.GetName().Name); });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public static class PostgreSqlQuartzExtensions
/// <summary>
/// Configures the <see cref="QuartzFeature"/> to use the PostgreSQL job store.
/// </summary>
public static QuartzFeature UsePostgreSql(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true)
public static QuartzFeature UsePostgreSql(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true, bool useContextPooling = false)
{
feature.Services.AddDbContextFactory<PostgreSqlQuartzDbContext>(options =>
{
// Use PostgreSQL migrations.
options.UseNpgsql(connectionString, sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(PostgreSqlQuartzDbContext).Assembly.GetName().Name); });
});
if (useContextPooling)
feature.Services.AddPooledDbContextFactory<PostgreSqlQuartzDbContext>(options => UseNpgsql(connectionString, options));
else
feature.Services.AddDbContextFactory<PostgreSqlQuartzDbContext>(options => UseNpgsql(connectionString, options));

feature.ConfigureQuartz += quartz =>
{
Expand All @@ -48,4 +47,10 @@ public static QuartzFeature UsePostgreSql(this QuartzFeature feature, string con

return feature;
}

private static void UseNpgsql(string connectionString, DbContextOptionsBuilder options)
{
// Use PostgreSQL migrations.
options.UseNpgsql(connectionString, sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(PostgreSqlQuartzDbContext).Assembly.GetName().Name); });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public static class SqlServerQuartzExtensions
/// <summary>
/// Configures the <see cref="QuartzFeature"/> to use the SQL Server job store.
/// </summary>
public static QuartzFeature UseSqlServer(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true)
public static QuartzFeature UseSqlServer(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useClustering = true, bool useContextPooling = false)
{
feature.Services.AddDbContextFactory<SqlServerQuartzDbContext>(options =>
{
// Use SQL Server migrations.
options.UseSqlServer(connectionString, sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(SqlServerQuartzDbContext).Assembly.GetName().Name); });
});
if (useContextPooling)
feature.Services.AddPooledDbContextFactory<SqlServerQuartzDbContext>(options => UseSqlServer(connectionString, options));
else
feature.Services.AddDbContextFactory<SqlServerQuartzDbContext>(options => UseSqlServer(connectionString, options));

feature.ConfigureQuartz += quartz =>
{
Expand All @@ -48,4 +47,10 @@ public static QuartzFeature UseSqlServer(this QuartzFeature feature, string conn

return feature;
}

private static void UseSqlServer(string connectionString, DbContextOptionsBuilder options)
{
// Use SQL Server migrations.
options.UseSqlServer(connectionString, sqlServerDbContextOptionsBuilder => { sqlServerDbContextOptionsBuilder.MigrationsAssembly(typeof(SqlServerQuartzDbContext).Assembly.GetName().Name); });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ public static class SqliteQuartzExtensions
/// <summary>
/// Configures the <see cref="QuartzFeature"/> to use the SQLite job store.
/// </summary>
public static QuartzFeature UseSqlite(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString)
public static QuartzFeature UseSqlite(this QuartzFeature feature, string connectionString = Constants.DefaultConnectionString, bool useContextPooling = false)
{
feature.Services.AddDbContextFactory<SqliteQuartzDbContext>(options =>
{
// Use SQLite migrations.
options.UseSqlite(connectionString, sqlite => { sqlite.MigrationsAssembly(typeof(SqliteQuartzDbContext).Assembly.GetName().Name); });
});
if (useContextPooling)
feature.Services.AddPooledDbContextFactory<SqliteQuartzDbContext>(options => UseSqlite(connectionString, options));
else
feature.Services.AddDbContextFactory<SqliteQuartzDbContext>(options => UseSqlite(connectionString, options));

feature.ConfigureQuartz += quartz =>
{
Expand All @@ -40,4 +39,10 @@ public static QuartzFeature UseSqlite(this QuartzFeature feature, string connect

return feature;
}

private static void UseSqlite(string connectionString, DbContextOptionsBuilder options)
{
// Use SQLite migrations.
options.UseSqlite(connectionString, sqlite => { sqlite.MigrationsAssembly(typeof(SqliteQuartzDbContext).Assembly.GetName().Name); });
}
}

0 comments on commit dff5a75

Please sign in to comment.