Question

I create an Azure WebJob as shown in the following script which dequeues an item from Azure Storage Queue, and then stores it to an Azure Table. The process is finished running within 1 or 2 seconds, but it runs only few a times in a minute (and occasionally halted in approximately 10 minutes). Overall, it does not work well.

What am I missing? Maybe I'm mixing trigger job and continuous job, but it is hard to find an appropriate sample.

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("started at {0}", DateTime.Now.ToString("s"));

        // Continuous job should have infinite loop.
        while(true){
            var host = new JobHost();
            host.RunAndBlock();
        }
    }

    public static void ProcessQueueMessage([QueueInput("blogqueue")] string json)
    {
        var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings    ["StorageConnectionString"].ConnectionString);
        var tableClient = storageAccount.CreateCloudTableClient();

        // Store it to Azure Table Storage
        // omitted...

        Console.WriteLine("Received JSON: {0}", json);
    }
Was it helpful?

Solution

A few comments:

  1. You don't need the while(true) statement. RunAndBlock already has a while loop inside and it will block there.
  2. You don't need Manually retrieve the table. You can bind to a table as shown in the samples here
  3. You can bind to the storage account, you don't need to manually read the connection string from the configuration file and create the instance.

Here is an example of binding to storage account:

public static void ProcessQueueMessage([QueueInput("blogqueue")] string json, CloudStorageAccount) 
{
    ...
}

This is a wild guess but, from your code, it seems that the storage connection string is stored in StorageConnectionString in the config file. The JobHost expects either (1) the connection strings for runtime and data to be stored in AzureJobsData and AzureJobsRuntime (as described here) or (2) pass the connection string as parameter to the JobHost constructor. I think this is the reason of the crash.

OTHER TIPS

Just change your publish settings in ProjectName/Properties/webjob-publish-settings.json

"runMode": "OnDemand"  =>  "runMode": "Continuous"

For a continuous WebJob to work properly you need to enable "Always On" in the configuration tab of your website.

The "Always On" feature is only offered for Basic/Standard website plan, if it is not enabled your WebJob can go down after some time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top