I’m trying to find a way to avoid parallel method execution with multiple workers.
I have A, B and C methods to enqueue into hangfire server. And these method will be enqueued many times.
For example, when queue is like this [A1, A2, B1, B2, C1, B3, C2]
and worker count is 3.
I want to avoid the situation that two method A work at the same time since A method requires huge memory.
[worker1: A1, worker2: A2, worker3: B1] <- avoid the situation...
But B and C need to work in parallel.
[worker1: A1, worker2: B1, worker3: B2]
[worker1: C1, worker2: A2, worker3: B3]
Any idea?
Thanks!
Run two servers, one with a single worker that executes just A jobs; and another with as many workers as you need for B and C jobs.
Thank you for your idea!
I tried to implement your suggestion on an ASP.NET app like the following and it worked!
public class Startup
{
public void Configuration(IAppBuilder app)
{
GlobalConfiguration.Configuration.UseSqlServerStorage(
"Server=localhost;Database=Hangfire;");
app.UseHangfireDashboard();
app.UseHangfireServer(new BackgroundJobServerOptions { ServerName = "server1", WorkerCount = 1 });
app.UseHangfireServer(new BackgroundJobServerOptions { ServerName = "server2", WorkerCount = 5 });
}
}
public class HomeController : Controller
{
public ActionResult Index(int job)
{
using (var server = new BackgroundJobServer(
new BackgroundJobServerOptions { ServerName = "server1" }))
{
BackgroundJob.Enqueue(() => DoHeavyJob());
}
using (var server = new BackgroundJobServer(
new BackgroundJobServerOptions { ServerName = "server2" }))
{
BackgroundJob.Enqueue(() => DoLightJob);
}
...
}
}