How to perform background jobs on an ASP.NET MVC 3 site?

I am currently working on an e-commerce site, and there is one feature that I am not very sure how to implement. In most cases, you simply add the item (s) to your cart and buy them, which is probably the easiest workflow. What I ask is a little different, but what if there is time to buy a product? I mean that some sites give you the exact time to buy the product (for example, Soccer Manager), on those sites on which you cannot store the product forever, there is a limit of 15 minutes, and if you do not buy during this period, be released from your cart. (and most likely someone else will jump on him)

Now, as an ASP.NET MVC programmer, I would like to implement this function, but, as I said, I'm not sure how to do this. I think when I add the product to the cart, I need to hold on to time (something like ItemAddedAt), and I need to release this item in x minutes, so something needs to be launched in 6 minutes to release this product. In global thinking, I think I need a service, when I add an item, I also need to subscribe to this service, and the service starts a timer / work in the background. What I don’t know / do not have experience, this part, how to do it in an ASP.NET MVC project, is there a sample project, article, library or something like that?

Of course, I don’t know if my logic is suitable for this problem, I need a manual, if possible, some source code to work.

+6
source share
2 answers

AFAIK there is no standard way to declare / program tasks in an MVC project. The recommended way to accomplish what you want is to create a new Console Application project in your solution and use the Windows task scheduler to execute every X minutes, releasing any products that were more than X minutes in any cart.

To do this, you will need to refer to your MVC project from a new one (in order to access all models) or, even better, create a class library project, move the model / database classes there and link it from MVC projects and the console.


All that is said is actually a small β€œhack” that can be used to get programmed tasks in an MVC project. You can use the following code:

HttpContext.Current.Cache.Add("Task", "1", null, DateTime.MaxValue, TimeSpan.FromMinutes(5), CacheItemPriority.Normal, new CacheItemRemovedCallback(CheckCarts)); 

This line, which can be called, for example, from Global.asax, will add the entry "Task" to the cache. The stored value ("1") does not matter, it is important that the cache entry expires in five minutes and, when expired, calls the "CheckCarts" method (defined in Global.asax or in the class that you are executing this code).

 public void CheckCarts(string key, object value, CacheItemRemovedReason reason) { // Insert your code here to check for expired carts (...) // We add the entry again to the cache, so that this method will be called again in 5 minutes. HttpContext.Current.Cache.Add("Task", "1", null, DateTime.MaxValue, TimeSpan.FromMinutes(5), CacheItemPriority.Normal, new CacheItemRemovedCallback(CheckCarts)); } 

When the cache expires, the CheckCarts method is called, your code does everything it needs to do, and at the end adds itself to the cache again to be called after another 5 minutes.

+4
source

This script just screams signalR. This will allow you to do what you ask in a simple way.

If the items in your cart have an expiration date, you can get your survey if any items have expired. If you have one, you can run the delete code for this item and update your ui.

+3
source

Source: https://habr.com/ru/post/914497/


All Articles