Skip to content

Commit

Permalink
feat(mongo): Update mongo driver to v3 (#413)
Browse files Browse the repository at this point in the history
* Ephemeral mongo won't work with the v3 driver and mongo2go fails.
* Replaced with Testcontainers.MongoDb
* Removed unused usings
* Dropped netstandard 2.0 as mongo 3.0 removed target; it does support net472 but would require library to be built on windows agent
* Update actions versions
  • Loading branch information
ChrisMcKee authored Oct 28, 2024
1 parent a5d246a commit 5823ca5
Show file tree
Hide file tree
Showing 55 changed files with 128 additions and 180 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,29 @@ on:
pull_request:
branches: [ master ]

env:
DOTNET_CLI_TELEMETRY_OPTOUT: true
DOTNET_NOLOGO: true
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: true
DOTNET_USE_POLLING_FILE_WATCHER: true
NUGET_XMLDOC_MODE: skip

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Cache NuGet Packages
uses: actions/cache@v4
with:
key: nuget-cache
path: ~/.nuget/packages
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
dotnet-version: 8

- name: Restore dependencies
run: dotnet restore ./src
Expand Down
10 changes: 7 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v3
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Cache NuGet Packages
uses: actions/cache@v4
with:
key: nuget-cache
path: ~/.nuget/packages

- name: Restore dependencies
run: dotnet restore ./src
Expand All @@ -33,9 +38,8 @@ jobs:
- name: Test
run: dotnet test ./src -c Release --no-build --verbosity normal


- name: Create a Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
name: Release ${{ github.event.inputs.version }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.14" />
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
<PackageReference Include="jquery" Version="3.7.1" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="MongoDB.Driver" Version="2.29.0" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
</ItemGroup>
</Project>
46 changes: 14 additions & 32 deletions src/Hangfire.Mongo.Sample.ASPNetCore/MongoRunner.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,27 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Testcontainers.MongoDb;

namespace Hangfire.Mongo.Sample.ASPNetCore
{
public class MongoRunner : IDisposable
public class MongoTestRunner : IAsyncDisposable
{
private Mongo2Go.MongoDbRunner _runner;
public readonly MongoDbContainer MongoDbContainer =
new MongoDbBuilder()
.WithImage("mongo:7.0")
.Build();

public string ConnectionString => _runner?.ConnectionString;
public MongoRunner Start()
{
var homePath = Environment.OSVersion.Platform is PlatformID.Unix or PlatformID.MacOSX
? Environment.GetEnvironmentVariable("HOME")
: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");

if (string.IsNullOrEmpty(homePath))
{
throw new InvalidOperationException("Could not locate home path");
}
var dataDir = Path.Combine(homePath, "mongodb", "data");
for (int i = 0; i < 3; i++)
{
try
{
_runner = Mongo2Go.MongoDbRunner.StartForDebugging(
singleNodeReplSet: true,
dataDirectory: dataDir);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

public string MongoConnectionString { get; private set; }

return this;
public async Task Start()
{
await MongoDbContainer.StartAsync();
MongoConnectionString = MongoDbContainer.GetConnectionString();
}

public void Dispose()
public async ValueTask DisposeAsync()
{
_runner?.Dispose();
if (MongoDbContainer != null) await MongoDbContainer.DisposeAsync();
}
}
}
1 change: 0 additions & 1 deletion src/Hangfire.Mongo.Sample.ASPNetCore/MyRecurringjob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Diagnostics;
using System.Threading;

namespace Hangfire.Mongo.Sample.ASPNetCore;
Expand Down
1 change: 0 additions & 1 deletion src/Hangfire.Mongo.Sample.ASPNetCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;

namespace Hangfire.Mongo.Sample.ASPNetCore
{
Expand Down
9 changes: 4 additions & 5 deletions src/Hangfire.Mongo.Sample.ASPNetCore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ public Startup(IHostEnvironment env)
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddHangfire(config =>
services.AddHangfire(async config =>
{
var runner = new MongoRunner().Start();
services.AddSingleton(runner);
await using var mongoTestRunner = new MongoTestRunner();
await mongoTestRunner.Start();
// Read DefaultConnection string from appsettings.json
var mongoUrlBuilder = new MongoUrlBuilder(runner.ConnectionString)
var mongoUrlBuilder = new MongoUrlBuilder(mongoTestRunner.MongoConnectionString)
{
DatabaseName = "hangfire"
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.14" />
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
<PackageReference Include="jquery" Version="3.7.1" />
<PackageReference Include="MongoDB.Driver" Version="2.29.0" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Hangfire.Core" Version="1.8.14" />
<PackageReference Include="Mongo2Go" Version="3.1.3" />
<PackageReference Include="MongoDB.Driver" Version="2.29.0" />
<PackageReference Include="MongoDB.Driver" Version="3.0.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
</ItemGroup>
</Project>
47 changes: 14 additions & 33 deletions src/Hangfire.Mongo.Sample.NETCore/MongoRunner.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,27 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Testcontainers.MongoDb;

namespace Hangfire.Mongo.Sample.NETCore
{
public class MongoRunner : IDisposable
public class MongoTestRunner : IAsyncDisposable
{
private Mongo2Go.MongoDbRunner _runner;
public readonly MongoDbContainer MongoDbContainer =
new MongoDbBuilder()
.WithImage("mongo:7.0")
.Build();

public string ConnectionString => _runner?.ConnectionString;
public MongoRunner Start()
{
var homePath = Environment.OSVersion.Platform is PlatformID.Unix or PlatformID.MacOSX
? Environment.GetEnvironmentVariable("HOME")
: Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");

if (string.IsNullOrEmpty(homePath))
{
throw new InvalidOperationException("Could not locate home path");
}
var dataDir = Path.Combine(homePath, "mongodb", "data");
// try 3 times
for (int i = 0; i < 3; i++)
{
try
{
_runner = Mongo2Go.MongoDbRunner.StartForDebugging(
singleNodeReplSet: true,
dataDirectory: dataDir);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

public string MongoConnectionString { get; private set; }

return this;
public async Task Start()
{
await MongoDbContainer.StartAsync();
MongoConnectionString = MongoDbContainer.GetConnectionString();
}

public void Dispose()
public async ValueTask DisposeAsync()
{
_runner?.Dispose();
if (MongoDbContainer != null) await MongoDbContainer.DisposeAsync();
}
}
}
11 changes: 6 additions & 5 deletions src/Hangfire.Mongo.Sample.NETCore/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Threading.Tasks;
using Hangfire.Logging.LogProviders;
using Hangfire.Mongo.Migration.Strategies;
using Hangfire.Mongo.Migration.Strategies.Backup;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;

namespace Hangfire.Mongo.Sample.NETCore
Expand All @@ -11,7 +11,7 @@ public class Program
{
private const int JobCount = 100;

public static void Main()
public static async Task Main()
{
var migrationOptions = new MongoStorageOptions
{
Expand All @@ -24,10 +24,11 @@ public static void Main()
};

GlobalConfiguration.Configuration.UseLogProvider(new ColouredConsoleLogProvider());
using var runner = new MongoRunner().Start();

await using var mongoTestRunner = new MongoTestRunner();
await mongoTestRunner.Start();

JobStorage.Current = new MongoStorage(
MongoClientSettings.FromConnectionString(runner.ConnectionString),
MongoClientSettings.FromConnectionString(mongoTestRunner.MongoConnectionString),
databaseName: "Mongo-Hangfire-Sample-NETCore",
migrationOptions);

Expand Down
3 changes: 1 addition & 2 deletions src/Hangfire.Mongo.Tests/ExpirationManagerFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Hangfire.Mongo.Dto;
using Hangfire.Mongo.Tests.Utils;
using MongoDB.Bson;
using MongoDB.Driver;
using Xunit;

namespace Hangfire.Mongo.Tests
Expand All @@ -17,7 +16,7 @@ public class ExpirationManagerFacts : IDisposable
private readonly HangfireDbContext _dbContext;
private readonly CancellationToken _token;

public ExpirationManagerFacts(MongoDbFixture fixture)
public ExpirationManagerFacts(MongoIntegrationTestFixture fixture)
{
fixture.CleanDatabase();
_dbContext = fixture.CreateDbContext();
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Mongo.Tests/Hangfire.Mongo.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
<ProjectReference Include="../Hangfire.Mongo/Hangfire.Mongo.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="EphemeralMongo6" Version="1.1.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="Testcontainers.MongoDb" Version="3.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
4 changes: 2 additions & 2 deletions src/Hangfire.Mongo.Tests/Migration/Mongo/MigrationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ namespace Hangfire.Mongo.Tests.Migration.Mongo
[Collection("Database")]
public class MigrationFacts
{
private readonly MongoDbFixture _fixture;
private readonly MongoIntegrationTestFixture _fixture;

public MigrationFacts(MongoDbFixture fixture)
public MigrationFacts(MongoIntegrationTestFixture fixture)
{
fixture.CleanDatabase();
_fixture = fixture;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace Hangfire.Mongo.Tests.Migration.Mongo
[Collection("Database")]
public class MongoDatabaseFiller
{
private readonly MongoDbFixture _fixture;
private readonly MongoIntegrationTestFixture _fixture;

public MongoDatabaseFiller(MongoDbFixture fixture)
public MongoDatabaseFiller(MongoIntegrationTestFixture fixture)
{
_fixture = fixture;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Version15MigrationStepFacts
private readonly HangfireDbContext _dbContext;
private readonly IMongoDatabase _database;

public Version15MigrationStepFacts(MongoDbFixture fixture)
public Version15MigrationStepFacts(MongoIntegrationTestFixture fixture)
{
_dbContext = fixture.CreateDbContext();
_database = _dbContext.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Version16MigrationStepFacts
private readonly HangfireDbContext _dbContext;
private readonly IMongoDatabase _database;

public Version16MigrationStepFacts(MongoDbFixture fixture)
public Version16MigrationStepFacts(MongoIntegrationTestFixture fixture)
{
_dbContext = fixture.CreateDbContext();
_database = _dbContext.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Version18MigrationStepFacts
{
private readonly IMongoDatabase _database;

public Version18MigrationStepFacts(MongoDbFixture fixture)
public Version18MigrationStepFacts(MongoIntegrationTestFixture fixture)
{
var dbContext = fixture.CreateDbContext();
_database = dbContext.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Version19MigrationStepFacts
private readonly IMongoDatabase _database;
private readonly Random _random;
private readonly AddTypeToSetDto _addTypeToSetDto;
public Version19MigrationStepFacts(MongoDbFixture fixture)
public Version19MigrationStepFacts(MongoIntegrationTestFixture fixture)
{
var dbContext = fixture.CreateDbContext();
_database = dbContext.Database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Version20MigrationStepFacts
{
private readonly IMongoDatabase _database;
private readonly IMongoMigrationStep _migration;
public Version20MigrationStepFacts(MongoDbFixture fixture)
public Version20MigrationStepFacts(MongoIntegrationTestFixture fixture)
{
var dbContext = fixture.CreateDbContext();
_database = dbContext.Database;
Expand Down
2 changes: 1 addition & 1 deletion src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class MongoConnectionFacts
private readonly MongoConnection _connection;
private readonly IJobQueueSemaphore _jobQueueSemaphoreMock;

public MongoConnectionFacts(MongoDbFixture fixture)
public MongoConnectionFacts(MongoIntegrationTestFixture fixture)
{
_jobQueueSemaphoreMock = Substitute.For<IJobQueueSemaphore>();
var storageOptions = new MongoStorageOptions
Expand Down
Loading

0 comments on commit 5823ca5

Please sign in to comment.