I have an Azure function which process a queue having some records which needs to be inserted into a Database. Its working Fine. But I wanted to use a Timer trigger instead of queueTrigger so that I can check for a record every 30 sec. Can somebody please help me with this. I have tried a basic implementation of the Timer as described in https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer but i don't have an idea how can I process the record.
3 Answers
This is the answer that worked for me: There is no direct binding to Azure Queues or ServiceBus Queues when using a Timer Trigger. You have to write the Queue connection and reading logic yourself similar to what you would do if you are not using Functions or Console App. Something similar to the following for reading a single message from a ServiceBus Queue as an example:
var connectionString = "Endpoint=sb://.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=”"; var queueName = "";
var client = QueueClient.CreateFromConnectionString(connectionString, queueName); var message = client.Receive();
You can bind to CloudQueue via Queue input binding.
Here is a sample code of TimerTrigger that fetches message from the queue.
Function.json
{
"bindings": [
{
"type": "timerTrigger",
"name": "myTimer",
"schedule": "0 * * * * *",
"direction": "in"
},
{
"type": "queue",
"name": "clQueue",
"queueName": "myqueue",
"connection": "",
"direction": "in"
}
],
"disabled": false
}
Run.csx
#r "Microsoft.WindowsAzure.Storage"
using Microsoft.WindowsAzure.Storage.Queue;
public static async Task Run(TimerInfo myTimer, CloudQueue clQueue, TraceWriter log)
{
var message = await clQueue.GetMessageAsync();
log.Info($"{message.AsString}");
clQueue.DeleteMessage(message);
}
Based on your last comment, if you definitely don't need to use the queue trigger (remember that it won't matter if you have too many messages, Azure will scale and set the resources needed to process all messages for you, but that can be expensive to run for some time) you can simply upload the same code you'll use for example in a Console App to Azure Functions with all the libraries needed.
Here's an example of how to consume data from a queue in C#: https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-queues
You'll just need to adapt the code to run as a .csx script (the code you put in the Main method of the console app will be the same code you put in the Run method of the .csx script).