Using Azure WebJobs to Automate Stuff

DZone 's Guide to

Using Azure WebJobs to Automate Stuff

Azure WebJobs, a piece of Azure App Service, allows you to run scheduled scripts. See how you can use it to scheduled and automate basic tasks.

· Cloud Zone ·
Free Resource

I keep hearing about Azure WebJobs but I have never used them. Time to change this.

WebJobs are a feature of Azure App Service that can run a script at a specific time. In my case, I would like to hit a specific URL of my website at the same time every day.

WebJobsTo the right, you can see an example of the WebJobs form on the Azure portal that you need to fill in.

You need to supply a name for your WebJob.

You need to upload the script that will run. In my case, I used a PowerShell script. My script basically just loads the URL specified.

$progressPreference = "silentlyContinue";
$result = Invoke-WebRequest -Uri ("https://www.google.com") -Method Get -UseBasicParsing;

"Type", in the above image, refers to whether your job to be triggered or to run continuously. I want it to be triggered.

The Triggers field refers to whether you want it to be scheduled or manual, something that you can run on an ad hoc basis. I, of course, want it scheduled.

If you are familiar with Linux CRON, then the next box will make sense to you. For everyone else, I will try and make sense of it. The box consists of six numbers that can either have a value or a *. The numbers correspond to the following: {second} {minute} {hour} {day} {month} {day of the week}.

An hourly job would be expressed as 0 0 * * * * — every day of the week, every month, every day, every hour, and only when the minute and second equals zero. For more help with this, check out the MSDN docs about it. I want to use 0 30 21 * * * to run daily at 9:30 P.M.

That’s it! Everything is set up, and now it's time to wait and see if it works.

Oh No!

It failed to run at the specified time.

The reason for this is that the scheduler requires the Always On feature to be turned on, which is not available in the free App Service. Before you reach for your wallets, I found a solution in this blog post that allows them to run on the free tier.

The thinking behind this solution is that you need to keep the website alive throughout the day, so Tom has created a script that does this. His script can be found on his blog or on his GitHub page.

Set this script up to run every 5 minutes (0 */5 * * * *) like the example above.

The next thing you need to do is create a custom connection string in the application Settings blade called SecretThing. Tom’s script references this to access the website and keep it alive. The password you need to put in SecretThing can be found in you publish profile (downloaded from the Overview blade in the Azure portal). For more details and a better explanation, check out Tom’s blog.

One last thing to mention about WebJobs is that you can see details about when they have run at https://[YourWebAppName].scm.azurewebsites.net/azurejobs/#/jobs. This can be a great place to help debug your scripts.

automation, azure app service, azure webjobs, cloud

Published at DZone with permission of Simon Foster , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}