I made this app that takes time entries from fogbugz and posts them to basecamp, however right now it must be run manually. There is a requirement for me to make it run weekly or nightly, what is the best way to do this?
IMO, best way is to write a web service that does your scheduled tasks. And to develop a windows service that will call your web service depending to your schedule.
I would create a small Windows Service that just loads the page using a simple WebRequest. Another approach is to create a background thread in your global.asax's Application_Start event. There are a lot of links on this approach, e.g.:
++ on Canavar and gattaca's comments, but I would avoid the background thread question. I hear that sometimes these threads can be recycled and since your app doesn't restart they go into nothingness. I could be wrong here. Either way less mem and solid performance writting a windows service...unless you are on a shared host that won't let you install a service.
What is the client that is requesting the ASP.NET page and what does it do with the response? Most likely answer is there isn't a client and there isn't much of a response.
Setting aside "why is it an ASP.NET page then" sort of response. Some common approaches are:-
- Use a scheduled task but that takes external configuration.
- Don't use ASP.NET but create a service to perform the task but thats even more external configuration
An approach that can be contained within the Web application is to add some dummy object to the cache that has a specified expiry period, does not allow arbitary deletion by the cache's own algorithms and has a Callback method setup when the item is dropped.
A simple but utlimately flawed approach would be to set the expiration for the item at the time you would like to execute your operation. However a better approach would be use a shorter interval for the expiration then on each expiration check a database record to see if the operation needs to execute and/or re-establish the item in the cache.
Potentially you would do this DB test transactionally with a flag indicating that its being handled. That approach would allow for Web Gardens and Farms not to be tripping over each other trying to all do the same task.
On application start you can also check the DB to see if the task needs doing before establishing the item in the cache for the first time.