I actually had a requirement a few years back to do something similar to this. It was for processing monthly membership dues. The way I would do it is simple. First, in your database, add a table similar to this:
--------------------------------------
|Monthly Processing Table |
|------------------------------------|
|MonthlyProcessorId int PK |
|ProcessMonth int |
|ProcessYear int |
|HasBeenProcessed bool |
|CanBeProcessed bool |
|ProcessDate DateTime |
|------------------------------------|
Of course, you can add other fields as you see fit. This is only for a demonstration.
Next, create your web page that contains the button that should get pressed monthly to begin the processing. When the button gets hit, it should perform the following logic:
- Get the current month and year (
DateTime.Now)
- Search the monthly processing table for rows that match
ProcessMonth equal to DateTime.Now.Month AND ProcessYear equal to DateTime.Now.Year.
- If there are any rows returned, then check the row and see if
HasBeenProcessed is true and ProcessDate to see when and report this to the user and you are done. This is an important step because if rows are returns and HasBeenProcessed is true, then the monthly dues have probably already run for this month and you do not want to double charge people. So don't skip this step and stop the program execution logic here if HasBeenProcessed is true!
- If there are no rows returned, then create a new row. Set
ProcessMonth equal to DateTime.Now.Month, ProcessYear to DateTime.Now.Year. Set CanBeProcessed to true and HasBeenProcessed to false.
The last piece of the puzzle is to have a Windows Service that sits in the background and periodically pulls all rows from the database where HasBeenProcessed is equal to false AND CanBeProcessed is equal to true.
If any rows are returned, then you just process them and after processing them, set the ProcessDate to DateTime.Now and set HasBeenProcessed to true. Finally, your service just goes back to periodically checking. When I say "periodically checking", I'm talking about maybe once a day or maybe even once a week, depending on your requirements. This service shouldn't bog down your SQL server in any way.
Instead of a Windows Service, you could have a Console Application that is scheduled to run with Windows Scheduler. I prefer the Windows Service way though, because it just "feels more right" to me.
Now if you wanted to get really fancy about it, you could write your schedule time and logic in an interface. This way you could reuse this Windows Service down the road.