azure functions - how to schedule work with possible cancelation - c#

I'm trying to build an app to help a friend with diabetes. The diabetic will click a check in button on a phone app which will call an azure function passing an email address/mobile number etc, a duration stating when they can next check in, and another duration which specifies when it is too late to check in. If the diabetic doesn't check in during the allowed window an email or text will be sent to alert someone that the diabetic hasn't checked in. If they click the button in the next check in window the previously scheduled work needs to be cancelled (or perhaps something with state needs to be set that can then be checked before sending the email).
I have built the phone app and have managed to get a HttpTriggered function to email me. I'm now a little bit stuck. How do I schedule something to happen at a specific point in the future and how could I cancel it following the correct user interaction? Any help would be much appreciated.

I suggest you look into Durable Functions, which is an extension for Azure Functions (installed as a NuGet package) that allows running long running workflows (incl waiting for events and timers).
This extension uses storage queues and tables under the hood, but you only need to know the Durable Functions API. You also don't need to provision any other Azure services, since a Storage Account is already used by your Function App.
In your case you could use the Human Interaction pattern in combination with timers.
I have quite some video's explaining Durable Functions, you might find these helpful: https://youtube.com/playlist?list=PLoSzmz8jSD1fahiSdKdf4073AOdti_rx8.

How do I schedule something to happen at a specific point in the
future
You can make use of a Queue triggered Function (you can either use Azure Storage Queue or Azure Service Bus Queue).
Basically the idea is that you send the message to the queue with a visibility timeout so that the message only appears in the queue when you are ready to process it. This you can do in your HTTP triggered Function.
how could I cancel it following the correct user interaction?
This is where things get interesting!
The way you could do it is have some field in your database which would indicate that the user has already interacted and no further processing would be required.
Your Queue triggered Function would still fire but what you would do is first check your database if any further action is required.
If no action is required, then you would simply let the Function complete so that the message is removed from the queue.
If any action is required, then you would perform that action and then let the Function complete so that the message is removed from the queue.

Related

How can I trigger an Azure Function based on Application Insights messages with C#?

I want to create an Azure Function that changes some records in an external database when a certain message appears in the Application Insight logs. To be more specific: I want to trigger it when a "new" registered device (a device that has been reset) starts for he first time. In the database, I then want to attach the device to the logged-in user.
I currently have 2 solutions in mind:
Create something that triggers the function when a certain message appears.
Create a timed function (15 minutes or so) that check the logs for a certain message.
I'm not sure if the first one is even possible. For the second solution, I can't find a solution to be able to read the logs in Application Insights.
Does somebody know how I can create a script that reads the AppInsight logs and search for specific messages?
No idea if this is the most elegant solution, but it should work. Create a Diagnostic Setting and choose to stream the events to Event Hub or a storage account, and trigger the function for there. You'll have to do some filtering to find the message type you're looking for, and discard the ones that don't match.

How to send mail notification after Azure Devops Load test is completed

I want to automate the workflow to send out mail notifications once Load test under Azure Devops project is completed. I get to see there is no direct option available for the same. Please suggest what could be the best suitable solution.
I have created a simple URL based Load test and ran it. One way I already tried is using Devops REST API and get the status in Azure function. But that is tedious process. Please help in this.
result expected is immediately once Load test is done, there should be email triggered to the group or individual members
For now, the way you mentioned might be the way to go, as Azure DevOps does not provide notifications from this type of events OOB. Please submit your suggestion here.
References:
Default and supported notifications
Supported event types
Meanwhile, another idea I had was to have this test configured as a task in a Pipeline, so that you get an email notification sent upon build completion. However, do note that while the Cloud-based Load Test task can be added to a Job as of today, it has been deprecated, with these options as alternatives.

Checking a condition every minute on a WebAPI application

