views:

68

answers:

4

I am writing a c# windows service which will be polling an SFTP folder for new files (one file = one job) and processing them. Multiple instances of the service may be running at the same time, so it is important that they do not step on each other.

I realize that an SFTP folder does not make an ideal queue, but that's what I have to work with. What do I need to do to either use this SFTP folder as a concurrent message queue, or safely represent it in a way that can be used concurrently?

A: 

Multiple instances of the service may be running at the same time

On the same machine, or different ones?

joefis
On different ones
Gabe Moothart
A: 

Not sure if moving a file in Windows is an atomic operation. If it is, then when a service chooses to work on a file, it should attempt to move the file to another folder. If the move operation is successful, then it is safe to work on the file.

mbeckish
+1  A: 

Seems like your biggest problem would be dealing with multiple instances of the program stepping on each other and processing the same files.

The way I've handled this in the past is to have the program grab the first file and immediately rename it from say 'filename.txt' to 'filename.txt.processing'. The processes would be set up to ignore any file ending in '.processing' so that they don't step on each other. I don't think a file rename is perfectly atomic, but I've never had any problems with it.

Eric Petroelje
A: 

You could also leveragea datasbase to keep track of which files are being processed, have been processed or are awaiting processing.

This adds the complicatio of updating the table with new files.

Development 4.0