0

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.

Community
  • 1
  • 1
Craig Innes
  • 1,573
  • 11
  • 23

3 Answers3

2

I have several ideas depending on the hosting environment:

  1. 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.

  1. 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.

  2. 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.

  1. 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.
CubeRoot
  • 552
  • 2
  • 13
0

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

samy
  • 14,832
  • 2
  • 54
  • 82
0

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.

govin
  • 6,445
  • 5
  • 43
  • 56