I'm learning basic web application development using Microsoft WebAPI. I've created a drinks ordering service where users order drinks, post it to the server, and the server stores them for later.
Users post their order to a RESTful endpoint on an OrdersController. The endpoint stores the order in a list and checks the list against a condition. E.g, "Are number of orders > 5?"). If satisfied, the server sends a push notification to the users' mobile phones using the Google Cloud Messaging service.
I wish to expand the type of conditions that could trigger a push message. For example, "If no orders have been received in the last 5 minutes, send a push notification". In other words, I would need to check the condition more often than just in response to receiving a new order request.
What is the best way to accomplish this? My initial thought was just to create a Timer which runs the condition checking method at intervals, but a search of stack overflow has suggested that this kind of approach might be a bad idea.
I have several ideas depending on the hosting environment:
If it's your server (virtual or otherwise)
Create a seperate application that runs as a windows service or a windows application that handles that process seperately from the webAPI.
If running on Azure as a Cloud Application you can create a web worker role that is scheduled to run every so many minutes and could then query and process items.
If you only have a hosting enviornment for MVC/WebAPI then you could look into the cache/callback trick. You basically add an entry to the cache when a callback to a method.
a. Create a controller method that adds a cache entry with a callback to a method
b. The method does whatever work it need to do then calls the controller action so another entry is placed in the cache.
Each time the cache times out, it calls the callback method which you can use to process information and then call the controller method to start the process over again.
When I was experimenting with this, I created a small scheduling project that looped through a list of tasks. Each task was responsible for determing if they any processing was needed. Amazingly enough it worked well and the server never shuts down the process.
Keep in mind if your hosting in the cloud that this will simulate activity and memory usage which could cost you some amount of money, although I would think it would be trival.
I've heard they are scheduling solutions, possible even a monitoring service that can call a WebAPI endpoint every so many minutes which could work much like the callback method. You could check for the need to process each time the monitoring service calls the endpoint.
You can use Reactive Extensions: it lets you program time-related events in a declarative manner without having to bother with handling timeouts and tracking. You can even test your setup by controlling time itself!
For example, to trigger an event after some inactivity, you could use the Timeout method of an observable. The Buffer method can let group trigger an event after x drinks have been ordered, etc
In general what you describe sounds like a backend task/responsibility. You can consider this library HangFire.io to safely perform this operation from ASP.NET - http://hangfire.io/
The other way you could simulate event firing from with ASP.NET for simple one-off cases is , is you can you can use the built-in cache and its expiration event.

Long running operation on website

What if I have website with a button. User clicks on the button and starts a long running process. After a few hours (or minutes) user update webpage and see results. What is the best (and any other) way to implement long running operation on website?
From the user experience point of view I suggest you implement the job like you would implement order handling in an online shop.
When the user starts the job he should be able to track the state of the job. Is the job complete? Did the job fail? What is the progress of the job? He should probably also be able to cancel the job and perhaps modify properties of the job. You could implement an notification mechanism using e-mail or an SMS and the user should be able to control that.
By using JavaScript/AJAX you can provide a more interactive user experience where the job status web page is automatically refreshed at regular intervals in the background without forcing the user to refresh his browser.
The user should be able to leave his computer and later connect to the website from another computer and still be able to get information about pending and completed jobs. This requires some form of user login.
I suggest that you separate the job handling code from the website code. You could expose the job handling user a web service or another similar technology. The website should query the service and display the results providing a user interface on top of the job service.
How long? If it's really going to take hours, you don't want that code running in the web server. Have the server spawn an external process or start a service or something along that line, and put the long running code in there.
You could have that code put status updates somewhere (like in the database) as it runs, and the website can check that when the user comes back to see how it's doing.
I suggest you read up on Threading in asp.Net. For website performance, time consuming processes can be put on a different thread.
A different approach is executing the task with AJAX. This way, you can present the user interface to the user, and start an asynchronous process which handles the request.

Schedule method invocation C#

I'm using c# to communicate with twitter and i need to code a schedule system to send twitter messages at a user defined date.
The messages to be sent are in a database with the date and time of deliver.
Which is the best method to check the db for scheduled messages and send it when the time arrives?
How accurate do you need the timing to be? Could you get away with polling the database every 5 minutes, saying "Tell me all the messages which need to be delivered before current time + 5 minutes" and then sending them all? (And marking them as sent in the database, of course.) Does it matter if they're a bit early or late?
You can do the scheduling on the C# side to make it more accurate, but unless you really need to I'd stick with a very simple solution.
(Depending on your database there may be clever ways of getting callbacks triggered etc... but again, I'd stick with the simplest solution which works.)
In addition to the windows service option (or background thread), you could just set up a scheduled task to run an app that polls the DB and sends the tweets once every defined interval.
Windows schedules can be setup using C# if needed and are really easy to set up manually.
There are several ways to do this, but I guess the best way is to set up a Windows Service that will periodically poll (frequency is up to you) the DB for any scheduled tweets that hasn't been sent.
Needless to say you'll need to handle scenarios such as the Internet connection or DB being down, etc.
In fact the solution consists in using a windows service but it can't communicate directly with the ASP.NET MVC app. I've added a Web Service that handles the task and a System.Threading.Timer in Windows Service to periodically call the Web Service.

Categories

Resources