Windows Azure Platform Family of articles Catalog
In the previous chapter, we mentioned that Windows Azure calls the OnStart () method and the Run () method of the role instance in turn.
In this section we will take a closer look at the life cycle of role. First, let's go behind the scenes and see how a role instance is posted to the virtual machine and started up.
Role the process of deploying and running on a virtual machine
The following is a brief procedure for the release and launch of Role instances (roles Instance).
- Windows Azure chooses a host server with enough CPU cores in the server pool, or starts a new host server that meets the requirements
- Windows Azure copies the cloud service pack and configuration files to the host server. A hosting agent (host agent) on the host server initiates the virtual operating system.
- There is an agent named Waappagent on the virtual operating system. This agent is responsible for configuring the virtual operating system and initiating a process called wahostbootstrapper.
- If a startup task is defined on the role, Wahostbootstrapper runs these startup tasks and waits for all startup tasks marked as simple to execute correctly
- If the role is a Web role (web roles), Wahostbootstrapper starts iisconfigurator to configure IIS
- If the role is a worker role, Wahostbootstrapper starts a process named Waworkerhost, and if the role is a web role, Wahostbootstrapper initiates a process called Waiishost
- In the above process, load the assembly of the role and search for its implemented Roleentrypoint subclass
- Call the OnStart () method
- Call the Run () method. At the same time, the instance is marked as "ready" and added to the load balancer
- If the run () method exits, the OnStop () method is called. Waworkerhost/waiishost End Run, instance restart
- Wahostbootstrapper start cycle monitoring of the running state of the instance
Web role does not necessarily need to implement the Roleentrypoint class, because Web role is ultimately deployed on IIS and is the request of IIS to accept and forward users.
That's why if we remove WebRole.cs from a Web project, it doesn't affect the reason why the site is running.
However, if your web role implements the Roleentrypoint class, you want to make sure that the run () method does not exit or the instance restarts.
Status of Role instances (roles Instance)
Role instance can be in a different state: Busy or ready.
The allocation of load balancing tasks is only available when role is in the ready state.
If you want to temporarily change the state of role instance in your code, you can respond to Statuscheck (state check) events defined by the Roleenvironment class. The instance is marked as busy by the Setbusy () method Roleinstancestatuscheckeventargs in the event arguments. This way, the load balancer does not assign the task to the current instance until the next Wahostbootstrapper checks the status of the instance.
If your role Instance code exits the Run () method while it is running, or if an unhandled exception (Unhandledexceptions) is thrown, Wahostbootstrapper will restart your role Instance. This mechanism of automatic recovery can help improve the usability of the system. Of course, during a reboot, this instance cannot be accepted by the user in a non-ready state. If wahostbootstrapper itself crashes or the virtual machine goes down, Windows Azure restarts the virtual machine and role Instance. If the hosting server is down, Windows Azure will redeploy role instance to a healthy server after several recovery retries.
During Windows Azure shutdown role instance, the stopping event is triggered and the OnStop () method of the role is called. Here you can implement the processing that is required during the instance shutdown process, such as releasing the resources used by role.
Finally, if you want to force a restart of your instance, you can use the Roleenvironment.requestrecycle () method to notify Windows Azure to restart the instance.
The lifecycle of Windows Azure Cloud Service role