CreatePointerFile and more on NoWaitDirectory
This commit is contained in:
		| @ -7,6 +7,7 @@ using System; | |||||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||||
| using System.IO; | using System.IO; | ||||||
| using System.Linq; | using System.Linq; | ||||||
|  | using System.Text; | ||||||
| using System.Text.Json; | using System.Text.Json; | ||||||
| using System.Threading; | using System.Threading; | ||||||
|  |  | ||||||
| @ -153,9 +154,9 @@ public class FileRead : Shared.FileRead, IFileRead | |||||||
|         return results; |         return results; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private void MoveCollection(DateTime dateTime, List<(string matchingFile, string checkFile)> collection) |     private static List<(string, string, string, string, string)> GetCollection(List<(string matchingFile, string checkFile)> collection) | ||||||
|     { |     { | ||||||
|         long preWait; |         List<(string, string, string, string, string)> results = new(); | ||||||
|         string errFile; |         string errFile; | ||||||
|         string checkDirectory; |         string checkDirectory; | ||||||
|         string noWaitDirectory; |         string noWaitDirectory; | ||||||
| @ -165,10 +166,24 @@ public class FileRead : Shared.FileRead, IFileRead | |||||||
|             checkDirectory = Path.GetDirectoryName(checkFile); |             checkDirectory = Path.GetDirectoryName(checkFile); | ||||||
|             if (!Directory.Exists(checkDirectory)) |             if (!Directory.Exists(checkDirectory)) | ||||||
|                 _ = Directory.CreateDirectory(checkDirectory); |                 _ = Directory.CreateDirectory(checkDirectory); | ||||||
|             File.Move(matchingFile, checkFile); |  | ||||||
|             noWaitDirectory = Path.Combine(checkDirectory, "NoWaitDirectory"); |             noWaitDirectory = Path.Combine(checkDirectory, "NoWaitDirectory"); | ||||||
|  |             results.Add(new(matchingFile, checkFile, errFile, checkDirectory, noWaitDirectory)); | ||||||
|  |         } | ||||||
|  |         return results; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private void MoveCollection(DateTime dateTime, List<(string matchingFile, string checkFile)> collection) | ||||||
|  |     { | ||||||
|  |         long preWait; | ||||||
|  |         List<(string checkFile, string errFile)> postCollection = new(); | ||||||
|  |         foreach ((string matchingFile, string checkFile, string errFile, string checkDirectory, string noWaitDirectory) in GetCollection(collection)) | ||||||
|  |         { | ||||||
|  |             File.Move(matchingFile, checkFile); | ||||||
|             if (Directory.Exists(noWaitDirectory)) |             if (Directory.Exists(noWaitDirectory)) | ||||||
|  |             { | ||||||
|  |                 postCollection.Add(new(checkFile, errFile)); | ||||||
|                 continue; |                 continue; | ||||||
|  |             } | ||||||
|             if (_FileConnectorConfiguration?.FileHandleWaitTime is null) |             if (_FileConnectorConfiguration?.FileHandleWaitTime is null) | ||||||
|                 preWait = DateTime.Now.AddMilliseconds(1234).Ticks; |                 preWait = DateTime.Now.AddMilliseconds(1234).Ticks; | ||||||
|             else |             else | ||||||
| @ -186,10 +201,45 @@ public class FileRead : Shared.FileRead, IFileRead | |||||||
|                 if (!File.Exists(checkFile)) |                 if (!File.Exists(checkFile)) | ||||||
|                     break; |                     break; | ||||||
|                 if (new TimeSpan(DateTime.Now.Ticks - dateTime.Ticks).TotalSeconds > _BreakAfterSeconds) |                 if (new TimeSpan(DateTime.Now.Ticks - dateTime.Ticks).TotalSeconds > _BreakAfterSeconds) | ||||||
|                     throw new Exception($"Not all files were consumned after {_BreakAfterSeconds} second(s)!"); |                     throw new Exception($"Not all files were consumed after {_BreakAfterSeconds} second(s)!"); | ||||||
|                 Thread.Sleep(500); |                 Thread.Sleep(500); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |         if (postCollection.Any()) | ||||||
|  |         { | ||||||
|  |             Thread.Sleep(500); | ||||||
|  |             StringBuilder stringBuilder = new(); | ||||||
|  |             foreach ((string checkFile, string errFile) in postCollection) | ||||||
|  |             { | ||||||
|  |                 if (File.Exists(errFile)) | ||||||
|  |                     _ = stringBuilder.AppendLine(File.ReadAllText(errFile)); | ||||||
|  |                 if (File.Exists(checkFile)) | ||||||
|  |                     _ = stringBuilder.AppendLine($"<{checkFile}> was not consumed by the end!"); | ||||||
|  |             } | ||||||
|  |             if (stringBuilder.Length > 0) | ||||||
|  |                 throw new Exception(stringBuilder.ToString()); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private static void CreatePointerFile(int numberLength, string parentDirectory, List<string> matchingFiles) | ||||||
|  |     { | ||||||
|  | #nullable enable | ||||||
|  |         string checkFile; | ||||||
|  |         string writeFile; | ||||||
|  |         string? directoryName; | ||||||
|  |         int parentDirectoryLength = parentDirectory.Length; | ||||||
|  |         foreach (string matchingFile in matchingFiles) | ||||||
|  |         { | ||||||
|  |             directoryName = Path.GetDirectoryName(matchingFile); | ||||||
|  |             if (directoryName is null) | ||||||
|  |                 continue; | ||||||
|  |             checkFile = $"{matchingFile[0]}{directoryName.Substring(parentDirectoryLength + numberLength + 1)}"; | ||||||
|  |             writeFile = Path.Combine(parentDirectory, $"{directoryName.Substring(parentDirectory.Length + 1, numberLength)}.txt"); | ||||||
|  |             if (File.Exists(writeFile)) | ||||||
|  |                 continue; | ||||||
|  |             File.AppendAllLines(writeFile, new string[] { parentDirectory, matchingFile, directoryName, checkFile }); | ||||||
|  |         } | ||||||
|  | #nullable disable | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime) |     private Tuple<string, Test[], JsonElement[], List<FileInfo>> GetExtractResult(string reportFullPath, DateTime dateTime) | ||||||
| @ -205,6 +255,9 @@ public class FileRead : Shared.FileRead, IFileRead | |||||||
|         List<string> matchingFiles = GetMatchingFiles(ticks, reportFullPath, searchDirectories); |         List<string> matchingFiles = GetMatchingFiles(ticks, reportFullPath, searchDirectories); | ||||||
|         if (matchingFiles.Count != searchDirectories.Count) |         if (matchingFiles.Count != searchDirectories.Count) | ||||||
|             throw new Exception($"Didn't find all files after {_BreakAfterSeconds} second(s)!"); |             throw new Exception($"Didn't find all files after {_BreakAfterSeconds} second(s)!"); | ||||||
|  |         try | ||||||
|  |         { CreatePointerFile(numberLength, parentParentDirectory, matchingFiles); } | ||||||
|  |         catch (Exception) { } | ||||||
|         List<(string matchingFile, string checkFile)> collection = GetCollection(numberLength, parentParentDirectory, matchingFiles); |         List<(string matchingFile, string checkFile)> collection = GetCollection(numberLength, parentParentDirectory, matchingFiles); | ||||||
|         MoveCollection(dateTime, collection); |         MoveCollection(dateTime, collection); | ||||||
|         return results; |         return results; | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user