Hangfire.Redis.StackExchange 1.10.1-beta2
Hangfire.Redis.StackExchange
HangFire Redis storage based on HangFire.Redis but using lovely StackExchange.Redis client.
| Package Name | NuGet.org |
|---|---|
Hangfire.Redis.StackExchange |
|
Hangfire.Redis.StackExchange.StrongName |
Highlights
- Support for Hangfire Batches (feature of Hangfire Pro)
- Efficient use of Redis resources thanks to ConnectionMultiplexer
- Support for Redis Prefix, allow multiple Hangfire Instances against a single Redis DB
- Allow customization of Succeeded and Failed lists size
Despite the name,
Hangfire.Redis.StackExchange.StrongNameis not signed becauseHangfire.Coreis not yet signed.
Tutorial: Hangfire on Redis on ASP.NET Core MVC
Getting Started
To use Hangfire against Redis in an ASP.NET Core MVC projects, first you will need to install at least these two packages: Hangfire.AspNetCore and Hangfire.Redis.StackExchange.
In Startup.cs, these are the bare minimum codes that you will need to write for enabling Hangfire on Redis:
public class Startup
{
public static ConnectionMultiplexer Redis;
public Startup(IHostingEnvironment env)
{
// Other codes / configurations are omitted for brevity.
Redis = ConnectionMultiplexer.Connect(Configuration.GetConnectionString("Redis"));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddHangfire(configuration =>
{
configuration.UseRedisStorage(Redis);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseHangfireServer();
}
}
Attention: If you are using
Microsoft.Extensions.Caching.Redispackage, you will need to useHangfire.Redis.StackExchange.StrongNameinstead, because the former package requiresStackExchange.Redis.StrongNameinstead ofStackExchange.Redis!
Configurations
configuration.UseRedisStorage(...)
This method accepts two parameters:
The first parameter accepts either your Redis connection string or a
ConnectionMultiplexerobject. By recommendation of the official StackExchange.Redis documentation, it is actually recommended to create oneConnectionMultiplexerfor multiple reuse.The second parameter accepts
RedisStorageOptionsobject. As of version 1.7.0, these are the properties you can set into the said object:
namespace Hangfire.Redis
{
public class RedisStorageOptions
{
public const string DefaultPrefix = "{hangfire}:";
public RedisStorageOptions();
public TimeSpan InvisibilityTimeout { get; set; }
public TimeSpan FetchTimeout { get; set; }
public string Prefix { get; set; }
public int Db { get; set; }
public int SucceededListSize { get; set; }
public int DeletedListSize { get; set; }
}
}
It is highly recommended to set the Prefix property, to avoid overlap with other projects that targets the same Redis store!
app.UseHangfireServer(...)
This method accepts BackgroundJobServerOptions as the first parameter:
namespace Hangfire
{
public class BackgroundJobServerOptions
{
public BackgroundJobServerOptions();
public string ServerName { get; set; }
public int WorkerCount { get; set; }
public string[] Queues { get; set; }
public TimeSpan ShutdownTimeout { get; set; }
public TimeSpan SchedulePollingInterval { get; set; }
public TimeSpan HeartbeatInterval { get; set; }
public TimeSpan ServerTimeout { get; set; }
public TimeSpan ServerCheckInterval { get; set; }
public IJobFilterProvider FilterProvider { get; set; }
public JobActivator Activator { get; set; }
}
}
Of these options, several interval options may be manually set (to longer intervals) to reduce CPU load:
SchedulePollingIntervalis by default set to 15 seconds.HeartbeatIntervalis by default set to 30 seconds.ServerTimeoutandServerCheckIntervalis by default set to 5 minutes.
Dashboard
Written below is a short snippet on how to implement Hangfire dashboard in ASP.NET Core MVC applications, with limited access to cookie-authenticated users of Administrator role. Read more in official documentation.
public class AdministratorHangfireDashboardAuthorizationFilter : IDashboardAuthorizationFilter
{
public bool Authorize(DashboardContext context)
{
var user = context.GetHttpContext().User;
return user.Identity.IsAuthenticated && user.IsInRole("Administrator");
}
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCookieAuthentication(...);
// This middleware must be placed AFTER the authentication middlewares!
app.UseHangfireDashboard(options: new DashboardOptions
{
Authorization = new[] { new AdministratorHangfireDashboardAuthorizationFilter() }
});
}
Jobs via ASP.NET Core Dependency Injection Services
For cleaner and more managable application code, it is possible to define your jobs in a class that is registered via dependency injection.
public class MyHangfireJobs
{
public async Task SendGetRequest()
{
var client = new HttpClient();
await client.GetAsync("https://www.accelist.com");
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<MyHangfireJobs>();
}
Using this technique, the registered jobs service will be able to obtain other services as dependency via constructor parameters, such as Entity Framework Core DbContext; which enables the development of powerful jobs with relative ease.
Then later you can execute the jobs using generic expression:
BackgroundJob.Enqueue<MyHangfireJobs>(jobs => jobs.SendGetRequest());
BackgroundJob.Schedule<MyHangfireJobs>(jobs => jobs.SendGetRequest(), DateTimeOffset.UtcNow.AddDays(1));
RecurringJob.AddOrUpdate<MyHangfireJobs>("RecurringSendGetRequest", jobs => jobs.SendGetRequest(), Cron.Hourly());
No packages depend on Hangfire.Redis.StackExchange.
1.10.1-Beta2 - Fix #153 Appeared after 1.10.0 release. Only updates to this version for testing purposes and PLEASE report any issues. Stick to 1.9.3 for production. 1.10.0 - Fix #135 -CHANGED BEHAVIOR- Library now requeue jobs from dead server (read comments in the issue) thanks to @Lexy2 - Implement #150 (Replace async Redis calls with sync ones) thanks to @Lexy2 - Fix #148 (Can't schedule a job without specifying the queue ) thanks to @Lexy2 - Fix #142 (Scheduled jobs are returned incorrectly by the monitoring API) thanks to @Lexy2 1.9.5 - Address #139 support for netstandard2.0 - Fix #145 Added Job Queue parameter to the job information - Update to Hangfire 1.8.12 and StackExchange.Redis 2.7.33 Big thanks to @Lexy2 for all the work on this release 1.9.4 - Update to Hangfire 1.8.7 - Add Support for all features defined for the storage in Hangfire 1.8.7 - Update StackExchange.Redis to 2.7.10 1.9.3 - Fix the missing key prefixing (:) for GetSetCount and GetSetContains (thanks to BobSilent) 1.9.2 - Failed jobs page lists new items first (thanks to toddlucas) 1.9.1 - Downgrade to netStandard 2.0 for broader compatibility 1.9 - Switched AsyncLocal storage of redis lock to ThreadLocal - BREAKING CHANGE: Drop support for net461 - Added compatibility to Hangfire.Core 1.8 - BREAKING CHANGE: Namespace changes to uniform with folders (It wrongly appeared in previous ver) 1.8.5 - Speed up Recurring jobs Fetching (thanks to developingjim) 1.8.4 - Fix #90, #91 concurrent access on dictinoary (thanks to luukholleman) - Fix #94 occasional error in monitoringApi (thanks to tsu1980) 1.8.3 - Removed dependency to NewtonSoft.JSON (thanks to neyromant) 1.8.2 - Updated Hangfire to 1.7.11 (thanks to abarducci) - Updated StackExchange.Redis to 2.1.30 (thanks to abarducci) 1.8.1 - Updated Hangfire to 1.7.8 - Fixed #82 (thanks to @bcuff) 1.8.0 - Updated StackExchange.Redis to 2.0 (thanks to @andrebires) 1.7.2 - Added support for Lifo Queues (thanks to AndreSteenbergen) - Added option to not use transaction (thanks to AndreSteenbergen) - Enabled sliding expiration for distributed locks (thanks to pieceofsummer) - Add epired jobs watch to cleanup succeeded/deleted lists (thanks to pieceofsummer) - Make succeeded and deleted lists size configurable (thanks to pieceofsummer) - Fix job state order (thanks to pieceofsummer) - Exclude Fetched property from job parameters (thanks to pieceofsummer) 1.7.1 - Add expired jobs watcher to cleanup succeeded/deleted lists thanks to pieceofsummer 1.7.0 - Redis Cluster support (#42 thanks to gzpbx) - Update to VS2017 (#48 thanks to ryanelian)
.NET Standard 2.0
- Hangfire.Core (>= 1.8.12)
- Newtonsoft.Json (>= 13.0.3)
- StackExchange.Redis (>= 2.7.33)
| Version | Downloads | Last updated |
|---|---|---|
| 1.10.1-beta2 | 1 | 03/12/2025 |
| 1.10.1-beta | 1 | 03/12/2025 |
| 1.9.4 | 11 | 03/12/2025 |
| 1.9.4-beta.1 | 1 | 03/12/2025 |
| 1.9.4-beta | 2 | 03/12/2025 |
| 1.9.3 | 3 | 03/12/2025 |
| 1.9.3-beta | 2 | 03/12/2025 |
| 1.9.2 | 3 | 03/12/2025 |
| 1.9.1-beta | 2 | 03/12/2025 |
| 1.9.0 | 3 | 03/12/2025 |
| 1.9.0-beta | 2 | 03/12/2025 |
| 1.8.7 | 3 | 03/12/2025 |
| 1.8.6 | 3 | 03/12/2025 |
| 1.8.5 | 3 | 03/12/2025 |
| 1.8.4 | 3 | 03/12/2025 |
| 1.8.3 | 3 | 03/12/2025 |
| 1.8.2 | 3 | 03/12/2025 |
| 1.8.1 | 3 | 03/12/2025 |
| 1.8.0 | 3 | 03/12/2025 |
| 1.7.2 | 3 | 03/12/2025 |
| 1.7.0 | 3 | 03/12/2025 |
| 1.6.7.24 | 1 | 03/12/2025 |
| 1.6.7.7 | 1 | 03/12/2025 |
| 1.6.7.5 | 1 | 03/12/2025 |
| 1.6.7.4 | 1 | 03/12/2025 |
| 1.6.7.3 | 1 | 03/12/2025 |
| 1.6.7.1 | 1 | 03/12/2025 |
| 1.6.6.42 | 1 | 03/12/2025 |
| 1.6.6.41 | 1 | 03/12/2025 |
| 1.6.6.39 | 1 | 03/12/2025 |
| 1.6.6.37 | 1 | 03/12/2025 |
| 1.6.6.36 | 1 | 03/12/2025 |
| 1.6.6.31 | 1 | 03/12/2025 |
| 1.6.6 | 3 | 03/12/2025 |
| 1.6.5.24 | 1 | 03/12/2025 |
| 1.5.4.126 | 1 | 03/12/2025 |
| 1.5.4.116 | 1 | 03/12/2025 |
| 1.5.3 | 3 | 03/12/2025 |
| 1.5.3-rc | 3 | 03/12/2025 |
| 1.4.8 | 3 | 03/12/2025 |
| 1.4.7 | 3 | 03/12/2025 |
| 1.4.6 | 3 | 03/12/2025 |
| 1.4.5 | 3 | 03/12/2025 |
| 1.4.2 | 3 | 03/12/2025 |
| 1.4.1.3 | 1 | 03/12/2025 |
| 1.4.1.2 | 1 | 03/12/2025 |
| 1.1.4 | 3 | 03/12/2025 |
| 1.1.2-beta | 2 | 03/12/2025 |
| 1.1.0-beta | 2 | 03/12/2025 |
| 1.0.6-alpha | 2 | 03/12/2025 |
| 1.0.5-alpha | 2 | 03/12/2025 |
| 1.0.4-alpha | 2 | 03/12/2025 |
| 1.0.3-alpha | 2 | 03/12/2025 |
| 1.0.2-beta | 2 | 03/12/2025 |
| 1.0.2-alpha | 2 | 03/12/2025 |
| 1.0.1-alpha | 2 | 03/12/2025 |
| 1.0.0-alpha | 2 | 03/12/2025 |