Hangfire.Mongo 1.9.2

.NET Standard 2.0
dotnet add package Hangfire.Mongo --version 1.9.2
NuGet\Install-Package Hangfire.Mongo -Version 1.9.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Hangfire.Mongo" Version="1.9.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Hangfire.Mongo --version 1.9.2
#r "nuget: Hangfire.Mongo, 1.9.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Hangfire.Mongo as a Cake Addin
#addin nuget:?package=Hangfire.Mongo&version=1.9.2

// Install Hangfire.Mongo as a Cake Tool
#tool nuget:?package=Hangfire.Mongo&version=1.9.2

Hangfire.Mongo

Build Nuget downloads GitHub license

MongoDB support for Hangfire library. By using this library you can store all jobs information in MongoDB.

Installation

To install Hangfire MongoDB Storage, run the following command in the Nuget Package Manager Console:

PM> Install-Package Hangfire.Mongo

Usage ASP.NET Core

public void ConfigureServices(IServiceCollection services)
{
    var mongoUrlBuilder = new MongoUrlBuilder("mongodb://localhost/jobs");
    var mongoClient = new MongoClient(mongoUrlBuilder.ToMongoUrl());

    // Add Hangfire services. Hangfire.AspNetCore nuget required
    services.AddHangfire(configuration => configuration
        .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
        .UseSimpleAssemblyNameTypeSerializer()
        .UseRecommendedSerializerSettings()
        .UseMongoStorage(mongoClient, mongoUrlBuilder.DatabaseName, new MongoStorageOptions
        {
            MigrationOptions = new MongoMigrationOptions
            {
                MigrationStrategy = new MigrateMongoMigrationStrategy(),
                BackupStrategy = new CollectionMongoBackupStrategy()
            },
            Prefix = "hangfire.mongo",
            CheckConnection = true
        })
    );
    // Add the processing server as IHostedService
    services.AddHangfireServer(serverOptions =>
    {
        serverOptions.ServerName = "Hangfire.Mongo server 1";
    });

    // Add framework services.
}

Usage ASP.NET

var options = new MongoStorageOptions
{
    MigrationOptions = new MongoMigrationOptions
    {
        MigrationStrategy = new DropMongoMigrationStrategy(),
        BackupStrategy = new NoneMongoBackupStrategy()
    }
};
GlobalConfiguration.Configuration.UseMongoStorage("mongodb://localhost/jobs", options);
app.UseHangfireServer();
app.UseHangfireDashboard();

Usage Console

var options = new MongoStorageOptions
{
    MigrationOptions = new MongoMigrationOptions
    {
        MigrationStrategy = new DropMongoMigrationStrategy(),
        BackupStrategy = new NoneMongoBackupStrategy()
    }
};
var mongoStorage = new MongoStorage(
                MongoClientSettings.FromConnectionString("mongodb://localhost"),
                "jobs", // database name
                options);
            
using(new BackgroundJobServer(mongoStorage))
{
  ...
}

Custom collections prefix

To use custom prefix for collections names specify it on Hangfire setup:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseMongoStorage("mongodb://localhost/ApplicationDatabase",
        new MongoStorageOptions { Prefix = "custom" } );

    app.UseHangfireServer();
    app.UseHangfireDashboard();
}

Configuration Overrides

  • CheckQueuedJobsStrategy (default: Watch)
    • Watch uses change streams to watch for enqueued jobs. Will still poll using 'QueuePollInterval'.
    • Poll will poll periodically using 'QueuePollInterval', recommended for large installments.
    • TailNotificationsCollection uses a capped, tailable collection to notify nodes of enqueued jobs. Will still poll using 'QueuePollInterval'. Works with single node MongoDB instances.

Naming Convention

Hangfire.Mongo will ignore any naming conventions configured by your application. E.g. if your application use camel casing like this:

  var camelCaseConventionPack = new ConventionPack { new CamelCaseElementNameConvention() };
  ConventionRegistry.Register("CamelCase", camelCaseConventionPack, type => true);

it will be ignored by Hangfire.Mongo and Pascal Case will be used instead. Of cause only for Hangfire specific collections.

Migration

We sometimes introduce breaking changes in the schema. For this reason we have introduced migration. Three migration strategies exists.

  • Throw

    This is the default migration strategy. It will throw an InvalidOperationException never letting you get up and running if there is a schema version mismatch. So it forces you to decide what migration strategy is best for you and at the same time keeps your data safe.

  • Drop

    This will simply just drop your existing Hangfire.Mongo database and update the schema version. No fuzz and ready to start from scratch. It is the perfect strategy if you e.g. enque all your jobs at startup.

  • Migrate

    This will migrate your database from one schema version to the next until the required schema version is reached. Chances are that not all data can be migrated, why some loss of data might occur. Please use with caution and thougoughly test before deploying to production. We are not responsible for data loss.

    NOTE: Only forward migration is supported. If you need to revert to a previous schema version, you need to manually drop or restore the previous database.

