Trigger Webjobs at specific times

I want to run an Azure Webjob 24 hours after adding a record to the database using .NET. Obviously there will be multiple tasks for the Webjob, all at the scheduled time. Is there a way (in the Azure Library for .NET) where I can schedule these tasks?

I can use message queues, but I want to try and avoid unnecessarily polling the WebJob for new messages.

+3


source to share


3 answers


Since the Add Database Record event is the trigger here, you can use the Azure Management Libraries to create an Azure Scheduler Job to execute 24 hours after the db record was set. Azure Scheduler Jobs can only do 3 things: make HTTP / HTTPS requests or send messages to a queue. Since you don't want to poll queues, here are two options

  • Deploy an existing web resource as a Wep API where each task is available at unique URLs so that the scheduler task can make the correct HTTP / HTTPS request

  • Create a new WebAPI / Wep API that accepts a takeover request (such as a man in the middle) and progmatically runs an existing webJob on demand, again using the Azure Management Libraries.



Please let me know if any of these strategies will help.

+1


source


If you want to start a WebJob execution 24 hours after the record is inserted into the SQL database, I would definitely use Azure Queues for that. So after inserting the record, just add the message to the queue.

For this, you can easily use a property initialVisibilityDelay

that can be passed to a method CloudQueue.AddMessage()

. This will make the post invisible for 24 hours in your case, and then it will be processed by your Webjob. You don't need to schedule anything, just use the Continuous WebJob to listen for the queue.

Here's some sample code:



public void AddMessage(T message, TimeSpan visibilityDelay)
{
    var serializedMessage = JsonConvert.SerializeObject(message);
    var queue = GetQueueReference(message);
    queue.AddMessage(new CloudQueueMessage(serializedMessage), null, visibilityDelay);
}

private static CloudQueue GetQueueReference(T message)
{
    var storageAccount = CloudStorageAccount.Parse("Insert connection string");
    var queueClient = storageAccount.CreateCloudQueueClient();
    var queueReference = queueClient.GetQueueReference("Insert Queue Name");

    queueReference.CreateIfNotExists();
    return queueReference;
}

      

Hope it helps

+3


source


To call a WebJob from your site is not a good idea, not you can add WebJob code to your site and just call that code. you can still easily use the WebJob SDK on your website.

https://github.com/Azure/azure-webjobs-sdk-samples

we would not recommend calling a WebJob from your Website, it is that the call contains a secret that you would rather not store on your website (deployment credentials).

Recommendation : To separate the WebJob and website code, it is best to communicate using a queue , the WebJob listens on the queueand the website redirects the request to the queue .

+1


source







All Articles