I previousely worked on some Windows Services that can stop automatically when encountering critical errors.
Mig开发者_StackOverflow中文版rating those to Windows Azure, in WorkerRole, I'd like to do the same : being able to stop the WorkerRole execution from within.
I just encounter a little problem...
When the Run method ends, the WorkerRole OnStop method is triggered, then it's restarted... From what I've read, it's a normal behavior for a WorkerRole.
Then my question is : how to tell it 'job complete, do not restart please'
You can't stop a specific role instance. You can scale down from, say, 3 instances to 1, but you can't choose which ones to terminate.
If you're talking about a single-instance worker role that needs to start up, process something, then shut down (say, once a day), consider moving this role into its own deploymnt. Then, using the management API, deploy it, let it run for an hour (or however long you need it), and then suspend+delete it.
I'm not really sure this is actually possible. You could use
Thread.Sleep(Timeout.Infinite);
But since the worker role is deployed onto the cloud you will still have to pay I think The only way to actually stop stop this is by suspending the whole role from the Azure Platform.
If your particular role uses only a single instance, you can lower the instance count for that role to 0 through the management API. Switching of a particular role is not a supported feature, but you can get it to work by building a kill switch service that you can call on a particular instance and simultaneously lower the number of instances. This requires a bit of plumbing code, but is doable.
If I am reading your question correctly.... The worker role process is stopped but then gets started again?
I have run into issues where, if the worker role runs for a long time it essentially starts again. What happens is that Azure assumes that after a long time (generally 30 seconds) it assumes the role has hung and will write the Message back to the worker queue to ensure the process is completed.
I am taking a stab here, but is it possible in your scenario that the stopping of the worker role does not result in the message being deleted from the Worker Role message queue? If so, you may want to see if there is way to explicitly delete the message you are processing before you terminate the process.
More info here
http://guerillaprogrammer.com/jakew/post/2010/03/23/Expiring-Azure-Queue-Message.aspx
I think you can use Thread.Suspend() http://msdn.microsoft.com/en-us/library/system.threading.thread.suspend.aspx and if you want to resume it again use Thread.Resume()
recently I come across the same problem. My problem is fixed by commenting worker role part from ServiceConfiguration.Cloud.cscfg,ServiceConfiguration.Local.cscfg and ServiceDefinition.csdef file and Packaging the project.
精彩评论