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
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.
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
Post a Comment