Azure WebJobs - How to run a job on a schedule using the free tier

The WebJobs capability of Microsoft Azure is pretty powerful. I picked it up in the last few weeks and found it a bit confusing to grok all the moving parts. Further complicating matters, the SDK was updated this week and the docs are not yet in sync. (Not complaining, I'm sure they will be updated soon.

WebJobs are hosted in an App Service (Web App). The SDK provides for "binding" the job to an object in storage (typically a queue or blob), and implementing such a binding along with a continuous job will run the function when new objects appear. While this is pretty terrific, it is not free. The Web App must be configured for "Always On", which requires a Basic or Standard plan. At the moment, the smallest Basic plan is about $56/month.

In my scenario, I have no need for guaranteed processing. I simply want my code to wake up on a schedule, call out to an http endpoint and process the result. As I reviewed the notes for the 1.1.1 release, I noticed a TimerTrigger. Just what I need! :)

However, the implementation for TimerTrigger requires that the JobHost must run continuously. When I deployed the sample to my Web App on the free tier, it would show as failed. Looking at the logs, I see that my code does what I want, but then the process is killed when the Web App shuts down due to inactivity.

Fortunately, there is an alternative. Azure includes Scheduler Job Collections. And the scheduler also includes a free tier, which is limited to 5 jobs at a maximum frequency of Every Hour.

The key takeaway here is that the two capabilities (Schedule and WebJobs Timer Trigger) are not related. I found details on this blog: http://blog.amitapple.com/post/2015/06/scheduling-azure-webjobs.

The trick is that my web job should not run continuously, rather call my function and then end. The necessary code snippet is buried in the tutorial page, which I've copied here for easier reference. (And tweaked to remove unnecessary params for my scenario.)

public class Program
{
  static void Main(string[] args)
  {
    JobHost host = new JobHost();
    host.Call(typeof(Program).GetMethod("RunTask"));
  }

  [NoAutomaticTrigger]
  public static void RunTask()
  {
    Console.WriteLine("Creating queue message: ", message);
  }
}

With my code now correct, the job starts, calls my method and ends. This shows as "Success" in the WebJob status site (https://.scm.azurewebsites.net/azurejobs/#/jobs/triggered/).

I hope this post helps to clear up confusion with the various pieces and allows you to leverage the free tier for this capability.