Skip to content

Commit

Permalink
Merge pull request #10 from andreaskueffel/development
Browse files Browse the repository at this point in the history
add standalone app for syncing and deleting files
  • Loading branch information
andreaskueffel authored Nov 1, 2023
2 parents 95926da + edc8550 commit 55db313
Show file tree
Hide file tree
Showing 30 changed files with 1,050 additions and 377 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/createnuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ jobs:
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
run: dotnet restore FileSyncLibNet
- name: Pack

run: dotnet pack --no-restore --configuration Release -p:Version=${{ steps.version.outputs.version }} -o nuget
run: dotnet pack --no-restore --configuration Release -p:Version=${{ steps.version.outputs.version }} -o nuget FileSyncLibNet/FileSyncLibNet.csproj
- name: Push
if: github.ref == 'refs/heads/main'
run: dotnet nuget push nuget/*.nupkg --skip-duplicate --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --no-symbols
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,4 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
/*.sln
/FileSyncLibNet/FileSyncLibNet.sln
16 changes: 16 additions & 0 deletions FileSyncApp/FileSyncApp.csproj
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>
28 changes: 28 additions & 0 deletions FileSyncApp/IgnorePropertyResolver.cs
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;
}
}
}

79 changes: 79 additions & 0 deletions FileSyncApp/Program.cs
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();


}
}

}
28 changes: 0 additions & 28 deletions FileSyncJob/FileSyncJobOptions.cs

This file was deleted.

21 changes: 21 additions & 0 deletions FileSyncLibNet/Commons/FileJobOptionsBase.cs
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using FileSyncLibNet.FileSyncJob;

namespace FileSyncLibNet.FileSyncJob
namespace FileSyncLibNet.Commons
{
public interface IFileSyncJob
public interface IFileJob
{
void StartJob();
void ExecuteNow();
Expand Down
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; }
}
}
}
104 changes: 104 additions & 0 deletions FileSyncLibNet/FileCleanJob/FileCleanJob.cs
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;

Check warning on line 14 in FileSyncLibNet/FileCleanJob/FileCleanJob.cs

View workflow job for this annotation

GitHub Actions / build

The field 'FileCleanJob.TimerCleanup' is never used
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");
// }

//}

}
}
14 changes: 14 additions & 0 deletions FileSyncLibNet/FileCleanJob/FileCleanJobOptions.cs
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; }
}
}
Loading

0 comments on commit 55db313

Please sign in to comment.