Serverless Azure Function – Run a cron job using time trigger

This post is about using serverless Azure function for running a batch job
or cron job. There are many instances where in, we can execute the cron job using Azure Function using time trigger.

Serverless Azure Function allows us to schedule a custom block of code to execute on time the client wanted and gain immediate visibility of logging in the Azure portal. Since this is serverless, we need not bother about setting up infrastructure. This does not need to have Virtual machines to setup or IIS to be configured in order to host any service.

We will create a demo project which is going to run as a batch job and fetch the BBC news rss feed for asia region. And we are going to send an email using SMTP settings for gmail. So every 6 hours this cron job is going to sniff bbc news reader rss feed and email me the top headings to my gmail inbox.

Before we start creating new project and proceed, please ensure you have installed the Azure Development workload for visual studio 2017.

Azure function gives us multiple triggers types, which we can use as a template and use.

1.HTTPTrigger – To Trigger the execution of your code by using an HTTP request.
2. TimerTrigger – To Execute cleanup or other batch tasks on a predefined schedule.
3. CosmosDBTrigger – To Process Azure Cosmos DB documents when they are added or updated in collections in a NoSQL database.
4. BlobTrigger – To Process Azure Storage blobs when they are added to containers. You might use this function for image resizing.
5. QueueTrigger – To Respond to messages as they arrive in an Azure Storage queue.

We need to Add System.ServiceModel in references for using SyndicationFeed and it will fetch and read rss feeds.

PM> Install-Package System.ServiceModel.Syndication

We would like to run this Azure function in every 6 hours, so 4 times in a day. We would select time trigger as Azure function type.

For testing we will change the time trigger to 20 seconds, in the below image we see the the cron job is being executed every 20 seconds.

Publish Azure Function to Azure Cloud

We will publish the azure function to cloud. We need to created a new profile in Azure for us to publish this Azure Time Trigger function.

We will name our Azure function as RSSFeedSniffer

After being successful published, it shall be redirected to
https://rssfeedsniffer.azurewebsites.net/

It can also be seen in Azure portal dashboard.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.