views:

545

answers:

10

I am developing a website using VS 2008 (C#). My current mission is to develop a module that should perform the following tasks:

  • Every 15 minutes a process need to communicate with the database to find out whether a new user is added to the "User" table in the database through registration
  • If it finds an new entry, it should add that entry to an xml file (say NewUsers18Jan2009.xml).

In order to achieve this, which of the following one is most appropriate?

  1. Threads
  2. Windows Service
  3. Other

Are there any samples available to demonstrate this?

+3  A: 

Separate this task from your website. Everything website does goes through webserver. Put the logic into class library (so you can use it in the future if you will need to ad on-demand checking), and use this class in console application. Use Windows “Scheduled task” feature and set this console app to run every 15 minutes. This is far better solution than running scheduled task via IIS.

smok1
+3  A: 
Traveling Tech Guy
A: 

Create a new table (or text file) that stores the last time you did a "new user export" or additionally just look for the modified/creation date of the last export file. When your script hits your DB do a SQL command to get all users created after the last export time. Spit out new XML for each user.

Set this script to run as a windows scheduled task/cron job/maybe even a database trigger.

Pete Duncanson
+1  A: 

I think your approach is wrong. You should do one of two things:

  • Add a user to the XML file as the last step in creating the user.
  • Generate the XML file on demand when it is requested. This will give you real-time information with very little overhead.
Juliet
+1  A: 

A windows service would give you a good solution - or if you're using SQL Server you could fire this kind of processing from a SQL Agent job.

If you want the code to be 'part' of your web application you could always fire the logic fro a heartbeat page which runs your task(s) whenever the url is called - you can then poll the url from a service or agent job.

Chris W
A: 

Since you seem to be adding all users for a given day to a single XML file (as per your post), why do you need to do this every 15 minutes? Wouldn't it be sufficient to do this once after midnight?

When you do that, preferably in a Windows Service (if it has to run every 15 minutes) or in a command line app that's scheduled to run once at e.g. 0:15 hours, you'll just need to check the "sign up" date for the users and if there's any that signed up the past day, you add them to your list and export that list to the XML file at the end of processing the table.

Marc

marc_s
This is Visitors Tracking website;It has to report to the admin for eveny 15 minutes whether a new entry is there or not.
OK, then I'd support everyone else who suggested using a Windows NT Service to handle this - scan the table of Users for the changes since the last scan (keep track of that last scan date) and fill the changes into a XML file
marc_s
+2  A: 

Why in the world would an admin need to get pinged every 15 minutes? Poor admin!

I would just put a time stamp on each entry in your users table and create a quick report to allow the admin to query the data whenever they need it.

Chuck
:) that how our architect designed the HLD
Ouch! And I'm assuming you're at an organization that forces you to live by the HLD once it's approved. :-)
Chuck
Get yourself a new architect :)
Runeborg
A: 

The simplest approach is to create a System.Threading.Timer in your app on Application_Start and put it in some static field so it does not get collected. That way you can have your web app poll the database without an external process needed. Of course if your app goes down so does the timer. For the polling logic just keep a last userId (if you have an increment policy) and check for new added users by filtering WHERE id > lastId.

AZ
A: 

While I also voted for creating a Windows Service to perform this function for you, the simplest way I could think to do it would be to put a trigger on your "Users" table that would create the xml file for you when a user is inserted.

sunmorgus
A: 

I second Chuck's answer - you could pull this data from the database using an XML query and send it directly, no need to much around creating files on the system.

Paddy