creating azure worker rules from a vm - c#

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.

Related

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

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.

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.

Start a process on Azure remotely

I would like to be able to start a number of concurrent processes (command line programs) on Azure remotely. I need to be able to make a call from a C# program to start these processes off. What would be the best approach to this?
Set up an Azure VM with IIS
Install the various .exes that I want to run.
Write and install a web service on the box that can take parameters and start the processes on the server.
Is there an easier way to do this using Azure though? I am not familiar with Azure VMs or worker roles etc.
Thanks!
You could make use of Azure Worker Roles and Windows Azure Storage for that purpose. What you could do is either package the executables along with the worker role package so that they are always available in your role or you could save them in blob storage so that you can download on demand into your worker role. From your calling application, you can put messages in a queue which will be constantly polled by your worker role. The message will contain enough information (for example exe to invoke etc.). Once the message is received, the worker role will execute the executable.
Hope this helps.

Spawning and executing a Worker process in Azure

The google has really failed me on this one. I am new to Azure and am only intermediate at .NET
I have an Azure solution going and I've written some code in a Web Role which runs great. What I would like to do now is move some of this code into an Azure Worker, which will be initialized by a controller function in the Web Role
What on earth do I need to do to get this going locally? I have created the Worker project within the SLN. I just need to know how to fire it up and run it.
I think part of my problem is I am assuming these workers behave like Heroku workers... is this the case? Because what I need is something like a queue system (a bunch of "worker tasks" in one big queue).
A lot of the links I've found for tutorials seem to tap dance around how to actually initialize the process from a Web Role.
Workers in Windows Azure are not tasks; they're entire VMs. To make your life easier, memorize this little detail: Web Role instances are Windows Server 2008 with IIS running, and Worker Roles are the same thing but with IIS disabled.
When you added that worker role to your project, you actually now have a new set of virtual machines running (at least one, depending on the instance count you set). These VMs have their own OnStart() and Run() methods you can put code into, for bootstrapping purposes.
If you grab the Windows Azure training kit, you'll see a few labs that show how to communicate between your various role instances (a common pattern being the use of Windows Azure queues). There's a good example of background processes with the Guestbook hands-on lab (the very first lab).
More info on this, as I've gotten it going now..
If you're coming from a Heroku background, then an Azure Worker is more or less the function in Rails that you'd actually execute with the queue. Unlike Heroku queued operations, an Azure Worker just runs endlessly and keeps polling for new stuff to do... hence the templated sleep(10000) in the Run() function.
The most conventional way I've found to make a Web and Worker talk to each other is by queue messages via Azure ServiceBus which is currently NOT emulated, meaning you need a functioning Azure account to make this work, and it will work even if you are running locally. You just need internet access.
A ServiceBus message can pass an entire object over to the Worker (so long as the Worker proj has the right dependencies in it), so that's kind of nice.
I think you're just having trouble starting the azure emulator along with your worker/web roles? Just set the azure configuration project as the start up project and run that. It'll boot up the emulator along with all your roles.

Categories