3

Is it a good practice to Wait/Sleep inside the RoleEntryPoint.OnStart method of a webrole? We want to make sure our service is fully ready before we tell Azure that we are ready to service request.

Prapti
  • 65
  • 1
  • 5

3 Answers3

3

Yes you can wait for a while in the OnStart method to initialize services.

OnStart performs the initialization of your system. If you have a service that you need to setup and initialize before you enter running state of your role you need to initialize that in the OnStart before you exit that method.

The role environment will continue to calling the Run method of your RoleEntryPoint right after you exit OnStart. The exact time it takes to call the next method is unknown. This will also move your service from RoleInstanceStatus.Busy to RoleInstanceStatus.Ready at which time the role instance will begin to receive traffic.

As far as I know there is no initialization timeout for the role instances. (The OnStop method on the other hand, when your instance for what ever reason shuts down, does, for practical reasons, have a limited time to finish executing. Not sure but at one time I heard five or fifteen seconds.)

noopman
  • 660
  • 1
  • 4
  • 15
  • There are initialisation timeouts. Microsoft Services told me that roles have 15 minutes to enter the ready state otherwise other update domains are brought down for updates. This means all of your roles could be brought down at once. Our Azure app just got caught out - during an automatic Azure OS upgrade, a worker role took over 15 minutes to do its start tasks (a problem of ours) and then Azure started upgrading roles in the other Update Domain. This meant roles in both update domains - and our app - were down at the same time. – Yoshi Mar 07 '13 at 20:58
  • The timeouts for OS upgrades are documented here http://msdn.microsoft.com/en-us/library/windowsazure/hh543978.aspx – sharptooth May 22 '13 at 10:11
1

As far as I know, when inside the OnStart the role status would be at Busy, which means it reported to the Fabric Controller that it still in initialization phase. I don't think it's good or bad to sleep in OnStart, but you might need to consider the role start timeout.

I think the better idea would be put your waiting logic in Run with some flags, so that it cannot accept the request until you are ready.

Shaun Xu
  • 4,476
  • 2
  • 27
  • 41
  • Not sure why you'd recommend putting artificial wait statements in `Run()`. You can do all of your setup in `OnStart()` and never leave that method until you're ready to be added to the load balancer. – David Makogon Feb 15 '12 at 04:56
  • As far as I know keep the application in OnStart will make the deployment status stay in Busy. And I remembered that there's a timeout for role initializing. If it takes too long to finalize the OnStart the Fabric Controller might report the deployment failed, I guess. – Shaun Xu Feb 15 '12 at 05:31
1

The purporse of the OnStart method is to perform all the initialization within. So, after this function exits, your instance is supposed to be fully functional. In your case I think it is good to wait a little bit in OnStart. During that time the role will have "Busy" status and will not receive any incoming web requests but still can communicate with storage and database.

In our project we have OnStart downloading all the necessary data from Blob storage, unpacking and registering it. Then we check that role is healthy and only after that leave the OnStart method.

corvus
  • 2,330
  • 2
  • 18
  • 16