Skip to content

Commit d4e20b8

Browse files
VincentVrijburgvytautask
authored andcommitted
Changes to fix the connection issues for both Non-SSL and SSL encrypted databases (#124 and #127) (#129)
* Changes to fix the connection issues (#124 and #127) * Changes to fix connection issues with SSL encrypted databases (#124 and #127)
1 parent 9e12fe4 commit d4e20b8

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

src/Hangfire.PostgreSql/PostgreSqlMonitoringApi.cs

+17-5
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,19 @@ namespace Hangfire.PostgreSql
3636
{
3737
public class PostgreSqlMonitoringApi : IMonitoringApi
3838
{
39-
private readonly NpgsqlConnection _connection;
39+
private readonly string _connectionString;
40+
private readonly Action<NpgsqlConnection> _connectionSetup;
4041
private readonly PostgreSqlStorageOptions _options;
4142
private readonly PersistentJobQueueProviderCollection _queueProviders;
4243

4344
public PostgreSqlMonitoringApi(
44-
NpgsqlConnection connection,
45+
string connectionString,
46+
Action<NpgsqlConnection> connectionSetup,
4547
PostgreSqlStorageOptions options,
4648
PersistentJobQueueProviderCollection queueProviders)
4749
{
48-
_connection = connection ?? throw new ArgumentNullException(nameof(connection));
50+
_connectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString));
51+
_connectionSetup = connectionSetup;
4952
_options = options ?? throw new ArgumentNullException(nameof(options));
5053
_queueProviders = queueProviders ?? throw new ArgumentNullException(nameof(queueProviders));
5154
}
@@ -387,7 +390,12 @@ SELECT COUNT(*)
387390
});
388391
}
389392

390-
protected virtual NpgsqlConnection GetConnection() => _connection;
393+
protected virtual NpgsqlConnection GetConnection()
394+
{
395+
var connection = new NpgsqlConnection(_connectionString);
396+
_connectionSetup?.Invoke(connection);
397+
return connection;
398+
}
391399

392400
private Dictionary<DateTime, long> GetHourlyTimelineStats(
393401
NpgsqlConnection connection,
@@ -466,7 +474,11 @@ private IPersistentJobQueueMonitoringApi GetQueueApi(
466474

467475
private T UseConnection<T>(Func<NpgsqlConnection, T> action)
468476
{
469-
return action(GetConnection());
477+
using (var connection = GetConnection())
478+
{
479+
connection.Open();
480+
return action(connection);
481+
}
470482
}
471483

472484
private JobList<EnqueuedJobDto> EnqueuedJobs(NpgsqlConnection connection, IEnumerable<long> jobIds)

src/Hangfire.PostgreSql/PostgreSqlStorage.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class PostgreSqlStorage : JobStorage
4141
private readonly string _connectionString;
4242

4343
public PostgreSqlStorage(string nameOrConnectionString)
44-
: this(nameOrConnectionString, null, new PostgreSqlStorageOptions())
44+
: this(nameOrConnectionString, new PostgreSqlStorageOptions())
4545
{
4646
}
4747

@@ -139,16 +139,12 @@ public PostgreSqlStorage(NpgsqlConnection existingConnection)
139139

140140
public override IMonitoringApi GetMonitoringApi()
141141
{
142-
var connection = _existingConnection ?? CreateAndOpenConnection();
143-
_connectionSetup?.Invoke(connection);
144-
145-
return new PostgreSqlMonitoringApi(connection, _options, QueueProviders);
142+
return new PostgreSqlMonitoringApi(_connectionString, _connectionSetup, _options, QueueProviders);
146143
}
147144

148145
public override IStorageConnection GetConnection()
149146
{
150147
var connection = _existingConnection ?? CreateAndOpenConnection();
151-
_connectionSetup?.Invoke(connection);
152148

153149
return new PostgreSqlConnection(connection, QueueProviders, _options, _existingConnection == null);
154150
}
@@ -199,6 +195,8 @@ internal NpgsqlConnection CreateAndOpenConnection()
199195
};
200196

201197
var connection = new NpgsqlConnection(connectionStringBuilder.ToString());
198+
199+
_connectionSetup?.Invoke(connection);
202200
connection.Open();
203201

204202
return connection;

tests/Hangfire.PostgreSql.Tests/Hangfire.PostgreSql.Tests.csproj

-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@
1717
<EmbeddedResource Include="*.sql" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
1818
</ItemGroup>
1919

20-
<ItemGroup>
21-
<Compile Remove="Certificates\**" />
22-
<EmbeddedResource Remove="Certificates\**" />
23-
<None Remove="Certificates\**" />
24-
</ItemGroup>
25-
26-
<ItemGroup>
27-
<None Remove="Scripts\Clean.sql" />
28-
</ItemGroup>
29-
3020
<ItemGroup>
3121
<EmbeddedResource Include="Scripts\Clean.sql" />
3222
</ItemGroup>

tests/Hangfire.PostgreSql.Tests/PostgreSqlMonitoringApiFacts.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public PostgreSqlMonitoringApiFacts()
3131
SchemaName = ConnectionUtils.GetSchemaName()
3232
};
3333

34-
_storage = new PostgreSqlStorage(ConnectionUtils.CreateConnection(), _options);
34+
_storage = new PostgreSqlStorage(ConnectionUtils.GetConnectionString(), _options);
3535
}
3636

3737
[Fact, CleanDatabase]

tests/Hangfire.PostgreSql.Tests/Utils/ConnectionUtils.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public static class ConnectionUtils
1414
private const string DefaultDatabaseName = @"hangfire_tests";
1515
private const string DefaultSchemaName = @"hangfire";
1616

17-
private const string DefaultConnectionStringTemplate = @"Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=password;";
17+
private const string DefaultConnectionStringTemplate = @"Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=password;";
1818

19-
public static string GetDatabaseName()
19+
public static string GetDatabaseName()
2020
{
2121
return Environment.GetEnvironmentVariable(DatabaseVariable) ?? DefaultDatabaseName;
2222
}

0 commit comments

Comments
 (0)