Skip to content

Instantly share code, notes, and snippets.

@odinserj
Last active July 18, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odinserj/0e6644059069b883e7a04569bc34be64 to your computer and use it in GitHub Desktop.
Save odinserj/0e6644059069b883e7a04569bc34be64 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
using Hangfire;
namespace MyApplication
{
public class HomeController : Controller
{
private readonly IBackgroundJobClient _backgroundJobs;
public HomeController()
{
_backgroundJobs = JobStorageRegistry.GetBackgroundJobClient("hf-low-performance");
}
public ActionResult Index()
{
_backgroundJobs.Enqueue(() => Console.WriteLine("Hello, \"hf-low-performance\""));
return View();
}
}
}
using System;
using System.Collections.Concurrent;
using Hangfire;
namespace MyApplication
{
public static class JobStorageRegistry
{
public static readonly ConcurrentDictionary<string, JobStorage> Storages = new ConcurrentDictionary<string, JobStorage>();
public static IRecurringJobManager GetRecurringJobManager(string storageKey)
{
if (!Storages.TryGetValue(storageKey, out var storage))
{
throw new InvalidOperationException($"No storage registered for the '{storageKey}' key");
}
return new RecurringJobManager(storage);
}
public static IBackgroundJobClient GetBackgroundJobClient(string storageKey)
{
if (!Storages.TryGetValue(storageKey, out var storage))
{
throw new InvalidOperationException($"No storage registered for the '{storageKey}' key");
}
return new BackgroundJobClient(storage);
}
}
}
using Hangfire;
using Hangfire.Pro.Redis;
using Microsoft.Owin;
using MyApplication;
using Owin;
[assembly: OwinStartup(typeof(Startup))]
namespace MyApplication
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSerilogLogProvider();
JobStorageRegistry.Storages.TryAdd("hf-low-performance", new RedisStorage("localhost:6379", new RedisStorageOptions
{
Prefix = "hf-low-performance"
}));
JobStorageRegistry.Storages.TryAdd("hf-high-performance", new RedisStorage("localhost:6379", new RedisStorageOptions
{
Prefix = "hf-high-performance"
}));
foreach (var storage in JobStorageRegistry.Storages)
{
app.UseHangfireDashboard($"/{storage.Key}", new DashboardOptions(), storage.Value);
app.UseHangfireServer(new BackgroundJobServerOptions(), storage.Value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment