tags:

views:

24

answers:

2

Hi all,

For a work project I am undertaking a task that will see me create a C#.NET Windows Service that will run 24/7. The program will essentially simply move files (based on a given path and a regex) between various locations - from/to HTTP, Network Paths and FTP. The administrator will schedule these jobs via an administration page in ASP.NET, where all jobs will be stored in a database. Seemingly the hardest part is scheduling these jobs - it is very simple to schedule tasks to run on a certain day at a certain time, but it also needs to be robust enough to allow tasks to run every day, every week day, every certain day of the month, or even every few seconds/minutes. I have concluded that the easiest way to store this scheduling information would be to use cron syntax (http://adminschoice.com/crontab-quick-reference) in the database.

The application has a timer to check the database for changes every 10 minutes, but the main issue I have is going about retrieving the tasks to be performed - I guess one option would be to simply retrieve everything every 10 minutes, and check each individual row to see if they are due to be performed. But I feel this is a very inefficient and technically lazy way - how would you guys go about this?

I know that Windows Scheduled Tasks would be a much easier way, but seems like my boss doesn't want that.

Thanks in advance to anyone that can offer some tips/advice.

A: 

If the number of tasks you have to keep track of isn't huge, you could maintain a priority queue of all the tasks in memory. The nodes would contain the record key (from the database) and the computed next update date/time.

When the program starts up, read every record from the database and construct your priority queue. Once the queue is constructed, look at the first item's date and set a timer to wake up at that time. When the timer wakes up, execute all tasks that are currently due, reschedule them for their next update date/time, find the next item that needs updating and set a timer to wake up when that time arrives.

To handle modifications, you can have the web application that modifies things to notify your service, too. You'd have to cancel the existing timer and re-schedule, but you wouldn't have to read the entire database except at startup.

That said, I agree with @Oded: convince your boss to use Windows Scheduled Tasks. If he doesn't like that interface, you can have your web app (or a service that the web app communicates with) talk to the Windows Task Scheduler API. Codeplex has a wrapper: http://taskscheduler.codeplex.com/

Jim Mischel
A: 

Have a look at Quartz.NET - pretty much does exactly what you want. You can schedule it using Cron expressions too.

If you store the enough information about each job in the database, you'll be able to create a job each time your service starts. You can poll for changes periodically and update the job's rules.

Hosting this in a windows service is a good way to do it - allow you to monitor that its running and provides standard mechanisms to restart it should it fall over (net start xxx) etc. Much better than scheduled tasks.

Michael Shimmins