How to save money on Azure with WebJobs

 Date: April 26, 2016

This blog is hosted on Azure. It is using WordPress, which stores its data in MySQL database hosted by ClearDb. I blogged about the issues with WordPress database size over a year ago. The issue is inserting transient entries into database that are growing its size and exceeding free 20MB ClearDB limit. As I mentioned in this blog post - you can use plugins that clear database for you, or...you can use web jobs. Azure WebJobs is very neat way to perform custom tasks, such as database maintenance, periodically.

I created a web job that is performing database maintenance for me. Everyday it removes 'transient' entries from wp_options table to keep my MySQL database under 20MB ClearDB limit.

One problem was: how to do MySQL backup without mysqldump? As you know, there is a Virtual Machine underneath every Azure website. And this VM has MySQL, along with mysqldump installed:

Azure Console: mysqldump

Based on this article I created a small Console App called DbMaintenance that performs 3 mentioned tasks:

  1. Backup database
  2. Remove transient entries from wp_options table.
  3. Backup database

Two backups gives me more confidence that I have my database backed up correctly. Especially because I am performing them before and after the operation that potentially can broke things.

You can customize my app by editing Initialize method. Current paths (for mysqldump and database backup directory) are set for Azure Website. Additionally you have to create directory for backups (D:\home\db-backups). You can do it from the web app console available on the Azure Portal:

mkdir D:\home\db-backup

Once this is done, you need to zip DbMaintenance.exe and MySql.Data.dll file, and create a WebJob. On the Azure Portal go to your web app -> Settings -> WebJobs, and add a new WebJob:

Azure Portal: add WebJob

You can see the status in Settings -> WebJobs -> YOUR_JOB_NAME:

azure webjobs

By clicking on logs column, you can get details about web job:

Azure WebJob: details

And console output from each execution:

Azure WebJob: console output

I configured my WebJob to run everyday. You can also run it on demand, or continuously.

You can find more about Azure WebJobs in this article.

 Tags:  blog programming

Previous
⏪ Azure Portal Tips & Tricks - 03. Customizing Side Bar

Next
Azure Portal Tips &Tricks - 04. Keyboard Shortcuts ⏩