Microsoft Azure Websites: Launch WebJobs actions using queues or blobs

These days I’m working on a project that needs to launch some tasks which can run in the background. I am using Microsoft Azure Web sites so I can use Web Jobs for that purpose. Few days ago I was talking about how it’s possible using a console application and publish it on Azure as WebJob. However, this time I don’t want a scheduled task, but the process is launched through an action. Thanks to WebJobs SDK I can create a background process that keeps listening for certain actions. The SDK is still in pre release.

To test it, I created a Microsoft Azure project type WebJob :

Microsoft Azure WebJob

And I installed the SDK WebJobs through Nuget:

PM> Install-Package Microsoft.Azure.Jobs -Pre

using Microsoft.Azure.WebJobs;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebJob
{
    // To learn more about Microsoft Azure WebJobs, please see http://go.microsoft.com/fwlink/?LinkID=401557
    class Program
    {
        static void Main()
        {
            var host = new JobHost();

            host.RunAndBlock();

        }

        public static void CheckingBlobs([BlobTrigger("input/{name}")] TextReader input, [Blob("output/{name}")] out string output)
        {
            output = input.ReadToEnd();
        }

        public static void TestingQueuesTriggers([QueueTrigger("queuejobs")]string message)
        {
            Console.WriteLine("TestingQueuesTriggers launched: {0}", message);
        }
    }
}

In the code above, the first thing we do is create an object of type JobHost that keeps the process running and waiting for any of the triggers. In addition, we have two methods that allow us to capture two things: new messages in a queue called queuejobs and upload new blobs into a container called input. In the case of blobs, so that the event is launched, the date of the file in input must be greater than the date of the possible file in the output container, if there was.

Before publishing on Azure, we can see if the job works correctly, running it on local:

WebJobs SDK local

WebJobs need two connection strings: AzureWebJobsDashboard and AzureWebJobsStorage. The first one is used to store the traces related to the diagnosis of the job and the second one will be the Azure Storage account on which the service will listen.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https;AccountName=[YOUR_ACCOUNT_NAME];AccountKey=[YOUR_ACCOUNT_KEY]"/>
  <add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https;AccountName=[YOUR_ACCOUNT_NAME];AccountKey=[YOUR_ACCOUNT_KEY]"/>
  </connectionStrings>
</configuration>

You can also specify the Storage account inside of the WebJob, when we create the instance of JobHost:

            var storage = "DefaultEndpointsProtocol=https;AccountName=[YOUR_ACCOUNT_NAME];AccountKey=[YOUR_KEY]";
            var host = new JobHost(new JobHostConfiguration
            {
                StorageConnectionString = storage,
                DashboardConnectionString = storage
            });

            host.RunAndBlock();

To add new blobs and messages in the queue I’ve used Azure Storage Explorer.

Publishing a WebJob to Azure

Through the action Publish as WebJob We are ready to upload the job to the web site::

Publis as Azure WebJob

In this case you need to setup the WebJob as Run Continuously:

Run Continuously

Select the web site that you want for your tasks. If we had not indicated any Microsoft Azure Storage account where the queue is associated to trigger and the container where we will store the blobs, by default the service will search both connection strings in the web site configuration:

WebJobs connection strings

On the other hand, we have a page where we can view the status of WebJob once it is on Azure:
https://[YOUR_SITE].scm.azurewebsites.net/azurejobs/#/jobs/continuous/WebJobTest. El enlace puede verse en la sección de WebJobs del sitio web:

WebJobs Logs

Here we can see every time a method is executed and the output of the WebJob:

WebJob output
The process of the queues is much faster than the blob. The service checks the container every 10-20 minutes.

Hope this helps.

Happy backgrounding!