Restart Azure Worker Role from Web Role

南笙酒味 提交于 2019-12-06 11:55:46

Anything like posting an event to an Azure Queue, posting a blob to Azure Blobs, changing a record in Azure Tables or even making some change in SQL Azure will work - the web role will do the change and the worker role will wait for that change. Perhaps Azure Queues would be the cleanest way, although I'm not sure.

One very important thing you should watch for is that if you decide to use polling - like query a blob until it appears - you should insert a delay between the queries, otherwise this code:

while( true ) {
   if( storage.BlobExists( blobName ) ) {
       break;
   }
}

will rush into the storage and you'll encounter outrageous transaction fees. In case of SQL Azure you will not see any fees, but you'll waste the service capacity for no good and this will slow down other operations you queue to SQL Azure.

This is how is should be done:

while( true ) {
   if( storage.BlobExists( blobName ) ) {
       break;
   }
   // value should not be less that several hundred (milliseconds)
   System.Threading.Thread.Sleep( 15 * 1000 );
}

Well I suggest you use Azure Fluent Management (which uses the Service Management API internally). Take a look at the "Deploying to Windows Azure" page.

What you will want to do is the following:

  • Cloud Service: mywebapp.cloudapp.net
    • Production slot
      • Role: MyMvcApplication
  • Cloud Service: mybackgroundworker.cloudapp.net
    • Production slot
      • No DEPLOYMENT

So you would typically have a Cloud Service running with a Web Role and that's it. What you do next is create the Worker Role, add your code, package it to a cspkg file and upload it to blob storage.

Finally you would have some code in your Web Role that can deploy (or remove) the Worker Role to that other Cloud Service by downloading the package locally and then running code similar to this:

 var subscriptionManager = new SubscriptionManager(TestConstants.SubscriptionId);
 var deploymentManager = subscriptionManager.GetDeploymentManager();

 deploymentManager
       .AddCertificateFromStore(Constants.Thumbprint)
       .ForNewDeployment(TestConstants.HostedServiceName)
       .SetCspkgEndpoint(@"C:\mypackage")
       .WithNewHostedService("myelastatestservice")
       .WithStorageAccount("account")
       .AddDescription("my new service")
       .AddLocation(LocationConstants.NorthEurope)
       .GoHostedServiceDeployment();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!