Skip to main content

Windows Azure - CRON jobs on Web Roles

In one of the current project we need to support jobs on SQL Azure. If you already tried to create a job in SQL Azure maybe you noticed that there is no support for that. I mean that the job concept don’t exist on SQL Azure – yet, there are some rumors that in the near future we will have something like this.
Our client needs this functionality now and we cannot wait one more week. Because of this we had to find a solution to make our client happy.
On the store of Windows Azure we can find an application called Scheduler that can help us to implement something like this (CRON jobs). You can find more about this app on the following link: http://vunvulearadu.blogspot.ro/2013/02/cron-job-in-windows-azure-scheduler.html
In our case we cannot use this application because we have custom security configuration on the Azure machines. Because of this we cannot expose an endpoint to a 3rd part. The solution that we end up is a temporary solution that will be used until we will be able to run jobs on SQL Azure.
Having a web application, hosted on web roles we decided to incorporate this functionality in the current web roles. We don’t want to have a dedicated worker role for this – is more expensive, is more reliable but for our case we can live with this task on web roles also.
Solution
On the web roles we have an action that trigger the stored procedure from SQL Azure. Before calling the stored procedure, our method will check a table from Azure Storage to see when was the last time when the CRON “job” ran and if we need to run it again. Each time when the store procedure is called the last run time field from Azure Storage table is updated. This action is triggered on Application_Start() method on Global.asax and therefore is called each time the application starts.
In the same time we started a timer that is set to do the same thing but at a specific time interval – in our case every 2 weeks.

Flow
1.      <!--[endif]-->Application Start
<!--[if !supportLists]-->2.      <!--[endif]-->Try to run the job
<!--[if !supportLists]-->a.      <!--[endif]-->Check when was the last time when job ran
<!--[if !supportLists]-->                                                    i.     <!--[endif]-->We should run the job again
<!--[if !supportLists]-->1.      <!--[endif]-->Update the field that specifies the last time when the job ran
<!--[if !supportLists]-->2.      <!--[endif]-->Run the store procedure from SQL Azure
<!--[if !supportLists]-->                                                   ii.     <!--[endif]-->We don’t need to run the job
<!--[if !supportLists]-->1.      <!--[endif]-->Nothing happens
<!--[if !supportLists]-->3.      <!--[endif]-->Start the timer
<!--[if !supportLists]-->a.      <!--[endif]-->Run the following action at a specific time interval
<!--[if !supportLists]-->                                                    i.     <!--[endif]-->Check when was the last time when the job ran
<!--[if !supportLists]-->1.      <!--[endif]-->We should run the job again
<!--[if !supportLists]-->a.      <!--[endif]-->Update the field that specifies the last time when the job ran
<!--[if !supportLists]-->b.      <!--[endif]-->Run the store procedure from SQL Azure
<!--[if !supportLists]-->2.      <!--[endif]-->We don’t need to run the job

<!--[if !supportLists]-->a.      <!--[endif]-->Nothing happens

Why we need to run this code every time when the application starts?
Because even if we can configure the idle time of the application pool on ISS, we can end up in having our application on idle or the IIS resets. Because of this, we need to try to run the CRON “job” when the application starts.
Why we need a table from Azure Storage?
We don’t want to run the store procedure over and over again. Because of this we need to know when was the last time when the store procedure ran with success. Of course we could use SQL for this and log information in SQL, but we didn’t want to depend on SQL.
Also, we can have more than one web roles. In this case we don’t want to trigger this job multiple times – because of this we need to know what was the last time when the CRON “job” ran.
Why we don’t store the status of the store procedure on the table from Azure Storage?
In our case we had to make some cleaning tasks on SQL. Because of this we could afford to run the store procedure more then one time. We know about this risk and we accept this. If we would have had a job that should have run only once a week then we should have add the status of the CRON "job" to our table from Azure Storage.


Conclusion
This is a pretty simple solution that can be used with success when we need to run CRON “jobs” on Windows Azure. Based on the requirements we can have more complicated solutions. One thing that we should remember is to keep it simple. We don’t want to complicated things just for fun.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

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 http://go.microsoft.com/fwlink/?LinkId=260882 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.51 EF 6.0.2 VS2013 It see