views:

324

answers:

2

I have an application running on multiple IIS servers that need to parse a CSV file that is placed on a common network drive. If I use the System.IO.FileSystemWatcher Created event to be notified of the event when the file is available, how can I ensure only one server parses the file? If all servers are notified of this change, all will try to parse it. Should the first server copy this locally and then parse it? Would this cause errors on the other servers? Should I set the remote directory access to only allow one server read access?

The file contains records that needs to be inserted into a shared database, but there is no unique key for the records. Therefore, if more than one server grabs the file at the same time and inserts the records, there will be duplicates in the database.

+2  A: 

Key question is: Does something bad happen when all of them try and parse the file at roughly the same time? Multiple read access is basically allowed on the file system level, so this fact alone would not necessarily break anything.

I would probably try the method of creating a lock file on first file access and make servers look for the lock file before accessing the file. Delete the lock file when you're done.

You could also place an exclusive lock on the file, only one server will be able to do that at a time. The others will fail trying:

FileStream fs = new FileStream(
  FilePath, 
  FileMode.Open,
  FileAccess.ReadWrite, 
  FileShare.None
);
Tomalak
A: 

Another option instead of the lock is to rename the file into a directory. Make a sub-directory for each server and then have a server try and re-name the file into its directory (rename is an atomic operation). The one to succed gets to parse the file.

The lock issue will be tricky as the file will still be visible to each server and they might try and open the file and then repeatly error. The rename will be absolute as to the owner of the file.

Also keep in mind whatever solution you use that there will be the possibility of a "lost file". If a server stops processing a file (error, exception, reboot...) it might be abandoned. The typical solution to these types of problems is in addition to things like the file watcher you have servers poll every now and then as well.

Copying the file to a server dir will help a bit with this. One downside would be if a server goes completely off-line, then its file will sit in a dir until the server comes back on-line.

John Dyer