Skip to content

Commit

Permalink
Merge pull request #11 from andreaskueffel/development
Browse files Browse the repository at this point in the history
Add RememberLastSync property to improve performance
  • Loading branch information
andreaskueffel authored Nov 9, 2023
2 parents 55db313 + 17dff11 commit d7b99e4
Show file tree
Hide file tree
Showing 17 changed files with 717 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -398,3 +398,4 @@ FodyWeavers.xsd
*.sln.iml
/*.sln
/FileSyncLibNet/FileSyncLibNet.sln
/FileSyncAppWin/pub
8 changes: 7 additions & 1 deletion FileSyncApp/FileSyncApp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand All @@ -7,6 +7,12 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="System.Runtime.InteropServices" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
103 changes: 88 additions & 15 deletions FileSyncApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,60 @@
using FileSyncLibNet.FileSyncJob;
using FileSyncLibNet.Logger;
using FileSyncLibNet.SyncProviders;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Serilog;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.Json.Serialization;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using File = System.IO.File;

namespace FileSyncApp
{
internal class Program
public class Program
{
static void Main(string[] args)
public static event EventHandler JobsReady;
public static volatile bool keepRunning = true;
public static LoggingLevelSwitch LoggingLevel { get; private set; }
public static ILoggerFactory LoggerFactory { get; private set; }
public static Dictionary<string, IFileJob> Jobs = new Dictionary<string, IFileJob>();
public static void Main(string[] args)
{





LoggingLevel= new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Information);
#if DEBUG
LoggingLevel= new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Verbose);
#endif
ConfigureLogger();
LoggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { builder.AddSerilog(Serilog.Log.Logger); });
if (null!=args && args.Length > 0)
{
if (args.Contains("debug"))
{
LoggingLevel.MinimumLevel = Serilog.Events.LogEventLevel.Verbose;
}
}
Console.CancelKeyPress += (s, e) => { keepRunning = false; e.Cancel = true; };
RunProgram();

}


static void RunProgram()
{
Console.WriteLine("FileSyncApp - synchronizing folders and clean them up");
Dictionary<string, IFileJobOptions> jobOptions = new Dictionary<string, IFileJobOptions>();
Expand All @@ -34,20 +77,22 @@ static void Main(string[] args)
jobOptions.Add("CleanJob", cleanJob);

var syncFromEdgeToLocal = FileSyncJobOptionsBuilder.CreateBuilder()
.WithSourcePath("\\\\192.168.214.240\\share\\hri\\production")
.WithSourcePath("\\\\edgeip\\share\\service\\production")
.WithDestinationPath("temp")
.WithFileSyncProvider(SyncProvider.SMBLib)
.WithSubfolder("left")
.WithSubfolder("right")
.WithCredentials(new System.Net.NetworkCredential("USER", "Password", ""))
.WithInterval(TimeSpan.FromMinutes(10)+TimeSpan.FromSeconds(25))
.WithInterval(TimeSpan.FromMinutes(10) + TimeSpan.FromSeconds(25))
.SyncRecursive(true)
.Build();
jobOptions.Add("SyncFromEdgeToLocal", syncFromEdgeToLocal);


var hostname = Dns.GetHostName();

var syncFromLocalToRemote = FileSyncJobOptionsBuilder.CreateBuilder()
.WithSourcePath("temp")
.WithDestinationPath("Z:\\Serienspektren_Import\\53600002")
.WithDestinationPath("\\\\SERVER\\Share\\Subfolder\\" + hostname)
.WithFileSyncProvider(SyncProvider.FileIO)
.WithInterval(TimeSpan.FromMinutes(15))
.DeleteAfterBackup(false) //sonst werden die Daten wieder neu von der Edge geholt
Expand All @@ -59,21 +104,49 @@ static void Main(string[] args)
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)
//List<IFileJob> Jobs = new List<IFileJob>();

foreach (var jobOption in readJobOptions)
{

jobOption.Value.Logger = LoggerFactory.CreateLogger(jobOption.Key);
Jobs.Add(jobOption.Key, FileSyncJob.CreateJob(jobOption.Value));
}
JobsReady?.Invoke(null, EventArgs.Empty);
foreach (var job in Jobs)
{
jobOption.Value.Logger = new StringLogger(new Action<string>((x) => { Console.WriteLine(x); }) );
Jobs.Add( FileSyncJob.CreateJob(jobOption.Value));
job.Value.StartJob();
}
foreach(var job in Jobs)
Console.WriteLine("Press Ctrl+C to exit");
while (keepRunning)
{
job.StartJob();
Thread.Sleep(200);
}
Console.WriteLine("Press Enter to exit");
Console.ReadLine();
}


private static void ConfigureLogger()
{
string serilogFileTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext:l} {Message:lj}{NewLine}{Exception}";
string serilogConsoleTemplate = "{Timestamp:HH:mm:ss.fff}[{Level:u3}]{SourceContext:l} {Message:lj}{NewLine}{Exception}";
Serilog.Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.WriteTo.Async(asyncWriteTo => asyncWriteTo.Console(outputTemplate: serilogConsoleTemplate), blockWhenFull: true)
.WriteTo.Async(asyncWriteTo => asyncWriteTo.File(
("FileSyncApp.log"),
retainedFileCountLimit: 10,
fileSizeLimitBytes: 1024 * 1024 * 100,
rollingInterval: RollingInterval.Day,
outputTemplate: serilogFileTemplate),
blockWhenFull: true)
.MinimumLevel.ControlledBy(LoggingLevel)
.CreateLogger();


}



}

}
15 changes: 15 additions & 0 deletions FileSyncAppWin/FileSyncAppWin.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>

<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FileSyncApp\FileSyncApp.csproj" />
</ItemGroup>

</Project>
70 changes: 70 additions & 0 deletions FileSyncAppWin/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions FileSyncAppWin/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using FileSyncLibNet.Commons;

namespace FileSyncAppWin
{
public partial class MainForm : Form
{
Thread consoleThread;

public MainForm()
{
InitializeComponent();
this.FormClosing += (s, e) => { FileSyncApp.Program.keepRunning = false; consoleThread?.Join(10_000); };
consoleThread = new Thread(() =>
{
FileSyncApp.Program.Main(null);
});
FileSyncApp.Program.JobsReady += (s,e)=> {
foreach(var job in FileSyncApp.Program.Jobs)
{
job.Value.JobStarted += (j, text) =>
{
NewLogOutput($"{job.Key} STARTED - {text.Status} {text.Exception}");
};
job.Value.JobFinished += (j, text) =>
{
NewLogOutput($"{job.Key} FINISHED - {text.Status} {text.Exception}");
};
job.Value.JobError += (j, text) =>
{
NewLogOutput($"{job.Key} ERROR - {text.Status} {text.Exception}");
};
}
};
consoleThread.Start();
this.Resize += ((s, e) =>
{
this.SuspendLayout();
this.BeginInvoke(() =>
{
if (WindowState == FormWindowState.Minimized && ShowInTaskbar)
{
ShowInTaskbar = false;
notifyIcon1.ShowBalloonTip(10_000, "FileSyncAppWin", "I am here", ToolTipIcon.Info);
notifyIcon1.Visible = true;
}
});
this.ResumeLayout();
});
notifyIcon1.Click += (s, e) =>
{
this.BeginInvoke(() => { WindowState = FormWindowState.Normal; ShowInTaskbar = true; });
};
notifyIcon1.BalloonTipClicked += (s, e) => { this.BeginInvoke(() => { WindowState = FormWindowState.Normal; ShowInTaskbar = true; }); };
//notifyIcon1.BalloonTipShown += (s, e) => { ShowInTaskbar = false; };

//this.WindowState = FormWindowState.Minimized;
}




private void NewLogOutput(string e)
{

this.BeginInvoke(() => { textBox1.Text = string.Join(Environment.NewLine, (new string[] { $"{DateTime.Now.ToString("HH:mm:ss.fff")} {e}" }).Concat(textBox1.Text.Split(Environment.NewLine).Take(1000))); });

}
}
}
Loading

0 comments on commit d7b99e4

Please sign in to comment.