Run multiple scheduled tasks from a single Azure WebJob
Setup multiple WebJob tasks without having to create multiple WebJob projects
Create a single WebJob in Visual Studio to be called Continuously
Follow Microsoft's instructions to create and publish a WebJob from within Visual Studio. Set the WebJob run mode to "Run Continuously".
Then, using the Queue examples found in the link below, setup your WebJob to watch a queue.
Use the "Add Connected Services" dialog box in Visual Studio to attach an Azure Storage Account with a Queue that your WebJob function will watch. When a message is posted on the Queue, your function will get called.
In this example, a message named "adhoc" is placed in the Storage account / Queue, on an adhoc basis by other code in my website (see below).
public class Functions
{
public static void ProcessAdhocMessage([QueueTrigger("adhoc")] string message, TextWriter log)
{
...
}
}
Next, add an additional WebJob function to your project
Add a second function to the WebJob project to watch for a different message on the queue.
The Azure Scheduler Job service has lots of options to place a message on a storage queue at specific times, with various recurring and ending options.
Create an Azure "Scheduler Job Collection", and add a "Scheduler Job" with an Action of "Storage queue" to place a message on the queue that matches the QueueTrigger in the function just added. Now your second WebJob method will get called every time the Scheduler Job places that message on the queue. Many options are available to have the Scheduler add the message to the Queue repeatedly at defined times.
In this example, a second message named "daily" is being posted to the Storage account / Queue, by the Azure Scheduler Job service.
public class Functions
{
public static void ProcessAdhocMessage([QueueTrigger("adhoc")] string message, TextWriter log)
{
...
}
public static void ProcessDailyMessage([QueueTrigger("daily")] string message, TextWriter log)
{
...
}
}
Code to add a message to the queue and trigger a WebJob function
For adhoc, non-scheduled, triggering of the WebJob methods, connect to the Storage Account and place a message on the queue.
var storageAccount = CloudStorageAccount.Parse( ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString());
var client = storageAccount.CreateCloudQueueClient();
client.DefaultRequestOptions.RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(3), 3);
var queue = client.GetQueueReference("adhoc");
await queue.CreateIfNotExistsAsync();
var queueMessage = new CloudQueueMessage("some-value-to-send-to-the-queue");
await queue.AddMessageAsync(queueMessage);
The WebJob Main() method to process messages continuously
Add the following to the <connectionStrings> section of the WebJob App.config.
<add name="AzureWebJobsDashboard" connectionString="DefaultEndpointsProtocol=https; AccountName=accnt-name; AccountKey=accnt-key"/>
<add name="AzureWebJobsStorage" connectionString="DefaultEndpointsProtocol=https; AccountName=accnt-name; AccountKey=accnt-key"/>
class Program
{
static void Main()
{
var config = new JobHostConfiguration();
if (config.IsDevelopment)
{
config.UseDevelopmentSettings();
}
var host = new JobHost();
host.RunAndBlock();
}
}
For further information, see the following:
Azure WebJobs SDK Samples on GitHub - Queues