Skip to content

Commit 504d56d

Browse files
authored
Merge pull request #304 from dmitry-vychikov/feature/first-class-queue
First class queue support
2 parents 3bce96a + 34e2983 commit 504d56d

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

src/Hangfire.PostgreSql/Hangfire.PostgreSql.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<PrivateAssets>all</PrivateAssets>
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
</PackageReference>
38-
<PackageReference Include="Hangfire.Core" Version="1.7.28" />
38+
<PackageReference Include="Hangfire.Core" Version="1.8.0" />
3939
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
4040
<PackageReference Include="Npgsql" Version="6.0.0" />
4141
</ItemGroup>

src/Hangfire.PostgreSql/PostgreSqlStorage.cs

+16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public class PostgreSqlStorage : JobStorage
4141
private readonly Action<NpgsqlConnection> _connectionSetup;
4242
private readonly NpgsqlConnectionStringBuilder _connectionStringBuilder;
4343
private readonly NpgsqlConnection _existingConnection;
44+
45+
private readonly Dictionary<string, bool> _features =
46+
new(StringComparer.OrdinalIgnoreCase)
47+
{
48+
{ JobStorageFeatures.JobQueueProperty, true },
49+
};
50+
4451

4552
public PostgreSqlStorage(string connectionString)
4653
: this(connectionString, new PostgreSqlStorageOptions()) { }
@@ -420,5 +427,14 @@ private NpgsqlConnectionStringBuilder SetupConnectionStringBuilderParameters(Npg
420427

421428
return builder;
422429
}
430+
431+
public override bool HasFeature(string featureId)
432+
{
433+
if (featureId == null) throw new ArgumentNullException(nameof(featureId));
434+
435+
return _features.TryGetValue(featureId, out bool isSupported)
436+
? isSupported
437+
: base.HasFeature(featureId);
438+
}
423439
}
424440
}

tests/Hangfire.PostgreSql.Tests/Entities/TestJob.cs

+8
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,12 @@
33
namespace Hangfire.PostgreSql.Tests
44
{
55
public record TestJob(long Id, string InvocationData, string Arguments, DateTime? ExpireAt, string StateName, long? StateId, DateTime CreatedAt);
6+
7+
public class TestJobs
8+
{
9+
public void Run(string logMessage)
10+
{
11+
Console.WriteLine("Running test job: {0}", logMessage);
12+
}
13+
}
614
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.Threading;
2+
using Hangfire.PostgreSql.Tests.Utils;
3+
using Hangfire.Storage;
4+
using Hangfire.Storage.Monitoring;
5+
using Xunit;
6+
7+
namespace Hangfire.PostgreSql.Tests;
8+
9+
public class FirstClassQueueFeatureSupportTests
10+
{
11+
public FirstClassQueueFeatureSupportTests()
12+
{
13+
JobStorage.Current = new PostgreSqlStorage(ConnectionUtils.GetConnectionString());
14+
}
15+
16+
[Fact]
17+
public void HasFlag_ShouldReturnTrue_ForJobQueueProperty()
18+
{
19+
bool supportJobQueueProperty = JobStorage.Current.HasFeature(JobStorageFeatures.JobQueueProperty);
20+
Assert.True(supportJobQueueProperty);
21+
}
22+
23+
[Fact]
24+
[CleanDatabase]
25+
public void EnqueueJobWithSpecificQueue_ShouldEnqueueCorrectlyAndJobMustBeProcessedInThatQueue()
26+
{
27+
BackgroundJob.Enqueue<TestJobs>("critical", job => job.Run("critical"));
28+
BackgroundJob.Enqueue<TestJobs>("offline", job => job.Run("offline"));
29+
30+
BackgroundJobServer server = new(new BackgroundJobServerOptions() {
31+
Queues = new[] { "critical" },
32+
});
33+
34+
Thread.Sleep(200);
35+
36+
IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
37+
38+
JobList<EnqueuedJobDto> jobsInCriticalQueue = monitoringApi.EnqueuedJobs("critical", 0, 10);
39+
JobList<EnqueuedJobDto> jobsInOfflineQueue = monitoringApi.EnqueuedJobs("offline", 0, 10);
40+
41+
Assert.Empty(jobsInCriticalQueue); //Job from 'critical' queue must be processed by the server
42+
Assert.NotEmpty(jobsInOfflineQueue); //Job from 'offline' queue must be left untouched because no server is processing it
43+
}
44+
}

0 commit comments

Comments
 (0)