1

I create an service that moves files from Linux Computer to Windows by FTPClient. The files in LinuxComputer are generate by an Oracle Store Procedure.

The problem is, I don't know when the files are not being write anymore. Because, first Oracle create the file with 0kb, and then it start writing in it. I add a delayed to get the file, but it is not the solution.

FTP Connection

   FluentFTP.FtpClient originClient = new FluentFTP.FtpClient(FTPOriginHost, FTPOriginUser, FTPOriginPass);
  originClient.Connect();
  Log.Info("FTP client is Connected.");
  originClient.GetWorkingDirectoryAsync().ContinueWith(d => Log.Info(d.Result));
  originClient.SetWorkingDirectoryAsync(FTPOriginPath).Wait();
  originClient.GetWorkingDirectoryAsync().ContinueWith(d => Log.Info(d.Result)).Wait();
  return originClient;

Download

 originClient.GetListingAsync(FTPOriginPath).ContinueWith(t =>
   {
      foreach (var item in t.Result)
      {
         originClient.DownloadFileAsync(DestinationPath + item.Name, item.FullName, true).ContinueWith(tt =>
          {
              Log.Info(item.Name + " DOWNLOAD: OK");
          }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion).Wait();
       }
   }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion).Wait();

I thougth in moving file to another copy, to check if it is being copy or not, but if I move file, to another folder in Linux while it is being write, it is move ok, and continue being write ok in the other folder so it does not work.e

DanielGatti
  • 653
  • 8
  • 21
  • The only way of knowing when the files ends is to add a byte count at beginning of message. – jdweng Jul 27 '17 at 12:22

1 Answers1

1

What I usually do is to create an empty file after the main file has been written. The empty file will have the same name as the main file but either have a 'RDY_' suffix or '_RDY' prefix..

The client will then just check for these files, if it finds one, transfer the main file and after the transfer is complete delete the 'RDY' file. That way the process generating those files won't have a lock on that RDY file when you delete it.

Your download routine would have to be modified like this:

originClient.GetListingAsync(FTPOriginPath).ContinueWith(t =>
   {
      foreach (var item in t.Result)
      {
         if (item.Name.EndsWith("_RDY"))
         {
         originClient.DownloadFileAsync(DestinationPath + item.Name.Substring(0, 4), item.FullName.Substring(0, 4), true).ContinueWith(tt =>
          {
              Log.Info(item.Name.Substring(0, 4) + " DOWNLOAD: OK");
              originClient.DeleteFile(item.FullName); // of course you would have to adjust this call to the correct name of a method that deletes a file
          }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion).Wait();
         }
       }
   }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnRanToCompletion).Wait();

You would, of course, have to adjust the stored procedure that generates the main file as well.

Steffen Winkler
  • 2,805
  • 2
  • 35
  • 58