0

I'm trying to find the most straight forward way to have a single persistent variable in and Azure function. The use case is as follows:

1) check a web site for a number (eg. 76);

2) If number if greater than current number (eg. 72) send an email to users for each new number (eg. 73, 74, 75, 76);

3) Run the above every hour.

What would be the best way to save the new number after the function runs?

Thank you, I appreciate any thoughts on best practices.

MW

M.Will
  • 27
  • 5

1 Answers1

0

You could use the Queue Storage to implement it, set the variable num as a queue(eg. 72), in the function retrieve it and compare it with the number you send(I test with HTTP trigger). Dequeue the queue(72) and add a new message with the max one. The below is my test code.

 [FunctionName("Function1")]
    [return: Queue("number")]
    public static async Task<string> RunAsync(
        [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        // Parse the connection string and return a reference to the storage account.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Environment.GetEnvironmentVariable("AzureWebJobsStorage"));

        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();

        // Retrieve a reference to a queue
        CloudQueue queue = queueClient.GetQueueReference("number");

        // Async dequeue the message
        CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();
        await queue.DeleteMessageAsync(retrievedMessage);

        int numval = Convert.ToInt32(retrievedMessage.AsString);

        int num = Convert.ToInt32(req.Query["num"]);

        num = Math.Max(num, numval);

        return num.ToString();

    }
George Chen
  • 13,703
  • 2
  • 11
  • 26