-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from andreaskueffel/development
add standalone app for syncing and deleting files
- Loading branch information
Showing
30 changed files
with
1,050 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,3 +397,4 @@ FodyWeavers.xsd | |
# JetBrains Rider | ||
*.sln.iml | ||
/*.sln | ||
/FileSyncLibNet/FileSyncLibNet.sln |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FileSyncLibNet\FileSyncLibNet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace FileSyncApp | ||
{ | ||
//short helper class to ignore some properties from serialization | ||
public class IgnorePropertyResolver: DefaultContractResolver | ||
{ | ||
private readonly HashSet<string> ignoreProps; | ||
public IgnorePropertyResolver(params string[] propNamesToIgnore) | ||
{ | ||
this.ignoreProps = new HashSet<string>(propNamesToIgnore); | ||
} | ||
|
||
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization) | ||
{ | ||
JsonProperty property = base.CreateProperty(member, memberSerialization); | ||
if (this.ignoreProps.Contains(property.PropertyName)) | ||
{ | ||
property.ShouldSerialize = _ => false; | ||
} | ||
return property; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using FileSyncLibNet.Commons; | ||
using FileSyncLibNet.FileCleanJob; | ||
using FileSyncLibNet.FileSyncJob; | ||
using FileSyncLibNet.Logger; | ||
using FileSyncLibNet.SyncProviders; | ||
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace FileSyncApp | ||
{ | ||
internal class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
Console.WriteLine("FileSyncApp - synchronizing folders and clean them up"); | ||
Dictionary<string, IFileJobOptions> jobOptions = new Dictionary<string, IFileJobOptions>(); | ||
var jsonSettings = new JsonSerializerSettings | ||
{ | ||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, | ||
ContractResolver = new IgnorePropertyResolver(new string[] { "Logger" }), | ||
TypeNameHandling = TypeNameHandling.Auto, | ||
}; | ||
if (!File.Exists("config.json")) | ||
{ | ||
var cleanJob = FileCleanJobOptionsBuilder.CreateBuilder() | ||
.WithDestinationPath("temp") | ||
.WithInterval(TimeSpan.FromMinutes(21)) | ||
.WithMaxAge(TimeSpan.FromDays(30)) | ||
.WithMinimumFreeSpaceMegabytes(1024 * 30) | ||
.Build(); | ||
jobOptions.Add("CleanJob", cleanJob); | ||
|
||
var syncFromEdgeToLocal = FileSyncJobOptionsBuilder.CreateBuilder() | ||
.WithSourcePath("\\\\192.168.214.240\\share\\hri\\production") | ||
.WithDestinationPath("temp") | ||
.WithFileSyncProvider(SyncProvider.SMBLib) | ||
.WithSubfolder("left") | ||
.WithSubfolder("right") | ||
.WithCredentials(new System.Net.NetworkCredential("USER", "Password", "")) | ||
.WithInterval(TimeSpan.FromMinutes(10)+TimeSpan.FromSeconds(25)) | ||
.SyncRecursive(true) | ||
.Build(); | ||
jobOptions.Add("SyncFromEdgeToLocal", syncFromEdgeToLocal); | ||
|
||
var syncFromLocalToRemote = FileSyncJobOptionsBuilder.CreateBuilder() | ||
.WithSourcePath("temp") | ||
.WithDestinationPath("Z:\\Serienspektren_Import\\53600002") | ||
.WithFileSyncProvider(SyncProvider.FileIO) | ||
.WithInterval(TimeSpan.FromMinutes(15)) | ||
.DeleteAfterBackup(false) //sonst werden die Daten wieder neu von der Edge geholt | ||
.SyncRecursive(true) | ||
.Build(); | ||
jobOptions.Add("SyncFromLocalToRemote", syncFromLocalToRemote); | ||
|
||
var json = JsonConvert.SerializeObject(jobOptions, Formatting.Indented, jsonSettings); | ||
File.WriteAllText("config.json", json); | ||
} | ||
var readJobOptions = JsonConvert.DeserializeObject<Dictionary<string, IFileJobOptions>>(File.ReadAllText("config.json"), jsonSettings); | ||
List<IFileJob> Jobs = new List<IFileJob>(); | ||
foreach(var jobOption in readJobOptions) | ||
{ | ||
jobOption.Value.Logger = new StringLogger(new Action<string>((x) => { Console.WriteLine(x); }) ); | ||
Jobs.Add( FileSyncJob.CreateJob(jobOption.Value)); | ||
} | ||
foreach(var job in Jobs) | ||
{ | ||
job.StartJob(); | ||
} | ||
Console.WriteLine("Press Enter to exit"); | ||
Console.ReadLine(); | ||
|
||
|
||
} | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using FileSyncLibNet.Commons; | ||
using FileSyncLibNet.SyncProviders; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace FileSyncLibNet.FileCleanJob | ||
{ | ||
public abstract class FileJobOptionsBase : IFileJobOptions | ||
{ | ||
public NetworkCredential Credentials { get; set; } | ||
public string DestinationPath { get; set; } | ||
public TimeSpan Interval { get; set; } = TimeSpan.Zero; | ||
public ILogger Logger { get; set; } | ||
public string SearchPattern { get; set; } = "*.*"; | ||
public List<string> Subfolders { get; set; } = new List<string>(); | ||
public bool Recursive { get; set; } = true; | ||
public SyncProvider FileSyncProvider { get; set; } = SyncProvider.FileIO; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 7 additions & 8 deletions
15
FileSyncJob/IFileSyncJobOptions.cs → FileSyncLibNet/Commons/IFileJobOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
using Microsoft.Extensions.Logging; | ||
using FileSyncLibNet.SyncProviders; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace FileSyncLibNet.FileSyncJob | ||
namespace FileSyncLibNet.Commons | ||
{ | ||
public interface IFileSyncJobOptions | ||
public interface IFileJobOptions | ||
{ | ||
NetworkCredential Credentials { get; set; } | ||
string DestinationPath { get; set; } | ||
FileSyncProvider FileSyncProvider { get; set; } | ||
SyncProvider FileSyncProvider { get; set; } | ||
TimeSpan Interval { get; set; } | ||
ILogger Logger { get; set; } | ||
bool Recursive { get; set; } | ||
string SearchPattern { get; set; } | ||
List<string> Subfolders { get; set; } | ||
string SourcePath { get; set; } | ||
bool DeleteSourceAfterBackup { get; set; } | ||
bool SyncDeleted { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using FileSyncLibNet.Commons; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Threading; | ||
|
||
namespace FileSyncLibNet.FileCleanJob | ||
{ | ||
public class FileCleanJob // : IFileJob | ||
{ | ||
|
||
|
||
private readonly Timer TimerCleanup; | ||
private readonly ILogger log; | ||
private FileCleanJob(IFileCleanJobOptions fileCleanJobOptions) | ||
{ | ||
log = fileCleanJobOptions.Logger; | ||
//TimerCleanup = new Timer(new TimerCallback(CleanUp), null, TimeSpan.FromSeconds(20), fileCleanJobOptions.Interval); | ||
log.LogInformation("Creating timer for cleanup with interval {A}", fileCleanJobOptions.Interval); | ||
|
||
} | ||
|
||
public static IFileJob CreateJob(IFileCleanJobOptions fileCleanJobOptions) | ||
{ | ||
return (IFileJob)new FileCleanJob(fileCleanJobOptions); | ||
} | ||
|
||
//void CleanUp(object state) | ||
//{ | ||
// try | ||
// { | ||
// Dictionary<string, int> pathMaxDays = new Dictionary<string, int>(); | ||
// string[] subFolders = { "HriFFTLog", "HriShockLog", "HriLog", "HriDebugLog", "raw" }; | ||
// var driveInfo = new DriveInfo(Hauptprogramms.First().ProductionDataPath); | ||
// int days = 59; | ||
|
||
// foreach (Hauptprogramm h in Hauptprogramms) | ||
// { | ||
// int maxDays = Math.Max(0, h.Einstellungen.BasicSettings["DeleteProductionDataAfterDays"] - 1); | ||
// var paths = subFolders.Select(x => Path.Combine(h.ProductionDataPath, x)); | ||
// foreach (var p in paths) | ||
// pathMaxDays.Add(p, maxDays); | ||
// } | ||
// long minimumFreeSpace = Math.Max(2048, Hauptprogramms.Max(x => (int)x.Einstellungen.BasicSettings["MinimumFreeSpace"])) * 1024L * 1024L; | ||
|
||
// int fileCount = 0; | ||
// int hours = 24; | ||
// bool lowSpace = false; | ||
// while ((lowSpace = (driveInfo.AvailableFreeSpace < minimumFreeSpace && hours > 1)) || pathMaxDays.Values.Where(x => x <= days).Any()) | ||
// { | ||
// List<FileInfo> filesToDelete = new List<FileInfo>(); | ||
// var timeDiff = new TimeSpan(days, hours, 0, 0, 0); | ||
// foreach (var pathMaxDay in pathMaxDays) | ||
// { | ||
// if (!lowSpace && pathMaxDay.Value > days) | ||
// continue; | ||
// try | ||
// { | ||
// if (!Directory.Exists(pathMaxDay.Key)) | ||
// Directory.CreateDirectory(pathMaxDay.Key); | ||
// DirectoryInfo di = new DirectoryInfo(pathMaxDay.Key); | ||
// FileInfo[] fi = di.GetFiles(); | ||
// foreach (FileInfo f in fi) | ||
// { | ||
// if (f.LastWriteTime < (DateTime.Now - timeDiff)) | ||
// { | ||
// filesToDelete.Add(f); | ||
// } | ||
// } | ||
// } | ||
// catch (Exception ex) { log.LogError(ex, "exception getting file information of {A}", pathMaxDay.Key); } | ||
// } | ||
// if (filesToDelete.Count > 0) | ||
// 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); | ||
// //else | ||
// // 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); | ||
// foreach (var file in filesToDelete) | ||
// { | ||
// log.LogDebug("deleting file {A}", file.FullName); | ||
|
||
// try | ||
// { | ||
// file.Delete(); | ||
// fileCount++; | ||
// } | ||
// catch (Exception ex) { log.LogError(ex, "exception deleting file {A}", file.FullName); } | ||
// } | ||
// if (days > 0) | ||
// days--; | ||
// else | ||
// hours--; | ||
|
||
// } | ||
// } | ||
// catch (Exception exc) | ||
// { | ||
// log.LogError(exc, "TimerCleanup_Tick threw an exception"); | ||
// } | ||
|
||
//} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using FileSyncLibNet.SyncProviders; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
|
||
namespace FileSyncLibNet.FileCleanJob | ||
{ | ||
public class FileCleanJobOptions : FileJobOptionsBase, IFileCleanJobOptions | ||
{ | ||
public TimeSpan MaxAge { get; set; } | ||
public long MinimumFreeSpaceMegabyte { get; set; } | ||
} | ||
} |
Oops, something went wrong.