Skip to content

Instantly share code, notes, and snippets.

@odinserj
Created April 30, 2018 14:43
Show Gist options
  • Save odinserj/c7c904086c7b6f9b05cfcd8f968f1785 to your computer and use it in GitHub Desktop.
Save odinserj/c7c904086c7b6f9b05cfcd8f968f1785 to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using ConsoleSample;
using Hangfire;
using Hangfire.Logging;
using Hangfire.Pro.Redis;
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
[assembly: OwinStartup(typeof(Startup))]
namespace ConsoleSample
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseErrorPage();
app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "critical", "default" } }, new RedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False"));
app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "critical", "default" } }, new RedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False"));
app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "critical", "default" } }, new RedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False"));
app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "critical", "default" } }, new RedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False"));
app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "critical", "default" } }, new RedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False"));
app.UseHangfireDashboard("");
}
}
internal class Program
{
[Queue("critical")]
public static void Empty() { }
[Queue("default")]
public static void CreateJobs(string batchId)
{
BatchJob.Attach(batchId, leaf =>
{
Parallel.For(0, 5, j => { leaf.Enqueue(() => Empty()); });
});
}
[Queue("default")]
public static void CreateNested(string batchId)
{
BatchJob.Attach(batchId, nested =>
{
Parallel.For(0, 200, i =>
{
nested.StartNew(leaf =>
{
leaf.Enqueue(() => CreateJobs(leaf.BatchId));
});
});
});
}
private static void Main()
{
GlobalConfiguration.Configuration
.UseLogProvider(new ColouredConsoleLogProvider2(LogLevel.Info))
.UseRedisStorage("hangfire-redis.redis.cache.windows.net:6380,password=****,ssl=True,abortConnect=False")
.UseRedisMetrics()
.UseBatches();
RecurringJob.AddOrUpdate(() => Console.WriteLine("Hello!"), Cron.Minutely);
using (WebApp.Start<Startup>("http://localhost:12006"))
{
BatchJob.StartNew(batch =>
{
Parallel.For(0, 1000, i =>
{
batch.StartNew(nested => { nested.Enqueue(() => CreateNested(nested.BatchId)); });
});
});
Console.ReadLine();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment