Skip to content

Commit 55db313

Browse files
Merge pull request #10 from andreaskueffel/development
add standalone app for syncing and deleting files
2 parents 95926da + edc8550 commit 55db313

30 files changed

Lines changed: 1050 additions & 377 deletions

.github/workflows/createnuget.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ jobs:
3131
with:
3232
dotnet-version: 6.0.x
3333
- name: Restore dependencies
34-
run: dotnet restore
34+
run: dotnet restore FileSyncLibNet
3535
- name: Pack
3636

37-
run: dotnet pack --no-restore --configuration Release -p:Version=${{ steps.version.outputs.version }} -o nuget
37+
run: dotnet pack --no-restore --configuration Release -p:Version=${{ steps.version.outputs.version }} -o nuget FileSyncLibNet/FileSyncLibNet.csproj
3838
- name: Push
3939
if: github.ref == 'refs/heads/main'
4040
run: dotnet nuget push nuget/*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --no-symbols

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,4 @@ FodyWeavers.xsd
397397
# JetBrains Rider
398398
*.sln.iml
399399
/*.sln
400+
/FileSyncLibNet/FileSyncLibNet.sln

FileSyncApp/FileSyncApp.csproj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\FileSyncLibNet\FileSyncLibNet.csproj" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Serialization;
3+
using System.Collections.Generic;
4+
using System.Reflection;
5+
6+
namespace FileSyncApp
7+
{
8+
//short helper class to ignore some properties from serialization
9+
public class IgnorePropertyResolver: DefaultContractResolver
10+
{
11+
private readonly HashSet<string> ignoreProps;
12+
public IgnorePropertyResolver(params string[] propNamesToIgnore)
13+
{
14+
this.ignoreProps = new HashSet<string>(propNamesToIgnore);
15+
}
16+
17+
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
18+
{
19+
JsonProperty property = base.CreateProperty(member, memberSerialization);
20+
if (this.ignoreProps.Contains(property.PropertyName))
21+
{
22+
property.ShouldSerialize = _ => false;
23+
}
24+
return property;
25+
}
26+
}
27+
}
28+

FileSyncApp/Program.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using FileSyncLibNet.Commons;
2+
using FileSyncLibNet.FileCleanJob;
3+
using FileSyncLibNet.FileSyncJob;
4+
using FileSyncLibNet.Logger;
5+
using FileSyncLibNet.SyncProviders;
6+
using Newtonsoft.Json;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.IO;
10+
using System.Text.Json.Serialization;
11+
12+
namespace FileSyncApp
13+
{
14+
internal class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
Console.WriteLine("FileSyncApp - synchronizing folders and clean them up");
19+
Dictionary<string, IFileJobOptions> jobOptions = new Dictionary<string, IFileJobOptions>();
20+
var jsonSettings = new JsonSerializerSettings
21+
{
22+
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
23+
ContractResolver = new IgnorePropertyResolver(new string[] { "Logger" }),
24+
TypeNameHandling = TypeNameHandling.Auto,
25+
};
26+
if (!File.Exists("config.json"))
27+
{
28+
var cleanJob = FileCleanJobOptionsBuilder.CreateBuilder()
29+
.WithDestinationPath("temp")
30+
.WithInterval(TimeSpan.FromMinutes(21))
31+
.WithMaxAge(TimeSpan.FromDays(30))
32+
.WithMinimumFreeSpaceMegabytes(1024 * 30)
33+
.Build();
34+
jobOptions.Add("CleanJob", cleanJob);
35+
36+
var syncFromEdgeToLocal = FileSyncJobOptionsBuilder.CreateBuilder()
37+
.WithSourcePath("\\\\192.168.214.240\\share\\hri\\production")
38+
.WithDestinationPath("temp")
39+
.WithFileSyncProvider(SyncProvider.SMBLib)
40+
.WithSubfolder("left")
41+
.WithSubfolder("right")
42+
.WithCredentials(new System.Net.NetworkCredential("USER", "Password", ""))
43+
.WithInterval(TimeSpan.FromMinutes(10)+TimeSpan.FromSeconds(25))
44+
.SyncRecursive(true)
45+
.Build();
46+
jobOptions.Add("SyncFromEdgeToLocal", syncFromEdgeToLocal);
47+
48+
var syncFromLocalToRemote = FileSyncJobOptionsBuilder.CreateBuilder()
49+
.WithSourcePath("temp")
50+
.WithDestinationPath("Z:\\Serienspektren_Import\\53600002")
51+
.WithFileSyncProvider(SyncProvider.FileIO)
52+
.WithInterval(TimeSpan.FromMinutes(15))
53+
.DeleteAfterBackup(false) //sonst werden die Daten wieder neu von der Edge geholt
54+
.SyncRecursive(true)
55+
.Build();
56+
jobOptions.Add("SyncFromLocalToRemote", syncFromLocalToRemote);
57+
58+
var json = JsonConvert.SerializeObject(jobOptions, Formatting.Indented, jsonSettings);
59+
File.WriteAllText("config.json", json);
60+
}
61+
var readJobOptions = JsonConvert.DeserializeObject<Dictionary<string, IFileJobOptions>>(File.ReadAllText("config.json"), jsonSettings);
62+
List<IFileJob> Jobs = new List<IFileJob>();
63+
foreach(var jobOption in readJobOptions)
64+
{
65+
jobOption.Value.Logger = new StringLogger(new Action<string>((x) => { Console.WriteLine(x); }) );
66+
Jobs.Add( FileSyncJob.CreateJob(jobOption.Value));
67+
}
68+
foreach(var job in Jobs)
69+
{
70+
job.StartJob();
71+
}
72+
Console.WriteLine("Press Enter to exit");
73+
Console.ReadLine();
74+
75+
76+
}
77+
}
78+
79+
}

FileSyncJob/FileSyncJobOptions.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using FileSyncLibNet.Commons;
2+
using FileSyncLibNet.SyncProviders;
3+
using Microsoft.Extensions.Logging;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Net;
7+
8+
namespace FileSyncLibNet.FileCleanJob
9+
{
10+
public abstract class FileJobOptionsBase : IFileJobOptions
11+
{
12+
public NetworkCredential Credentials { get; set; }
13+
public string DestinationPath { get; set; }
14+
public TimeSpan Interval { get; set; } = TimeSpan.Zero;
15+
public ILogger Logger { get; set; }
16+
public string SearchPattern { get; set; } = "*.*";
17+
public List<string> Subfolders { get; set; } = new List<string>();
18+
public bool Recursive { get; set; } = true;
19+
public SyncProvider FileSyncProvider { get; set; } = SyncProvider.FileIO;
20+
}
21+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Threading.Tasks;
5+
using FileSyncLibNet.FileSyncJob;
56

6-
namespace FileSyncLibNet.FileSyncJob
7+
namespace FileSyncLibNet.Commons
78
{
8-
public interface IFileSyncJob
9+
public interface IFileJob
910
{
1011
void StartJob();
1112
void ExecuteNow();
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
using Microsoft.Extensions.Logging;
1+
using FileSyncLibNet.SyncProviders;
2+
using Microsoft.Extensions.Logging;
23
using System;
34
using System.Collections.Generic;
45
using System.Net;
6+
using System.Text;
57

6-
namespace FileSyncLibNet.FileSyncJob
8+
namespace FileSyncLibNet.Commons
79
{
8-
public interface IFileSyncJobOptions
10+
public interface IFileJobOptions
911
{
1012
NetworkCredential Credentials { get; set; }
1113
string DestinationPath { get; set; }
12-
FileSyncProvider FileSyncProvider { get; set; }
14+
SyncProvider FileSyncProvider { get; set; }
1315
TimeSpan Interval { get; set; }
1416
ILogger Logger { get; set; }
1517
bool Recursive { get; set; }
1618
string SearchPattern { get; set; }
1719
List<string> Subfolders { get; set; }
18-
string SourcePath { get; set; }
19-
bool DeleteSourceAfterBackup { get; set; }
20-
bool SyncDeleted { get; set; }
2120
}
22-
}
21+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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

Comments
 (0)