سؤال

I am developing a web site which I plan to host on 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. 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.

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.

Also, can I connect the site to my domain and use ssl for free using Azure Web Sites?

هل كانت مفيدة؟

المحلول

It looks like Azure finally has proper scheduling built in - see the Azure Scheduler documentation

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. 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. 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/.

Another alternative could be to use Aditi's Scheduler Service: http://www.aditicloud.com/.

Yet another alternative would be to write your own scheduler and hosting that solution in Azure Websites. I would recommend using Quartz.net scheduling library. 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. 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). 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/

Also, can I connect the site to my domain and use ssl for free using Azure Web Sites?

I don't think so. SSL is not free with Windows Azure Websites. Take a look at SSL pricing here: 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. This way, you can have everything encapsulated inside your ASP.NET MVC project.

Info about Timer class: 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. To get this set up, start the timer inside Application_Start() of Global.asax.cs:

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. Here is what my ScheduledTask class looks like:

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. I'll keep this task working for about a day so and you can see the results here:

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.

Azure Websites now supports Azure WebJobs - http://www.hanselman.com/blog/IntroducingWindowsAzureWebJobs.aspx

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.

If you don´t want pay for an Azure worker role, you can try it.

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.

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.

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.

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.

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.

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

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://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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top