[英]How to Schedule a task in windows azure worker role
I have a simple Azure Worker role running that performs a task every day at 12 PM. 我有一个简单的Azure Worker角色,每天下午12点执行任务。 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);
}
}
Here DoSomethingElse()
is a method to send an email at every day 12 PM, and also fires once and only once per day. 这里
DoSomethingElse()
是一种在每天下午12点发送电子邮件的方法,并且每天只触发一次。
How can I implement a scheduler that fire when the time is 12PM and execute DoSomethingElse()
. 如何实现在时间12PM时触发的调度程序并执行
DoSomethingElse()
。
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:
话虽如此,并且有可能重复其他答案已经陈述:
In your case, a simple message on a Windows Azure Queue, time-delayed to not show up until noon, would work. 在您的情况下,Windows Azure队列上的一条简单消息会延时,直到中午才会显示,可以正常工作。 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.
这可以通过队列消息轻松处理,或者您可以在单个实例上运行调度程序代码,方法是使用类似blob租约(可能只有一个写入锁定)作为互斥锁。 This is covered in @smarx's blog post, here .
@ smarx的博客文章中介绍了这一点 。
You could also use Quartz.Net http://quartznet.sourceforge.net/ using the blob lease mentioned ab ove is a great way to make sure only one of your instances is hosting tasks. 您也可以使用Quartz.Net http://quartznet.sourceforge.net/使用提到的blob租约,这是确保只有一个实例托管任务的好方法。
Using Quartz.Net to Schedule Jobs in Windows Azure Worker Roles 使用Quartz.Net在Windows Azure工作者角色中安排作业
Cloud Scheduler specifically deals with task scheduling in the cloud. Cloud Scheduler专门处理云中的任务调度。
I just came across this so I Haven't tried it. 我刚刚遇到这个,所以我没试过。
http://getcloudscheduler.com/ http://getcloudscheduler.com/
Update : Forget cloud scheduler! 更新 :忘记云调度程序! I ran my daily schedule 600 times consecutively resulting on 600 email being sent to my clients.
我连续600次运行每日计划,导致600封电子邮件发送给我的客户。 Don't use!!!
不要用!!!
Use the Azure Task Scheduler . 使用Azure任务计划程序 。 Nice tutorial by Scott Gu here .
由Scott辜霓侧教程这里 。
Specifically, I would look at the Storage Queue action type - just register for queue events in your Worker Role. 具体来说,我将查看存储队列操作类型 - 只需在您的工作者角色中注册队列事件。
(Note that this service may cost money if you want to schedule tasks more frequently than every hour.) (请注意,如果您希望比每小时更频繁地安排任务,则此服务可能会花钱。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.