Reputation: 237
I have a windows service to upload file to different ftp site.
In order to speed up uploading, this service installed on 3 machines. Then problem occurs, because 3 services all look into same location to pick up file, sometimes they just pick up same file and then exception throws.
How to avoid such situation? the service designed by .net 4.5
with async
method, so if file lock applied, is performance will be slower? I am thinking this way,
but don't know how.
Upvotes: 1
Views: 860
Reputation: 19340
You can create a queue. It can be MSMQ or another service, which will load db Table with file names. Your other services can use some lock table.
You would have a table with a single record, which your service will call update table t1 set t1.f1=t1.f1
in transaction. This will prevent other services from continuing.
In another table you have list of files and a flag if it is already taken.
Consider one service only reading these files and loading the file name table. 3 other services - end points, will lock the lock table record and then proceed to reading n
file names and mark them as taken. Once files are loaded, file names can be deleted.
If the service fails between taken and deleted: at the start of next process, check timestamp. If files were taken, lets say, yesterday and records still exist, may be they need to be processed again.
Upvotes: 0
Reputation: 51
You can open the file for exclusive read, if it fails it means another service currently processes it, then move to the next file.
File.Open("test.txt", FileMode.Open, FileAccess.Read, FileShare.None);
Upvotes: 2