Does each azure instance run cron? - c#

I am developing an ASP.NET application, which will be uploaded on Azure. If I have multiple instances on Azure and I want to run a cron job that will be necessary for my application. Then, I just want to confirm if that cron job will be run only one time or each instance will run that cron by itself?
For example: If I have 4 instances of cloud service on Azure and my application runs a cron job every day at 11:00 PM. So, I just want to confirm if that cron will be run only one time or each instance will run that cron on its own (i.e. cron will be run 4 times or we can say one time by each instance)?
Please suggest.

So far I've found 3 ways to do cron jobs BUT they all require some level of managing the multiple instances possibly running the tasks.
The choices I've used so far:
Windows Task Scheduler - create a startup script that adds the user and task the schedules it. More information here: Running Azure startup tasks as a real user and here: Building a Task Scheduler in Windows Azure
Using Quartz.Net - this I started with, but then moved to the windows task scheduler, but it may work for you since you can customize stuff easier. More information here: Using Quartz.net to Schedule Jobs in Windows.Azure Worker Roles
Using the new job scheduler in Mobile Services. I have not used this one, but when I read this blog: Job Scheduling in Windows Azure late last year I put it on my mental list to look at next time I need a job scheduler. It's still a little new, but it also may help you.

In your example, all 4 instances will try and run the cron job. If you would want to have only one instance run the job, you would need to implement some kind of "locking" mechanism. What normally folks do is that each instance will try and acquire a 1 minute lease on the same blob. Only one instance will be successful in acquiring the lease. You can put the logic that only the instance which is able to acquire the lease is executing that cron job.

As other users said, there are many ways to do that. I add a couple of suggestions. Have you developed a Cloude service web role or a Web site. If the former, the easiest way is to create also 1 worker role (only one) and run tasks from there. It the latter, you need an external "trigger": you can use Scheduler (by Aditi). You can get it from the Azure store (there is a free flan).

If a role (Web/Worker/VM role) has multiple instances, it means that you got the same account of Windows Server VMs (the version depends which guest OS version you configured). Each VM will run exact the same code of your application (like a ASP.NET Web Application), all requests to this role will be load-balanced and one of the VMs will sever the request based on round-robin policy.
So if your cron job only needs to be run only once, as Guarav said, you need some lock mechanism to let one of your role instances run it.

Related

Embedding Console application and scheduling in Azure

I have a console application that scrapes from different sites and update data in SQL Server, currently, it works on a physical server and is scheduled to run once a day.
We want to start using existing functions in Azure without the need for VM setup.
We thought about using WebJobs but we wonder if this solution can be effective for us.
(We do it without using selenium, without using any web browser)
You have some options.
Web Job
The name web job does not imply it must be a process with a frontend. So yes, it can be a console app (.exe) that is scheduled once a day. You pay for the underlying provisioned App Service Plan.
Azure Function
You can create a timer triggered azure function that will run some code. If the maximum processing time is 10 minutes you can use the pay-per-use model in which you only pay for the resources while the function is running.
If you cannot refactor it or split up into multiple functions each taking max. 10 minutes you have to provision an App Service Plan just like using a Web Job.
Azure Container Instances
If you can put your console app into a container you can use Azure Container Instances to spin up a container once a day and tear it down once the job is done. You will only pay for the resources used when the container is up and running.
My opinion
Since you have a job running once a day for approximately one hour I'd say go for Azure Container Instances as you will only pay for the time the container (job) is running.
If the process would take less then 10 minutes I would personally go for Azure Functions hosted on a consumption plan (pay-per-use model) as it is the cheapest option I know.

Preforming scheduled tasks in C# .NET MVC Application

I'm trying to implement a logic for my .NET MVC application where I would trigger scheduled tasks in my application on a following basis:
First scheduled task to run from 00 am to 1 am in the morning
Second scheduled task to run from 1:10 am to 08:00 am in the morning
Third scheduled task to run from 8:15 am to 11:15 pm every 1 hour
This would repeat every day 365 days a year...
Which mechanism in .NET or external library could I use to implement this the easiest way with little code?
I was trying to implement it with Quartz.NET library but I didn't like it at all, I had two scheduled tasks running where one would never run at all for the scheduled time...
You don't want to be performing long running and recurring background tasks in your web application for multiple reasons. You may go through the following blog post which outlines them very well.
I would recommend you moving this logic into a separate Windows Service or even a Console Application whose runs could be scheduled with the Windows Task Scheduler at the desired intervals.
The benefit of this is that you will be off-loading your web application from doing long running tasks which will be consuming precious resources from your webserver. Of course both the webserver and the worker service could share a common centralized datastore or messaging layer in order to communicate if necessary.
For scheduling tasks in ASP.NET MVC you have several options:
3rd party libraries like FluentScheduler, HangFire or Quartz.net.
Since .Net 4.5.2 there is QueueBackgroundWorkItem, a native way to schedule work in the background. Here is a guide on how to use it with ASP.NET MVC.
Azure services for background workloads and scheduled tasks. There are a couple of those aimed at different tasks, here is an overview of them.
Be careful if you try to implement a custom solution yourself, as any exception in a thread not associated with a request would halt the entire process.
Visual Cron is a decent task scheduler you can use. We use it, calling service methods on our asp.net / mvc / webapi servers.
http://www.visualcron.com/
There are some ways that you can create a scheduled task.
1- Create a WebApi and call it by local console or windows service application in period of time.
2- Using 3rd party libraries like FluentScheduler, HangFire or Quartz.net as Andreas said. it has its own side effects though.
3- If Sql server is used in your application, you can create a procedure for your task and put it on a job in Sql server. you can even use C# for your function if CLR is enabled in Sql Server,
If I want to choose, I will choose Sql Server jobs.
I had similar problem with my ASP.NET MVC App, I Wanted to do some works in special times and finally I could find a way.
My solution was Plesk scheduels task feature, as where the most windows servers use plesk control panel You can publish your app on a windows server with plesk CP and create a new actionresualt in your MVC App and write Your codes that want to execute them in special times.
By plesk scheduels tasks create a new task and call your Url with a cron that You made it, You can make many crons.
If I Could not explain It very well You can search about Plesk Schedueled Tasks on the Internet, You can find many good answers.