public void Configuration(IAppBuilder app)
{
    var migrationOptions = new MongoMigrationOptions
    {
        MigrationStrategy = new MigrateMongoMigrationStrategy(),
        BackupStrategy = new CollectionMongoBackupStrategy()
    };
    var storageOptions = new MongoStorageOptions
    {
        // ...
        MigrationOptions = migrationOptions
    };
    GlobalConfiguration.Configuration.UseMongoStorage("<connection string with database name>", storageOptions);

    app.UseHangfireServer();
    app.UseHangfireDashboard();
}

NOTE: By default the parameter InvisibilityTimeout of the MongoStorageOptions is configured with the value null, making the job to stay in status 'processing' in case of an error in the application. To solve this issue, set the value to 30 minutes like in the SqlServerStorageOptions.

public void Configuration(IAppBuilder app)
{
    var migrationOptions = new MongoMigrationOptions
    {
        MigrationStrategy = new MigrateMongoMigrationStrategy(),
        BackupStrategy = new CollectionMongoBackupStrategy()
    };
    var storageOptions = new MongoStorageOptions
    {
        // ...
        MigrationOptions = migrationOptions,
        InvisibilityTimeout = TimeSpan.FromMinutes(30)
    };
    GlobalConfiguration.Configuration.UseMongoStorage("<connection string with database name>", storageOptions);

    app.UseHangfireServer();
    app.UseHangfireDashboard();
}

Migration Backup

By default no backup is made before attempting to migrate. You can backup Hangfire collections by "cloning" each collection to the same database. Alternatively you can just write your own, by inheriting MongoBackupStrategy and use that implementation.

NOTE: This software is made by humans in our sparetime - we do our best but will not be held responsible for any data loss.

Contributors

License

Hangfire.Mongo is released under the MIT License.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (9)

Showing the top 5 NuGet packages that depend on Hangfire.Mongo:

Package Downloads
Emerald.AspNetCore

Emerald framework with AspNetCore support.

DiegoRangel.DotNet.Framework.CQRS.Infra.CrossCutting.Hangfire

A common library for implementing CQRS based CrossCutting Hangfire layer.

Emerald.MicroService

Framework to build microservices.

EaCloud.Hangfire

EaCloud Hangfire 后台任务组件,封装基于 Hangfire 后台任务的服务端实现。

Jcex.Infra.Hangfire

该库提供了一种方便的 Hangfire 定时任务管理方式,使用 MongoDB 来存储任务信息,并内置 Web 界面来监控和操作任务。全面可视化的界面提供了对任务状态和进度的监控。

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Hangfire.Mongo:

Repository Stars
gnsilence/HangfireHttpJob
hangfire的拓展程序,在原作者基础上增加了一些功能,调用api的方式来执行定时任务
Version Downloads Last updated
1.9.2 29,208 2/2/2023
1.9.1 49,409 12/19/2022
1.9.0 15,199 12/10/2022
1.7.3 178,554 8/24/2022
1.7.2 73,942 6/13/2022
1.7.1 16,927 6/8/2022
1.7.0 129,348 3/19/2022
0.7.28 113,234 1/2/2022
0.7.27 86,571 11/3/2021
0.7.25 69,633 9/20/2021
0.7.24 80,778 8/8/2021
0.7.22 167,234 5/2/2021
0.7.20 75,284 3/25/2021
0.7.19 58,682 2/10/2021
0.7.17 122,196 11/20/2020
0.7.12 126,964 8/12/2020
0.7.11 161,770 6/7/2020
0.6.7 167,074 3/19/2020
0.6.6 374,811 12/11/2019
0.6.5 79,842 10/21/2019
0.6.4 14,733 9/26/2019
0.6.3 25,227 8/14/2019
0.6.2 16,639 7/28/2019
0.6.1 35,453 6/16/2019
0.6.0 40,115 4/23/2019
0.5.15 143,490 1/3/2019
0.5.14 1,110 1/1/2019
0.5.13 11,268 12/13/2018
0.5.12 44,717 10/28/2018
0.5.11 83,820 9/5/2018
0.5.10 38,734 5/27/2018
0.5.9 49,881 2/20/2018
0.5.8 1,409 2/20/2018
0.5.7 78,075 11/20/2017
0.5.6 1,022 11/20/2017
0.5.5 11,358 10/14/2017
0.5.4 9,756 9/22/2017
0.5.3 2,261 9/21/2017
0.5.2 29,278 9/1/2017
0.5.1 1,548 8/30/2017
0.5.0 2,981 8/17/2017
0.4.1 13,486 6/22/2017
0.4.0 4,470 6/19/2017
0.3.2 56,089 12/10/2016
0.3.1 1,392 11/30/2016
0.3.0 1,251 11/29/2016
0.2.8 7,616 9/26/2016
0.2.6 215,991 5/29/2016
0.2.5 4,158 3/5/2016
0.2.4 1,657 2/15/2016
0.2.3 2,019 12/6/2015
0.2.2 4,961 7/12/2015
0.2.1 2,222 12/13/2014
0.2.0 1,548 12/7/2014

1.9.2
- Remove debug log written with Console.WriteLine (#336)
- Automatically run MongoDB during the tests (with EphemeralMongo) (#337)
- Make compatible with MongoDB.Driver linq v3 (#338)
- Update to Hangfire.Core 1.7.33
- Update MongoDB.Driver to 2.19.0