Embedding Console application and scheduling in Azure - c#

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.

Related

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.

Is there an alternative for Azure Cloud Services that can scale out faster?

We currently run our service as an Azure Cloud Service with a web role. It's rather convenient to deploy (from the .cspkg) and manage OS changes (the Guest OS images thing) and we even download rarely changing dependencies from blob storage and unpack them.
However scaling out is very slow - our code computes various metrics and at some moment it decides to scale out and issues a Change Deployment Configuration Management Service request. Then some magic happens inside Azure and something like 5-10 minutes later we have new service instances running our startup code - control finally gets into RoleEntryPoint.OnStart(). That's very slow for us - we'd like to have RoleEntryPoint.OnStart() invoked something like 5-10 seconds instead of 5-10 minutes after the scale-out request.
Is there any PaaS-style alternative to Cloud Services within Azure which is known to scale out faster?
The alternative would be App Service (web apps). You won't have the same control as with cloud services but they can still be customized pretty well, and they scale out (and up) really fast.

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.

Does each azure instance run cron?

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.

WCF VS Windows Console application

Environment: C#.NET VS 2012
We need to write an order delivery process. Basically it runs through the orders tables and and creates a file every night, that contains orders that are received on that day.
Traditionally we build this using Windows Console Application and a scheduled task wakes up this console application at every night (or every 6 hrs) to deliver the files
We are planning to re-write this console application. We are leaning towards both approaches i.e.
Approach 1: the scheduled task would run to deliver the order every night
Approach 2: ASP.NET web apps, that would also deliver the orders.
I am new to WCF, not yet tried it, is this a good situation to use WCF?
If so, can someone throw me some basic points how to implement this.
FYI: I have implemented another approach for some other client, where we have a ASMX web service that does this job, and the console application just calls the web service.
One disadvantages we have with this approach is, the file creation and delivery and everything is done through IIS and we prefer not to use IIS if it needs to be called from Windows Scheduler. This is for performance reasons.
Thanks
Suresh
Keep it simple. Run a console application as a scheduled task.
An IIS app (WCF service or WebApi) would only be useful if you get job requests, i.e. acting as a server.

Categories