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

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...