Skip to main content

CRON job in Windows Azure - Scheduler

Yesterday I realized that I have to run a task on Windows Azure every 30 minutes. The job is pretty simple; it has to retrieve new data from an endpoint. This is a perfect task for a CRON-base job.
The only problem with the current version of Windows Azure is that it doesn't have support for CRON jobs. People might say that we can have a timer and every 30 minutes we could run the given task. This is a good solution and it will work perfectly, but I wanted something different.
I didn’t want to create my own CRON-base job. I wanted something build-in in the system. I started to look around and I found an add-on for this. So, Windows Azure offers a Store for any company that want to offer different add-ons for Windows Azure. These add-ons can be very easily installed. If they are not free, the payment method is quite simple. Each month the Azure subscription will contain the cost of these add-ons. From my perspective this is a pretty simple and clean mechanism of payment.
Under the store I discovered the “Scheduler” add-on, offered by ADITI Cloud Services. This add-on gives us the possibility to create different jobs that are called at a specific time interval. We don’t need a timer, another machine or something similar.
How it works? It is based on normal HTTP requests that will be made automatically to your machine. Their servers will call your machines when a job needs to be executed. In this moment, they support only HTTP, without any kind of authentication. I expect in the near future to have support for authentication and HTTPS.
In this moment the service is free and you can execute around 5000 jobs per month for free. This mean that you can trigger a job every ~9 minutes.
Let’s see some code now. After you install the add-on from the Windows Azure Store, the “Scheduler” will generate a tenant id and a secret key. This will be used from your application when you will need to configure the jobs.
After this step, we need to install a NuGet package called “Aditi.Scheduler”. This will contain all the components that we need to be able to configure and use this add-on.
In our application we have to create an instance of “ScheduledTasks”. Using this instance we can create, modify or delete jobs.
ScheduledTasks scheduledTasks = new ScheduledTasks([tenantId], [secretKey]);

ScheduledTasks task = new TaskModel
    {
        Name = "MyFooJob",
        JobType = JobType.Webhook,
        CronExpression = "0 0/5 * 1/1 * ? *",
        Params = new Dictionary<string, object>
        {
            {"url", "http://foo.com/service1"}
        }
    };

scheduledTasks.CreateTask(task);
Each job can be changed, deleted and so on. What we should remember is to delete a job when we stop using it. Even if our solution will not be deployed anymore, the job will still be trigged each time. Because this solution is based on HTTP request, we need to expose a REST service from where we want to trigger our job.
A cool thing that we already have is the different type of jobs. We don’t have only web jobs but also jobs that use Service Bus Queue or Azure Queue. In this way we can listen to an Azure Queue from our application and our job will be triggered when a specific message is found in the queue. This feature can be used on worker roles that don’t have a HTTP endpoint exposed.
In conclusion I could say that this is a pretty interesting add-on that has a lot of potential.

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