Dynamically Creating Scheduled Tasks In An ASP.NET Website

I am in need of some advice on the best approach to dynamically creating some form of scheduled task at web application level. The scheduled task will be pulling information from an external API endpoint to store within a database at regular intervals until a specific end time.
Normally, I would create a Windows Service that will carry out this job for me. Unfortunately, the site administrator could create multiple tasks to query different criteria from the external API. So there could be multiple tasks running.
I have been working on implementing a background worker thread that runs on a timer. Would this be the best approach? Or would using something like Hangfire or Quartz.net be a better solution to ensure my background process is constantly running?
I don't know if it's feasible inside a web application to be ensure a task can constantly be run when required for a specific duration of time.
I tried to use Hangfire and Quartz.Net and in my opinion both of them are very good for scheduling task. Hangfire has a good interface for managing scheduled tasks, but as I encountered some problems after publishing the application on IIS, I switched to use Quartz.Net which works perfectly with Keep Alive Service For IIS 6.0/7.5. So, for using Quartz.Net you might have a look at Scheduled Tasks In ASP.NET With Quartz.Net. On the other hand, in order to make your published application to be alive after application pool recycling, IIS/Application restarting, etc. just install Keep Alive Service For IIS 6.0/7.5 on the server to which you publish your application. For detailed information regarding to these problems have a look at my answers on the pages below:
Quartz.net scheduler doesn't fire jobs/triggers once deployed
execute a schedule with quartz in visual start now with interval 24 hours
Hope this helps...

creating azure worker rules from a vm

is it possible to start Worker Role Instances dynamically from a c# application running on azure windows vm?
in azure i have a Medium virtual machine, on it there is a c# console application that runs automatically on 11:00PM daily and it keeps processing data until about 7:00AM, my data is getting bigger and thus needs more time to be processed and i need to finish processing all data before 5:00AM.
is it possible to use Worker rule to run an instance of the application an pass it a part of the data to process?
note that my process makes http requests to external websites and the processed data gets written to a mongodb.
i am not sure where to start, and i am not sure if using worker rules is better than creating couple vms.
in general how would you solve this problem with the tools available on azure?
Is it possible to start Worker Role Instances dynamically from a c#
application running on azure windows vm?
Absolutely Yes. In order to do so, you would need to consume Service Management API. You could either write code yourself to consume this API or there's a Windows Azure Management Library available to do so which you can install from Nuget. To learn more about this API, you may find this blog post useful: http://www.bradygaster.com/post/getting-started-with-the-windows-azure-management-libraries.
Generally speaking Worker Roles are equivalent to Windows Services in the sense that both are used to perform background tasks. Since you're performing background tasks through your VM, I can't see any reason why you can't do the same though a Worker Role instance. My recommendation would be to go through tutorials available online or Windows Azure Platform Training Kit to become familiar with Worker Role concepts and how you could make use of them in your project.
For your specific scenario you may want to look at the auto scale rules that are now available; In the configuration for the worker role, in the Azure Management Console, you can specify, for example, that you want at least two workers running between certain times each day.
The Service Management API gives you a lot more control, but the auto scale is quick and easy to start with.
Incidentally, if the work your worker has to do can be divided into atomic chunks, then you may want to use a storage queue to write all the tasks to and then have the worker role pull tasks off that queue. You can then configure the autoscale to monitor the length of the queue and start and stop workers as required.

Windows Azure - background/scheduled tasks?

Previously, there was worker role in Azure, now I can't see one - so what to use for background/scheduled tasks, like maintainance, email sending, etc, should I create virtual machine and create windows services there or is there easier way?
The definitive (current) guide to this FAQ is Guarav's Building a Simple Task Scheduler in Windows Azure — which, as it turns out, is not that simple, and it is not really suited to Azure websites (but rather roles).
The simplest solution is to create RESTful (ish) routes (controllers, etc) using something like the MVC Web API and get a cron job scheduler to kick them off. Recently I have been using the Aditi cloud scheduler which kicks of those jobs for you, and is free (5000 calls per month) in the marketplace.
There is this new scheduler http://www.windowsazure.com/en-us/services/scheduler/
Windows Azure Scheduler allows you to invoke actions—such as calling HTTP/S endpoints or posting a message to a storage queue—on any schedule. With Scheduler, you create jobs in the cloud that reliably call services both inside and outside of Windows Azure and run those jobs on demand, on a regularly recurring schedule, or designate them for a future date. This service is currently available as a standalone API.
If you are using Azure Web Sites (and they are very good) then there is the new WebJobs feature that lets you poke a http(s) endpoint or run scripts on a schedule.
Web and Worker Roles are part of the Cloud Service model, and both exist and haven't gone anywhere.
As stated in the comments to your question, the portal does not facilitate construction of these roles; this is something you'd create, either through Visual Studio, Eclipse (worker role), or PowerShell.
And you don't need a worker role for background tasks. As mentioned in dozens of other answers, worker and web roles are templates for Windows Server virtual machines. Since the VMs are stateless and restart each time from the same baseline, the template shapes what gets installed at startup.
You can run background tasks as a thread in either a web role or worker role. So if you wanted to, you could run all your background tasks within the same web role instances as your web site.
I recommend working through some of the basic examples in the Azure Training Kit, which walk through creating different roles from Visual Studio.

Categories