Skip to content

Commit

Permalink
Long living locks (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahydrax authored Sep 21, 2021
1 parent 351fa27 commit aec291d
Show file tree
Hide file tree
Showing 56 changed files with 1,511 additions and 1,776 deletions.
4 changes: 4 additions & 0 deletions Hangfire.PostgreSql.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,9 @@
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=Constants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateConstants/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="_" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Hangfire_002ECore_002ETests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Hangfire_002EPostgreSql_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">False</s:Boolean>
<s:Boolean x:Key="/Default/Environment/Filtering/ExcludeCoverageFilters/=Hangfire_002ESqlServer_002ETests_003B_002A_003B_002A_003B_002A/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EdotCover_002EIde_002ECore_002EFilterManagement_002EModel_002ESolutionFilterSettingsManagerMigrateSettings/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters&gt;&lt;Filter ModuleMask="Hangfire.Core.Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /&gt;&lt;Filter ModuleMask="Hangfire.SqlServer.Tests" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="True" /&gt;&lt;Filter ModuleMask="Hangfire.PostgreSql" ModuleVersionMask="*" ClassMask="*" FunctionMask="*" IsEnabled="False" /&gt;&lt;/ExcludeFilters&gt;&lt;/data&gt;</s:String>
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ public static int Execute(this IConnectionProvider connectionProvider,
int? commandTimeout = null,
CommandType? commandType = null)
{
using (var connectionHolder = connectionProvider.AcquireConnection())
{
return connectionHolder.Connection.Execute(
sql,
param,
transaction,
commandTimeout,
commandType);
}
using var connectionHolder = connectionProvider.AcquireConnection();
return connectionHolder.Connection.Execute(
sql,
param,
transaction,
commandTimeout,
commandType);
}

[CanBeNull]
Expand All @@ -33,15 +31,13 @@ public static T Fetch<T>(this IConnectionProvider connectionProvider,
int? commandTimeout = null,
CommandType? commandType = null)
{
using (var connectionHolder = connectionProvider.AcquireConnection())
{
return connectionHolder.Fetch<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}
using var connectionHolder = connectionProvider.AcquireConnection();
return connectionHolder.Fetch<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}

public static T FetchScalar<T>(this IConnectionProvider connectionProvider,
Expand All @@ -52,15 +48,13 @@ public static T FetchScalar<T>(this IConnectionProvider connectionProvider,
CommandType? commandType = null)
where T : struct
{
using (var connectionHolder = connectionProvider.AcquireConnection())
{
return connectionHolder.FetchScalar<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}
using var connectionHolder = connectionProvider.AcquireConnection();
return connectionHolder.FetchScalar<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}

[NotNull]
Expand All @@ -71,15 +65,13 @@ public static List<T> FetchList<T>(this IConnectionProvider connectionProvider,
int? commandTimeout = null,
CommandType? commandType = null)
{
using (var connectionHolder = connectionProvider.AcquireConnection())
{
return connectionHolder.FetchList<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}
using var connectionHolder = connectionProvider.AcquireConnection();
return connectionHolder.FetchList<T>(
sql,
param,
transaction,
commandTimeout,
commandType);
}
}
}
132 changes: 0 additions & 132 deletions src/Hangfire.PostgreSql/Connectivity/DefaultConnectionProvider.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

using Dapper;

namespace Hangfire.PostgreSql.Connectivity
{
internal sealed class NpgsqlConnectionProvider : IConnectionProvider
Expand Down
32 changes: 13 additions & 19 deletions src/Hangfire.PostgreSql/DatabaseInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Initialize()
using (var connectionHolder = _connectionProvider.AcquireConnection())
{
var connection = connectionHolder.Connection;
var lockTaken = LockDatabase(connection);
var lockTaken = LockDatabase(connection, _schemaName);
if (!lockTaken) return;

TryCreateSchema(connection);
Expand All @@ -48,6 +48,7 @@ public void Initialize()
{
try
{
connection.Execute($@"set search_path={_schemaName}");
connection.Execute(migration.Script, transaction: transaction);
}
catch (Exception e)
Expand All @@ -56,6 +57,7 @@ public void Initialize()
Log.ErrorException(errorMessage, e);
throw new ApplicationException(errorMessage, e);
}

lastMigration = migration;
Log.Info($"Installing Hangfire SQL migration #{migration.Version}");
}
Expand All @@ -68,22 +70,23 @@ public void Initialize()
transaction.Commit();
}

UnlockDatabase(connection);
UnlockDatabase(connection, _schemaName);
}

Log.Info("Hangfire SQL objects installed.");
}

private static bool LockDatabase(NpgsqlConnection connection)
=> connection.Query<bool>(@"select pg_try_advisory_lock(12345)").Single();
private bool LockDatabase(NpgsqlConnection connection, string lockId)
=> connection.Query<bool>(@$"select pg_try_advisory_lock(hashtext('{lockId}'))").Single();

private static void UnlockDatabase(NpgsqlConnection connection)
=> connection.Execute(@"select pg_advisory_unlock(12345)");
private void UnlockDatabase(NpgsqlConnection connection, string lockId)
=> connection.Execute(@$"select pg_advisory_unlock(hashtext('{lockId}'))");

private static int GetInstalledVersion(NpgsqlConnection connection)
private int GetInstalledVersion(NpgsqlConnection connection)
{
try
{
connection.Execute($@"set search_path={_schemaName}");
return connection.Query<int>(@"select version from schema").SingleOrDefault();
}
catch
Expand All @@ -94,16 +97,7 @@ private static int GetInstalledVersion(NpgsqlConnection connection)

private void TryCreateSchema(NpgsqlConnection connection)
{
try
{
connection.Execute($@"CREATE SCHEMA {_schemaName}");
}
catch
{
// Already created
}

connection.Execute($@"set search_path={_schemaName}");
connection.Execute($@"create schema if not exists {_schemaName}");
}

private static IEnumerable<MigrationInfo> GetMigrations()
Expand Down Expand Up @@ -134,7 +128,7 @@ private static Option<string> ReadStringResource(string resourceName)
{
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null) return default(Option<string>);
if (stream == null) return default;

using (var reader = new StreamReader(stream))
{
Expand All @@ -145,7 +139,7 @@ private static Option<string> ReadStringResource(string resourceName)
}
catch
{
return default(Option<string>);
return default;
}
}
}
Expand Down
Loading

0 comments on commit aec291d

Please sign in to comment.