Skip to content

Commit

Permalink
update to latest hangfire (#332)
Browse files Browse the repository at this point in the history
* update to latest hangfire

adds recommended indexes #330
removes jobQueueDto, now using JobDto instead
optimizing Monitoring API

* fix possible null

* use service for tests instead of mongo2go

* skip test which needs replicaset

* set connection string

* trying to remove test
  • Loading branch information
gottscj authored Dec 10, 2022
1 parent 9f5da1d commit d7ff0e3
Show file tree
Hide file tree
Showing 33 changed files with 596 additions and 354 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ jobs:
build:

runs-on: ubuntu-latest
services:
mongodb:
image: mongo
ports:
- 27017:27017

steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 6.0.x

- name: Restore dependencies
run: dotnet restore ./src
- name: Build
Expand Down
2 changes: 0 additions & 2 deletions .vscode/settings.json

This file was deleted.

10 changes: 5 additions & 5 deletions .vscode/launch.json → src/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "Build Hangfire.Mongo.Sample.ASPNetCore",
"program": "${workspaceRoot}/src/Hangfire.Mongo.Sample.ASPNetCore/bin/Debug/net5.0/Hangfire.Mongo.Sample.ASPNetCore.dll",
"program": "${workspaceRoot}/Hangfire.Mongo.Sample.ASPNetCore/bin/Debug/net6.0/Hangfire.Mongo.Sample.ASPNetCore.dll",
"args": [],
"cwd": "${workspaceRoot}/src/Hangfire.Mongo.Sample.ASPNetCore",
"cwd": "${workspaceRoot}/Hangfire.Mongo.Sample.ASPNetCore",
"stopAtEntry": false,
"launchBrowser": {
"enabled": true,
Expand All @@ -32,7 +32,7 @@
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceRoot}/src/Hangfire.Mongo.Sample.ASPNetCore/Views"
"/Views": "${workspaceRoot}/Hangfire.Mongo.Sample.ASPNetCore/Views"
}
},
{
Expand All @@ -41,9 +41,9 @@
"request": "launch",
"preLaunchTask": "Build Hangfire.Mongo.Sample.NETCore",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceRoot}/src/Hangfire.Mongo.Sample.NETCore/bin/Debug/net5.0/Hangfire.Mongo.Sample.NETCore.dll",
"program": "${workspaceRoot}/Hangfire.Mongo.Sample.NETCore/bin/Debug/net6.0/Hangfire.Mongo.Sample.NETCore.dll",
"args": [],
"cwd": "${workspaceRoot}/src/Hangfire.Mongo.Sample.NETCore",
"cwd": "${workspaceRoot}/Hangfire.Mongo.Sample.NETCore",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "externalTerminal",
"stopAtEntry": false,
Expand Down
4 changes: 4 additions & 0 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"testExplorer.useNativeTesting": true,
"dotnetCoreExplorer.searchpatterns":"**/bin/**/*.{dll,exe}"
}
18 changes: 9 additions & 9 deletions .vscode/tasks.json → src/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
"command": "dotnet",
"args": [
"build",
"${workspaceRoot}/src/Hangfire.Mongo/Hangfire.Mongo.csproj"
"${workspaceRoot}/Hangfire.Mongo/Hangfire.Mongo.csproj"
],
"osx": {
"args": [
"-f",
"netstandard1.5"
"netstandard2.0"
]
},
"linux": {
"args": [
"-f",
"netstandard1.5"
"netstandard2.0"
]
},
"group": {
Expand All @@ -51,7 +51,7 @@
"command": "dotnet",
"args": [
"build",
"${workspaceRoot}/src/Hangfire.Mongo.Sample.NETCore/Hangfire.Mongo.Sample.NETCore.csproj"
"${workspaceRoot}/Hangfire.Mongo.Sample.NETCore/Hangfire.Mongo.Sample.NETCore.csproj"
],
"group": {
"kind": "build",
Expand All @@ -66,7 +66,7 @@
"command": "dotnet",
"args": [
"build",
"${workspaceRoot}/src/Hangfire.Mongo.Sample.ASPNetCore/Hangfire.Mongo.Sample.ASPNetCore.csproj"
"${workspaceRoot}/Hangfire.Mongo.Sample.ASPNetCore/Hangfire.Mongo.Sample.ASPNetCore.csproj"
],
"group": {
"kind": "build",
Expand All @@ -81,7 +81,7 @@
"command": "dotnet",
"args": [
"build",
"${workspaceRoot}/src/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj"
"${workspaceRoot}/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj"
],
"osx": {
"args": [
Expand Down Expand Up @@ -110,18 +110,18 @@
"test",
"--filter",
"Category!=DataGeneration",
"${workspaceRoot}/src/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj"
"${workspaceRoot}/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj"
],
"osx": {
"args": [
"-f",
"netcoreapp2.0"
"net6.0"
]
},
"linux": {
"args": [
"-f",
"netcoreapp2.0"
"net6.0"
]
},
"group": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="bootstrap" Version="5.2.2" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.31" />
<PackageReference Include="Hangfire.Core" Version="1.7.31" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.32" />
<PackageReference Include="Hangfire.Core" Version="1.7.32" />
<PackageReference Include="jquery" Version="3.6.1" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions src/Hangfire.Mongo.Sample.ASPNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void ConfigureServices(IServiceCollection services)
BackupStrategy = new CollectionMongoBackupStrategy()
},
CheckQueuedJobsStrategy = CheckQueuedJobsStrategy.Watch,
InvisibilityTimeout = TimeSpan.FromMinutes(5)
};

//config.UseLogProvider(new FileLogProvider());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="bootstrap" Version="5.2.2" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.31" />
<PackageReference Include="Hangfire.Core" Version="1.7.31" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.32" />
<PackageReference Include="Hangfire.Core" Version="1.7.32" />
<PackageReference Include="jquery" Version="3.6.1" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<ProjectReference Include="..\Hangfire.Mongo\Hangfire.Mongo.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.7.31" />
<PackageReference Include="Hangfire.Core" Version="1.7.32" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="MongoDB.Driver" Version="2.18.0" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions src/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<NoWarn>$(NoWarn);CS0618</NoWarn>
Expand Down Expand Up @@ -26,9 +26,9 @@
<ProjectReference Include="../Hangfire.Mongo/Hangfire.Mongo.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.3" />
<PackageReference Include="EphemeralMongo6" Version="0.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Binary file not shown.
6 changes: 4 additions & 2 deletions src/Hangfire.Mongo.Tests/Migration/Mongo/MigrationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using Newtonsoft.Json;
using Xunit;

namespace Hangfire.Mongo.Tests.Migration.Mongo
Expand All @@ -37,7 +38,8 @@ public class MigrationFacts
[InlineData("Hangfire-Mongo-Schema-015.zip", true)]
[InlineData("Hangfire-Mongo-Schema-016.zip", true)]
[InlineData("Hangfire-Mongo-Schema-017.zip", true)]
// [InlineData("Hangfire-Mongo-Schema-018.zip", true)]
//[InlineData("Hangfire-Mongo-Schema-018.zip", true)]
//[InlineData("Hangfire-Mongo-Schema-019.zip", true)]
public void Migrate_Full_Success(string seedFile, bool assertCollectionHasItems)
{

Expand Down Expand Up @@ -183,7 +185,7 @@ private static void SeedCollectionFromJson(HangfireDbContext connection, string
return;
}
var documents = new List<BsonDocument>();
using (var jsonReader = new JsonReader(json))
using (var jsonReader = new MongoDB.Bson.IO.JsonReader(json))
{
while (!jsonReader.IsAtEndOfFile())
{
Expand Down
147 changes: 147 additions & 0 deletions src/Hangfire.Mongo.Tests/Migration/Version20MigrationStepFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Hangfire.Mongo.Migration;
using Hangfire.Mongo.Migration.Steps;
using Hangfire.Mongo.Migration.Steps.Version20;
using Hangfire.Mongo.Tests.Utils;
using MongoDB.Bson;
using MongoDB.Driver;
using Moq;
using Xunit;

namespace Hangfire.Mongo.Tests.Migration
{
public class Version20MigrationStepFacts
{
private readonly Mock<IMongoMigrationContext> _mongoMigrationBagMock;
private readonly IMongoDatabase _database;
private readonly Random _random;
private readonly IMongoMigrationStep _migration;
public Version20MigrationStepFacts()
{
var dbContext = ConnectionUtils.CreateDbContext();
_database = dbContext.Database;
_mongoMigrationBagMock = new Mock<IMongoMigrationContext>(MockBehavior.Strict);
_random = new Random();
_migration = new RemoveJobQueueDto();
}

[Fact]
public void ExecuteStep01_MergeJobQueueIntoJobDto_Success()
{
// ARRANGE
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph");
collection.DeleteMany("{}");
collection.InsertMany(CreateJobQueueDtos());

// ACT
var result = _migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object);

// ASSERT
Assert.True(result, "Expected migration to be successful, reported 'false'");
var migrated = collection.Find(new BsonDocument("_t", "JobDto")).ToList();
foreach (var doc in migrated)
{
Assert.True(doc.Contains("Queue"));
Assert.True(doc.Contains("FetchedAt"));
}
var migratedFrom = collection.Find(new BsonDocument("_t", "JobQueueDto")).ToList();

Assert.Empty(migratedFrom);
}

[Fact]
public void ExecuteStep01_AlreadyRunJobsGetsFetchedAtSet_Success()
{
// ARRANGE
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph");
collection.DeleteMany("{}");
var jobs = CreateJobQueueDtos(jobQueueCount: 5, jobDtoCount: 3);
collection.InsertMany(jobs);

// ACT
var result = _migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object);

// ASSERT
Assert.True(result, "Expected migration to be successful, reported 'false'");
var migrated = collection.Find(new BsonDocument("_t", "JobDto")).ToList();
foreach (var doc in migrated)
{
Assert.True(doc.Contains("Queue"));
Assert.True(doc.Contains("FetchedAt"));
}
var migratedFrom = collection.Find(new BsonDocument("_t", "JobQueueDto")).ToList();
Assert.Empty(migratedFrom);

var fetchedJobs = migrated.Where(b => b["FetchedAt"] != BsonNull.Value).ToList();
foreach (var item in fetchedJobs)
{
var job = jobs.Find(j => j["_id"] == item["_id"]);
Assert.Equal(job["StateHistory"][0]["CreatedAt"], item["FetchedAt"]);
}
}

[Fact]
public void ExecuteStep02_AddIndexes_Success()
{
// ARRANGE
var collection = _database.GetCollection<BsonDocument>("hangfire.jobGraph");
collection.DeleteMany("{}");
collection.InsertMany(CreateJobQueueDtos(0, 5));
var migration = new CompoundIndexes();
// ACT
var result = migration.Execute(_database, new MongoStorageOptions(), _mongoMigrationBagMock.Object);

// ASSERT
var index = collection.Indexes.List().ToList().FirstOrDefault(b => b["name"].AsString == "IX_SetType_T_Score");
var index2 = collection.Indexes.List().ToList().FirstOrDefault(b => b["name"].AsString == "T_ExpireAt");
Assert.NotNull(index);
Assert.NotNull(index2);
}

public List<BsonDocument> CreateJobQueueDtos(int jobQueueCount = 5, int jobDtoCount = 5)
{
var list = new List<BsonDocument>();
var count = new[] { jobQueueCount, jobDtoCount }.Max();

for (int i = 0; i < count; i++)
{
BsonDocument jobQueueDto = null;
if (jobQueueCount > i)
{
jobQueueDto = new BsonDocument
{
["FetchedAt"] = BsonNull.Value,
["Queue"] = "default",
["JobId"] = ObjectId.GenerateNewId(),
["_t"] = "JobQueueDto",
["_id"] = ObjectId.GenerateNewId()
};
list.Add(jobQueueDto);
}

if (jobDtoCount > i)
{
var jobDto = new BsonDocument
{
["JobId"] = jobQueueDto != null ? jobQueueDto["_id"] : ObjectId.GenerateNewId(),
["_t"] = "JobDto",
["_id"] = ObjectId.GenerateNewId(),
["StateName"] = "Enqueued",
["StateHistory"] = new BsonArray
{
new BsonDocument
{
["CreatedAt"] = DateTime.UtcNow.AddDays(-1)
}
}
};
list.Add(jobDto);
}
}
return list;
}

}
}
Loading

0 comments on commit d7ff0e3

Please sign in to comment.