Properly handle Windows Azure OnStop events

Source: Internet
Author: User
Keywords Azure azure onstop

Author information: This article is by the Windows http://www.aliyun.com/zixun/aggregation/13357.html ">azure and asp.net MVC group programmer/writer Rick Anderson."

Restart Web Roles

How to reboot is a frequently overlooked issue in Windows Azure. Proper handling of restart operations is essential so that you do not lose data or damage your intrinsic data, enabling you to quickly shut down, restart, and efficiently process new requests. Windows Azure Cloud Service is approximately restarted two times a month to update the operating system. (For details on operating system updates, see role Instance restarts Due to OS upgrades.) When a page application closes, the Roleenvironment.stopping event is triggered. Because the Web role boilerplate created by Visual Studio does not override the OnStop method, the application has only a few seconds to process the HTTP request before shutting down the computer. If your web role is busy processing a pending request, some requests may be lost. You can postpone the restart time or let your Web role defer for up to 5 minutes to rewrite the OnStop method and call sleep, but this is not ideal. Once the stopping event is raised, Load Balance (LB) stops sending requests to web role, so delaying the shutdown time is longer than processing the pending request to keep your virtual machine in a sleep state. The best approach is to wait for the OnStop method until there are no more requests, and then start the shutdown. The earlier the shutdown, the sooner the VM restarts and starts processing the request. To use the best method to shut down the computer, add the following code to the Webrole class.

The code above checks the current counter of the ASP.net request. The OnStop method calls the sleep latency shutdown whenever there is a request. Once the counter of the current request drops to zero, OnStop returns and initiates the shutdown. If the Web server is too busy to finish processing a pending request within 5 minutes, the application will also force the shutdown. Remember that once the stopping event is triggered, LB stops sending requests to the web role, so unless you have a large number of web role (or too few instances), never more than a few seconds to complete the current request.

The above code writes data for trace, but unless you perform a complex on-demand transmits, the trace data for the OnStop method will never appear on wadlogstable. Later, I'll show you how to use DebugView to view these trace events. And how to get tracing sharable in the OnStart method of Web role.

Optimal restart method for Worker roles

Handling stopping events in a worker role requires a different approach. Typically, worker role processes queue messages in the Run method. The method includes two global variables; one notification Run method stopping event has been triggered, and another notifies the OnStop method to safely start the shutdown. (Start shutdown after OnStop returns). The following code illustrates both of these global methods.

When OnStop calls, the global variable onstopcalled is set to true, and if no queue events need to be processed, in the Run method you can determine whether to shut down at the first level of the loop based on the value of onstopcalled.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.