简体   繁体   English

Azure网站 - 计划任务

[英]Azure Web Sites - scheduled tasks

I am developing a web site which I plan to host on Windows Azure. 我正在开发一个我打算在Windows Azure上托管的网站。 The site will need to run some daily/weekly scheduled jobs, synchronizing to various 3rd party data sources, sending user notifications, etc. It should also be able to run on-demand async tasks such as sending emails to users etc. 该站点需要运行一些每日/每周预定作业,同步到各种第三方数据源,发送用户通知等。它还应该能够运行按需异步任务,例如向用户发送电子邮件等。

My initial thought was to host this using Azure Cloud Services, with one web role running MVC 4 and one worker role both taking care of scheduled tasks, and pulling async tasks (sending emails etc) out of queue storage. 我最初的想法是使用Azure云服务来托管它,一个运行MVC 4的Web角色和一个辅助角色都负责计划任务,并将异步任务(发送电子邮件等)从队列存储中拉出来。 However, this is going to cost me, seeing how I need to pay double for compute hours. 然而,这将花费我,看看我需要为计算时间支付双倍的费用。

The project might justify this cost in the future, but before business picks up I'd really prefer a cheaper alternative. 该项目可能在未来证明这一成本是合理的,但在业务开始之前,我真的更喜欢更便宜的替代品。 Therefore I'm looking into Azure Web Sites. 因此,我正在研究Azure网站。

I could cut the cost in half by using Azure Web Sites for the MVC site and having a worker role running the taks, but I'd love to hear about other alternatives, besides the obvious option to manually trigger them from my admin module. 我可以通过将Azure网站用于MVC站点并让工作人员角色运行这些成本来减少一半的成本,但我很想知道其他替代方案,除了从我的管理模块手动触发它们的明显选项。

Also, can I connect the site to my domain and use ssl for free using Azure Web Sites? 此外,我可以将网站连接到我的域并使用Azure网站免费使用ssl吗?

It looks like Azure finally has proper scheduling built in - see the Azure Scheduler documentation 看起来Azure最终内置了适当的调度 - 请参阅Azure Scheduler文档

Whilst it's in preview and you'll most likely have to use the REST API for now, I'm sure it won't be long before the functionality is available in the management portal. 虽然它处于预览状态,您现在很可能不得不使用REST API,但我确信它不会在管理门户中提供功能之前很久。 I've been waiting ages for this! 我一直在等待这个年龄!

If you're comfortable writing code in node.js, do take a look at Windows Azure Mobile Services . 如果您愿意在node.js中编写代码,请查看Windows Azure Mobile Services It has the capability to define and execute scheduled tasks. 它具有定义和执行计划任务的能力。 More about this can be found here: http://www.windowsazure.com/en-us/develop/mobile/tutorials/schedule-backend-tasks/ . 有关这方面的更多信息,请访问: http//www.windowsazure.com/en-us/develop/mobile/tutorials/schedule-backend-tasks/

Another alternative could be to use Aditi's Scheduler Service: http://www.aditicloud.com/ . 另一种选择可能是使用Aditi的调度服务: http//www.aditicloud.com/

Yet another alternative would be to write your own scheduler and hosting that solution in Azure Websites. 另一种方法是编写自己的调度程序并在Azure网站中托管该解决方案。 I would recommend using Quartz.net scheduling library. 我建议使用Quartz.net调度库。 It's free, open source and used by many folks. 它是免费的,开源的,并被许多人使用。

I still think going Worker Role route for job processing is a viable solution. 我仍然认为工作处理的工作者角色路线是一个可行的解决方案。 What you could do is host the front-end infrastructure in a Windows Azure Website and have it communicate to the worker role via Windows Azure queues. 您可以做的是在Windows Azure网站中托管前端基础架构,并通过Windows Azure队列与工作者角色进行通信。 Assuming you host 2 instances of worker roles in Extra Small VM size, it's going to cost you about $30.00 per month ($0.02 x 2 x 750 hours). 假设您在Extra Small VM大小中托管2个工作者角色实例,那么每月花费约30.00美元($ 0.02 x 2 x 750小时)。 I wrote a blog post on building your own task scheduler and hosting it in a worker role not too long ago. 我写了一篇关于构建自己的任务调度程序的博客文章,并在不久前将其托管在一个辅助角色中。 You can read that post here: http://gauravmantri.com/2013/01/23/building-a-simple-task-scheduler-in-windows-azure/ 你可以在这里阅读那篇文章: http//gauravmantri.com/2013/01/23/building-a-simple-task-scheduler-in-windows-azure/

Also, can I connect the site to my domain and use ssl for free using Azure Web Sites? 此外,我可以将网站连接到我的域并使用Azure网站免费使用ssl吗?

I don't think so. 我不这么认为。 SSL is not free with Windows Azure Websites. Windows Azure网站不提供SSL。 Take a look at SSL pricing here: http://www.windowsazure.com/en-us/pricing/details/web-sites/ . 请在此处查看SSL定价: http//www.windowsazure.com/en-us/pricing/details/web-sites/

You can use the Timer class to run the scheduler inside your Azure Web Site project. 您可以使用Timer类在Azure Web站点项目中运行调度程序。 This way, you can have everything encapsulated inside your ASP.NET MVC project. 这样,您可以将所有内容封装在ASP.NET MVC项目中。

