Thursday, December 10, 2009

Windows Azure role stuck in Initializing/Busy/Stopping

Recently I see a lot of questions with that description around Windows Azure Forums. There is one particular reason that appears to be causing this behaviour in most of the cases. I want to describe it with respect to Web Role and Worker Role

Worker Role

While requirements for referenced assemblies in Worker roles are same as requirements for these in Web Roles, there is another important point to observe. And this is that your Run() method should never end. If it ends, than Windows Azure Fabric thinks there is something wrong are recycles your role. That’s why you initial Run method will look like this:

1publicoverridevoid Run()

2 {

3// This is a sample worker implementation. Replace with your logic.

4Trace.WriteLine("WorkerRole2 entry point called", "Information");

5

6while (true)

7 {

8Thread.Sleep(10000);

9Trace.WriteLine("Working", "Information");

10 }

11 }

It is important to not remove the while (true) cycle as it it the “lifecycle” of our worker role. If we remove it,our worker role will enter an endless loop of:

[runtime] Role entrypoint . COMPLETED OnStart() [runtime] Role entrypoint . CALLING Run() Information: Worker Process entry point called [runtime] Role entrypoint . COMPLETED Run() ==> ROLE RECYCLING INITIATED[runtime] Role instance recycling is starting Information: ... Information: ... [fabric] Role state Recycle [fabric] Role state Stopping [fabric] Role state Stopped [fabric] Role state Stopping [fabric] Role state Stopped [fabric] Role state Suspended [fabric] Role state Aborted [fabric] Role state Teardown [fabric] Role state Destroyed [fabric] Role state Created [fabric] Role state Suspended

The model is that you're expected to never return from Run(). (If you do, we think something went wrong and restart you.) If you have other threads doing work, you can just go into an infinite sleep at the bottom of Run(). (Thread.Sleep(Timeout.Infinite))

Disclaimer

This is my personal blog space. Everything which I write here or anywhere on the web is solely my own subjective opinion and shall not be associated (neither affiliated) with any company - neither the company I currently work for, nor any company I have worked for in the past. Use the information and any code / links provided at your own risk.