brushwood
brushwood

Reputation: 115

azure cloud service OnStop() and RoleEnvironmentStopping not called on scaling

I have an Azure cloud services web role that needs to make a call to a REST service when it starts up and then when it shuts down to register the role instance with a proxy server on start and remove it when stopping. I overrode the OnStart() and OnStop() methods to add the calls and it works as expected in all instances except when scaling down. When stopping the cloud service or restarting a role instance, the call to the REST service in the OnStop() method runs and completes successfully. When scaling up, the OnStart() method runs and the call to the REST service registers the new role instance with the proxy. However, when scaling back down it looks like the OnStop() method never runs. I also tried using the RoleEnvironmentStopping event handler since that does not have a time limit but the result was the same. I can't find any detailed documentation from Microsoft on the exact lifecycle of a role instance during the scaling action. When I look at my virtual network, it looks like the role instance has it's ip address removed well before the vm is destroyed. Does it make sense that during scaling, the role instance does not have network access when the OnStop() method is called? Is there somewhere else I can/should be doing this cleanup?

Upvotes: 0

Views: 651

Answers (1)

kwill
kwill

Reputation: 11008

OnStop does get called during scale down operations. Typically when I have seen this sort of issue it is due to the code in OnStop writing logging to diagnostics (ie. Diagnostics.Trace lines), but Windows Azure Diagnostics not having enough time to transfer that diagnostics data to storage before the VM is shut down. This makes it look like OnStop is not being called, but in reality it is just the logs not being put into storage.

There are a few ways to verify this:

  1. Manually write something to storage in OnStop. You could add a message to a queue, write an entry to a table, etc.
  2. Force an on demand diagnostics transfer in OnStop. You can see sample code in the RequestOnDemandTransfer() function at http://www.packtpub.com/article/digging-into-windows-azure-diagnostics-tutotial.
  3. Hook up a debugger and set a breakpoint on your OnStop method. If you are using SDK 2.2 you can use the remote debugger functionality from within the Server Explorer tool in Visual Studio. If you are not then you will need to use WinDBG on the VM itself.

Upvotes: 1

Related Questions