Win
Win

Reputation: 2613

How to reset a database value after an elapsed time ASP.NET MVC C#

Im stuck as to how to tackle a problem i have come up against. I want the user to change a value that is stored to a database from a dropdown. After an elapsed time (say 1 hour) i want the value to be reset to the default. Will i have to write a service that sits on a server and gets activated when the value is changed? Literally don't know where to start and therefor look for a solution.

Upvotes: 1

Views: 243

Answers (2)

László Koller
László Koller

Reputation: 1159

This sounds like a perfect use case for Revalee. Revalee, an open source project, is a service that allows you to schedule callbacks to your web application. Revalee manages task persistence and scheduling using a Windows Service, but leverages your ASP.NET MVC application to handle the required processing (resetting the database value, in your case).

To use Revalee, you would:

  1. Install the Revalee Service, a Windows Service, on your server. The Windows Service is available in the source code (which you would compile yourself), in a precompiled version available at the Revalee website, or easily installable via Chocolatey.

  2. Use the Revalee client library in your Visual Studio project. The client library is available in the source code (which, again, you would compile yourself) or in a precompiled version available via NuGet.

  3. To register a callback (using Revalee's client library, in this example), you would include a method similar to the following in your ASP.NET MVC application. This would be called when the user initially stores the "reset-able" value:

    private void ScheduleReset(int userId)
    {
        // The callback will be 1 hour from now
        DateTimeOffset callbackTime = DateTimeOffset.Now.AddHours(1.0);
    
        // The callback URL will include the user ID
        Uri callbackUrl = new Uri(
                string.Format(
                        "http://mywebapp.com/Schedule/Reset/{0}",
                        userId
                    )
            );
    
        // Register the callback request with the Revalee service
        RevaleeRegistrar.ScheduleCallback(callbackTime, callbackUrl);
    }
    
  4. When the Reset action is called back (with the included user's ID) in one hour, then your application would reset that user's stored value to its preferred default value.

The Revalee project website has a complete API Reference as well as instructions on how to install and configure the Windows Service.

I hope this helps. Good luck!

Disclaimer: I was one of the developers involved with the Revalee project. To be clear, however, Revalee is free, open source software. The source code is available on GitHub.

Upvotes: 2

user1666620
user1666620

Reputation: 4808

this seems like a design decision which has multiple "right" answers.

I might add a field to the record that has a datetime which stores the date the item was changed. Batch job then runs regularly which picks up those records which have expired since the last run time, then changes them back.

Upvotes: 0

Related Questions