|
| 1 | +using FileSyncLibNet.Commons; |
| 2 | +using Microsoft.Extensions.Logging; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +namespace FileSyncLibNet.FileCleanJob |
| 9 | +{ |
| 10 | + public class FileCleanJob // : IFileJob |
| 11 | + { |
| 12 | + |
| 13 | + |
| 14 | + private readonly Timer TimerCleanup; |
| 15 | + private readonly ILogger log; |
| 16 | + private FileCleanJob(IFileCleanJobOptions fileCleanJobOptions) |
| 17 | + { |
| 18 | + log = fileCleanJobOptions.Logger; |
| 19 | + //TimerCleanup = new Timer(new TimerCallback(CleanUp), null, TimeSpan.FromSeconds(20), fileCleanJobOptions.Interval); |
| 20 | + log.LogInformation("Creating timer for cleanup with interval {A}", fileCleanJobOptions.Interval); |
| 21 | + |
| 22 | + } |
| 23 | + |
| 24 | + public static IFileJob CreateJob(IFileCleanJobOptions fileCleanJobOptions) |
| 25 | + { |
| 26 | + return (IFileJob)new FileCleanJob(fileCleanJobOptions); |
| 27 | + } |
| 28 | + |
| 29 | + //void CleanUp(object state) |
| 30 | + //{ |
| 31 | + // try |
| 32 | + // { |
| 33 | + // Dictionary<string, int> pathMaxDays = new Dictionary<string, int>(); |
| 34 | + // string[] subFolders = { "HriFFTLog", "HriShockLog", "HriLog", "HriDebugLog", "raw" }; |
| 35 | + // var driveInfo = new DriveInfo(Hauptprogramms.First().ProductionDataPath); |
| 36 | + // int days = 59; |
| 37 | + |
| 38 | + // foreach (Hauptprogramm h in Hauptprogramms) |
| 39 | + // { |
| 40 | + // int maxDays = Math.Max(0, h.Einstellungen.BasicSettings["DeleteProductionDataAfterDays"] - 1); |
| 41 | + // var paths = subFolders.Select(x => Path.Combine(h.ProductionDataPath, x)); |
| 42 | + // foreach (var p in paths) |
| 43 | + // pathMaxDays.Add(p, maxDays); |
| 44 | + // } |
| 45 | + // long minimumFreeSpace = Math.Max(2048, Hauptprogramms.Max(x => (int)x.Einstellungen.BasicSettings["MinimumFreeSpace"])) * 1024L * 1024L; |
| 46 | + |
| 47 | + // int fileCount = 0; |
| 48 | + // int hours = 24; |
| 49 | + // bool lowSpace = false; |
| 50 | + // while ((lowSpace = (driveInfo.AvailableFreeSpace < minimumFreeSpace && hours > 1)) || pathMaxDays.Values.Where(x => x <= days).Any()) |
| 51 | + // { |
| 52 | + // List<FileInfo> filesToDelete = new List<FileInfo>(); |
| 53 | + // var timeDiff = new TimeSpan(days, hours, 0, 0, 0); |
| 54 | + // foreach (var pathMaxDay in pathMaxDays) |
| 55 | + // { |
| 56 | + // if (!lowSpace && pathMaxDay.Value > days) |
| 57 | + // continue; |
| 58 | + // try |
| 59 | + // { |
| 60 | + // if (!Directory.Exists(pathMaxDay.Key)) |
| 61 | + // Directory.CreateDirectory(pathMaxDay.Key); |
| 62 | + // DirectoryInfo di = new DirectoryInfo(pathMaxDay.Key); |
| 63 | + // FileInfo[] fi = di.GetFiles(); |
| 64 | + // foreach (FileInfo f in fi) |
| 65 | + // { |
| 66 | + // if (f.LastWriteTime < (DateTime.Now - timeDiff)) |
| 67 | + // { |
| 68 | + // filesToDelete.Add(f); |
| 69 | + // } |
| 70 | + // } |
| 71 | + // } |
| 72 | + // catch (Exception ex) { log.LogError(ex, "exception getting file information of {A}", pathMaxDay.Key); } |
| 73 | + // } |
| 74 | + // if (filesToDelete.Count > 0) |
| 75 | + // log.LogWarning("free space on drive {A} {B} MB is below limit of {C} MB. Deleting {D} files older than {E}", driveInfo.RootDirectory, (driveInfo.AvailableFreeSpace / 1024L / 1024L), (minimumFreeSpace / 1024L / 1024L), filesToDelete.Count, timeDiff); |
| 76 | + // //else |
| 77 | + // // log.LogDebug("free space on drive {A} {B} MB is below limit of {C} MB. No files older than {D} found", driveInfo.RootDirectory, (driveInfo.AvailableFreeSpace / 1024L / 1024L), (minimumFreeSpace / 1024L / 1024L), timeDiff); |
| 78 | + // foreach (var file in filesToDelete) |
| 79 | + // { |
| 80 | + // log.LogDebug("deleting file {A}", file.FullName); |
| 81 | + |
| 82 | + // try |
| 83 | + // { |
| 84 | + // file.Delete(); |
| 85 | + // fileCount++; |
| 86 | + // } |
| 87 | + // catch (Exception ex) { log.LogError(ex, "exception deleting file {A}", file.FullName); } |
| 88 | + // } |
| 89 | + // if (days > 0) |
| 90 | + // days--; |
| 91 | + // else |
| 92 | + // hours--; |
| 93 | + |
| 94 | + // } |
| 95 | + // } |
| 96 | + // catch (Exception exc) |
| 97 | + // { |
| 98 | + // log.LogError(exc, "TimerCleanup_Tick threw an exception"); |
| 99 | + // } |
| 100 | + |
| 101 | + //} |
| 102 | + |
| 103 | + } |
| 104 | +} |
0 commit comments