Windows Azure is considered to be a cloud based operating system which allows developers to leverage a network of computers to permit what amounts to infinite scaling. This scaling is all managed within data centers run by Microsoft. Of the many uses for Azure one is computation. With the ability to leverage so many computers the prospect of performing heavy computation becomes very attractive. For this purpose, Azure supports what are known as Worker roles which act as background processing agents.
If you have flirted with Azure at all, you know that one of the most popular uses is to host web applications and allow for “n” instances to be spun up and down at will, this allowing companies to hug the demand curve rather then planning for the highest usage point. The roles which carry this out are called Web roles. However, the other type of role, the one which carries out background processing is called a Worker role.
Worker roles are much like Web roles in that they have OnStart and OnEnd events, but a web role is, by default, externally accessible, a worker role is not. Think of a worker role as a messenger boy, they allow for communication.
I have constructed a rather simple example to show them in action. What we have is a simple guestbook which allows a guest to leave his name, a message, and a picture. All of this information is stored in a blob and saved to my local storage account, however, the worker role is ticking every second and if its see’s a message in the queue, it will process that message. In this case, the message will be the address to the blob and the action will be to create three images: one resized to half the width of the original, one resized to 1/4 of the original, and the final one is the original itself with all meta information.
Below is the code for the Run method, which is invoked as the role begins executing, and the ProcessMessage method which executes a message when it is found in the queue:
Personally, I really hate how they have this setup. Basically defining a busy wait inside the role as it looks for something to do. I thinking a better process would be to find a way to wire the role up a queue within the web role and make this event driven. Seems like it would make more sense given the structure of .NET in general.
This code is fairly straightforward. If you wish to see more of the code and how I am doing the resizing, I invite you to download the source available at the bottom of this page in Visual Studio 2010 format.
Worker roles are a very interesting concept, though by no means new to the world. The fact that they are totally managed by the Azure cloud makes them easy to deal with. I also suspect that using the FabricController you can control how many worker roles are spun up or down at a time. That is likely to be next experiment. The final piece of code to talk about is the action which handles the saving of the Guestbook sign request, for it also speaks to the Queue so the worker role can be alerted it has a job to do.
The key code to look at here is lines 24-26, though admittingly you wont see much as I have it wrapped by my StorageHelper class, which is available via the download. The way I have it setup the worker role checks every second for a message. Still even with such a short delay as I cannot guarantee the new pictures will be in place, I have to use the original for display or do a resize here and send the original dimensions as part of the message.
That is a weakness of the Worker role, with respect to web development. We will not see the work done instantly for things like resizing or even program maintenance. One has to be very careful when using Worker roles.