Multiple Queues(Servers) With Different Worker Counts In Hangfire

Recently I came across a requirement in a .Net Web API project to handle some time consuming requests to another system which is beyond from my control. Because of this users have to wait till the requests are completed. So to overcome the issue my team came up with Hangfire; a cool open source queue which is an easy way to perform fire-and-forget, delayed and recurring tasks inside ASP.NET applications.

Hangfire: http://hangfire.io/

image

In this posts I’m going to talk about the configuration and some features I used in my project but if you browse through the official site you can come across may other features in the Hangfire tool.

Install Hangfire

Hangfire have allowed developers to add Hangfire to project through Manage NuGet Packages. once you install Hangfire, two other components will be installed to your project which are depended.

image

Configure Mutiple Hangfire Servers

To configure Hangfire you need to do the following.

In Global.asax.cs file of you Web Api Project add the following code to Application_Start() method. By placing this code you will configure Hangfire and Sql Server. In my case I have configure my Data Context.

protected void Application_Start()
{
Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("WMSDataContext");
}

 

Now we have to tell our application to initialize Hangfire. In Startup.cs add the following code to the Configuration(IAppBuilder app) method. This where you have to specify the servers and queues you will be needing in your project.

In my user requirement I need to create two servers with single queues with different worker counts. I have one queue in one server with one worker count and another with more than one worker counts. one server with sequential processing and one with multiple processing at a time.

public class Startup
{
public void Configuration(IAppBuilder app)
{
var mainQOptions = new BackgroundJobServerOptions
{
ServerName = String.Format("{0}:mainqueue", Environment.MachineName),
WorkerCount = Environment.ProcessorCount * 5,
Queues = new[] { "mainqueue" }
};
var sequentialQOptions = new BackgroundJobServerOptions
{
ServerName = String.Format("{0}:sequentialqueue", Environment.MachineName),
WorkerCount = 1,
Queues = new[] { "sequentialqueue" }
};

app.UseHangfireDashboard();
app.UseHangfire(config =>
{
config.UseServer(mainQOptions);
config.UseServer(sequentialQOptions);
});
}
}

Use Hangfire

Now I’ll show you how to use the in your code. Following code shows how to direct the method to be executed to the correct queue.

MethodToExcute() >>> Method that you are going to queue in Hangfire.

var client = new BackgroundJobClient();
var state = new EnqueuedState("sequentialqueue");
var job = Job.FromExpression(() => MethodToExcute());
string jobId = client.Create(job, state); //oPutawayFacade.PutawayBox(boxNumber, pallet, userInfo.M3UserName, userInfo.M3Password);

Hangfire Dashboard

Hangfire has it own amazing dashboard where you can track the progress and the status of the tasks you have queued.

This is the two servers which was created from the above code.

image

These are some feature I used from Hangfire tool. there are more features that can be used by you in your projects. You can Schedule, Delay tasks in the queues.

Jump Into Development With Kinect V2

During the past couple days I have been working on really cool demo project. Rotate a 3D image using Kinect V2!! It was really difficult to me to find some guide lines to development apart from the tutorial there in the SDK.

If you want to understand how things work, keep reading!

Prerequisites

Installation

From Manage NuGet you can install Kinect v2 to your project.image

Activate the Kinect Sensor

KinectSensor _sensor = KinectSensor.GetDefault();
if (_sensor != null)
{
_sensor.Open();
_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);

_reader.MultiSourceFrameArrived += Reader_MultiSourceFrameArrived;
}

Kinect Sample Video