Info about Timer class: http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx 关于Timer类的信息: http//msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx

I tested this in a MVC project on Azure Web Site and can confirm that it works. 我在Azure网站上的MVC项目中对此进行了测试,并确认其有效。 To get this set up, start the timer inside Application_Start() of Global.asax.cs: 要进行此设置,请在Global.asax.cs的Application_Start()内启动计时器:

private Timer _timer;
protected void Application_Start()
{
    _timer = new Timer(1 * 60 * 1000); // 1 minute
    _timer.Elapsed += (sender, e) => ScheduledTask.Process();
    _timer.Enabled = true;

    // other code goes here
}

In my example, the timer calls ScheduledTask.Process() every minute. 在我的示例中,计时器每分钟调用ScheduledTask.Process()。 Here is what my ScheduledTask class looks like: 这是我的ScheduledTask类的样子:

public class ScheduledTask {

    public int Id { get; set; }
    public DateTime Time { get; set; }

    public static void Process() {

        using (var db = new ApplicationDbContext()) {
            db.ScheduledTasks.Add(new ScheduledTask {
                Time = DateTime.Now
            });
            db.SaveChanges();
        }

    }

}

In Process(), you can do whatever you want, but I'm just creating an entry in the database to test whether this works. 在Process()中,你可以做任何你想做的事情,但我只是在数据库中创建一个条目来测试它是否有效。 I'll keep this task working for about a day so and you can see the results here: 我会让这项任务工作大约一天,所以你可以在这里看到结果:

http://contactmanager1.azurewebsites.net/ScheduledTask http://contactmanager1.azurewebsites.net/ScheduledTask

Update 更新

It turns out that this isn't the best way to set up scheduled tasks, because this timer dies when the application pool times out (due to low activity on the site). 事实证明,这不是设置计划任务的最佳方式,因为此计时器在应用程序池超时时死亡(由于站点上的活动较少)。 The timer automatically starts up again when application pool starts up again, but for low volume sites, this isn't a reliable solution. 当应用程序池再次启动时,计时器会自动重新启动,但对于低容量站点,这不是一个可靠的解决方案。

The article The Dangers of Implementing Recurring Background Tasks In ASP.NET has a very interesting idea to have background task running in your Asp.Net application without risk that the AppDomain shutdown stop the task. 文章实现重复背景任务的危险在ASP.NET中有一个非常有趣的想法,即在您的Asp.Net应用程序中运行后台任务,而没有AppDomain关闭停止任务的风险。

If you don´t want pay for an Azure worker role, you can try it. 如果您不想为Azure辅助角色付费,可以尝试一下。

I just need to say at first that you are incorrect about needing to paying double :) 我只需要先说你不需要支付双倍:)

A "WebRole" in Azure PaaS (platform-as-a-service) is just a WorkerRole+IIS. Azure PaaS(平台即服务)中的“WebRole”只是一个WorkerRole + IIS。

So, since your logic that needs to be put in a timer is very minimal, and probably won't be taking up a lot of resources, you can just start your background worker thread in the OnStart of the "WebRole.cs" file that is inside of your web app. 因此,由于您需要放入计时器的逻辑非常少,并且可能不会占用大量资源,因此您只需在“WebRole.cs”文件的OnStart中启动后台工作线程即可在你的网络应用程序内。

This would result in both your web application, as well as your small worker task being hosted on the same Virtual Machine - so you only pay for one. 这将导致您的Web应用程序以及您的小型工作人员任务托管在同一个虚拟机上 - 因此您只需支付一个。

As a side point - if you wanted 10 "background" tasks, but they don't do heavy work and don't need to be scaled independently, then you could also just host or run all of them from a single "WorkerRole" VM as well. 作为一个侧面点 - 如果你想要10个“后台”任务,但是他们不做繁重的工作并且不需要独立扩展,那么你也可以从一个“WorkerRole”VM托管或运行所有这些任务同样。

namespace MvcWebRole1
{
    public class WebRole : RoleEntryPoint
    {
        public override bool OnStart()
        {
            // Start my background thread processing task.
            MyBackgroundTaskThing.Start();

            return base.OnStart();
        }
    }
}

Hi i'm adding thoses link because Microsoft announce they was retire the scheduled part on Azure. 嗨,我正在添加链接,因为微软宣布他们已退出Azure上的预定部分。

https://azure.microsoft.com/en-us/updates/azure-scheduler-will-retire-on-september-30-2019/ https://azure.microsoft.com/en-us/updates/azure-scheduler-will-retire-on-september-30-2019/

https://support.microsoft.com/en-us/help/4316957/products-reaching-end-of-support-for-2019 https://support.microsoft.com/en-us/help/4316957/products-reaching-end-of-support-for-2019

To perform new scheduled task (or program at back end) you have too explore the Logic Apps. 要执行新的计划任务(或后端程序),您也要探索逻辑应用程序。

https://azure.microsoft.com/fr-fr/services/logic-apps/ https://azure.microsoft.com/fr-fr/services/logic-apps/

https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-examples-and-scenarios https://docs.microsoft.com/en-us/azure/logic-apps/logic-apps-examples-and-scenarios

I'm look to show a fully sample code on blog soon. 我希望尽快在博客上展示完整的示例代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM