Skip to main content

Different methods of creating CRON jobs on Windows Azure

In the last couple of weeks we saw a lot of new features related to Windows Azure. In this moment if want to run a task or a job on Azure we had a lot of possibilities starting from Worker Roles and ending with Web Jobs or Schedulers on Mobile Services.
In this post we will look over a part of the services that are available on Windows Azure to run different jobs. For each of them we will try to identify the best case when we can use them.

We will start with Worker Roles. Theoretically behind the Worker Role we have a VM that gives to us the full power of the CPU and Memory that is available on it. On such machines we can run the most complex jobs that we can have. On such roles we can implement any kind of jobs, without any kind of limitations. Because the VM is managed by Azure platform we don’t need to be worried about OS, security updates and so on.
I would use this kind of machines if I would need to process an audio stream, generate an ISO with a custom content or doing some heavy tasks.
Another place where we have the ability to run jobs is on Hadoop (HDInsight). On this service we can run jobs that are consuming a lot of resources to process different content. For example if we have a data source (like a job) from where we need to extract some statistical information. This jobs perfect option for us when we have a lot of input data and based on it we need to extract reports or statistical information.
I would use Hadoop for jobs that need to process large amounts of logs data to extract different reports.
In Mobile Services we have the ability to schedule and run different jobs. They are very similar with CRON jobs and can be written in JavaScript. The jobs that can be run from Mobile Services are very simple and are oriented to the mobile service and mobile applications. This means that the job should be very simple and would process the data only for a user or for a limited number of users. For example this jobs are great if you need to update different fields from customers table or clean some tables.
But you shouldn't use this job to process pictures or make long call requests to different jobs (for example call 10-20 different external services and send clients data). You should use the jobs from Mobil Services at your application level. For more complicated jobs, you should try to use other solutions.
Azure Scheduler is something like a scheduler. Basically, this is the scheduler mechanism that gives you the ability to call a custom URL at a specific time interval. You will not be able to define the action that needs to be executed, only the endpoint that needs to be called. The endpoint that can be called can be an HTTP, HTTPs endpoint or a queue. For queues you have the ability to specify the body content of the message that is added to the queue.
This can be used with success in combination with Worker Roles (using queues) or WebJobs. For example if we have a cleaning task that needs to be executed every 4 days, we can define an Azure Scheduler that will call a specific URL of our site every 4 days. The URL will trigger our custom clean action.
The last available mechanism to define and run a job or a task is by Web Jobs. Using Web Jobs we have the ability to run any task on the web instances where we run our application. It is important to know that the Web Jobs will run on the same instances that we host our application. Because of this we need to be aware of what kind of tasks we want to run using Web Jobs. We don’t want to have tasks that consume a lot of resources from our instances and put our web application down. The good part is that the load balancer from Windows Azure will select the instances that has the lowest load. Like Mobile Services “CRON’ jobs, Web Jobs are recommended to use for small and simple jobs. For complex jobs we can use with success worker roles of HDInsight.

In this post we saw different ways to define a job/task. We don’t have a best solution, based on our needs we need to know to select the solution that is more suitable for us.


Popular posts from this blog

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder.
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token.
The following code show to you how you could write the encode and decode methods.

Entity Framework (EF) TransactionScope vs Database.BeginTransaction

In today blog post we will talk a little about a new feature that is available on EF6+ related to Transactions.
Until now, when we had to use transaction we used ‘TransactionScope’. It works great and I would say that is something that is now in our blood.
using (var scope = new TransactionScope(TransactionScopeOption.Required)) { using (SqlConnection conn = new SqlConnection("...")) { conn.Open(); SqlCommand sqlCommand = new SqlCommand(); sqlCommand.Connection = conn; sqlCommand.CommandText = ... sqlCommand.ExecuteNonQuery(); ... } scope.Complete(); } Starting with EF6.0 we have a new way to work with transactions. The new approach is based on Database.BeginTransaction(), Database.Rollback(), Database.Commit(). Yes, no more TransactionScope.
In the followi…