如何计划在Windows Azure中工作者角色任务 [英] How to Schedule a task in windows azure worker role
问题描述
我有在中午12点执行任务,每天一个简单的Azure工作者角色上运行。下面是code,完成此。
I have a simple Azure Worker role running that performs a task every day at 12 PM. Below is the code that accomplishes this.
public override void Run()
{
try
{
while (true)
{
int time = Convert.ToInt32(DateTime.Now.TimeOfDay);
if (time == 12)
{
DoSomethingElse();
}
}
}
catch (Exception ex)
{
Log.Add(ex, true);
}
}
下面 DoSomethingElse()
是在每天下午12点发送电子邮件的方法,同时也触发一次,每天只有一次。
Here DoSomethingElse()
is a method to send an email at every day 12 PM, and also fires once and only once per day.
我如何能实现调度火灾时,时间是中午12点执行 DoSomethingElse()
。
How can I implement a scheduler that fire when the time is 12PM and execute DoSomethingElse()
.
我的问题是:这是(高于code)是最好的方法或使用任何第三方工具。
My question is: Is this (above code) is the best method or use any 3rd party tool.
推荐答案
有几个在这里的其他问题的处理这(和我上面标之一)。话虽如此,在重复什么其他的答案已经状态的风险:
There are several other questions here that deal with this (and I've marked one above). Having said that, and at the risk of repeating what other answers already state:
在你的情况,在Windows Azure的队列一个简单的信息,延时不显示,直到中午时分,会工作。这也有助于应对多实例情况:如果你运行你的角色的两个实例,你不想跑两次相同的计划任务,所以您需要一种方法来只有这些实例之一执行此code。这是很容易通过队列中的消息处理,或者你可以通过使用类似一个blob租赁(可能只有反对一个写锁)作为一个互斥体上的单个实例上运行调度code。这是覆盖在@ smarx的博客文章,这里 。
In your case, a simple message on a Windows Azure Queue, time-delayed to not show up until noon, would work. This also helps deal with multi-instance scenarios: If you're running two instances of your role, you don't want the same scheduled task running twice, so you need a way to have only one of those instances execute this code. This is easily handled via queue message, or you could run scheduler code on a single instance by using something like a blob lease (which may only have one write-lock against it) as a mutex. This is covered in @smarx's blog post, here.
这篇关于如何计划在Windows Azure中工作者角色任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!