diff --git a/ZeroLevel/Services/FileSystem/FSUtils.cs b/ZeroLevel/Services/FileSystem/FSUtils.cs index 6333b70..2bdea1c 100644 --- a/ZeroLevel/Services/FileSystem/FSUtils.cs +++ b/ZeroLevel/Services/FileSystem/FSUtils.cs @@ -178,7 +178,9 @@ namespace ZeroLevel.Services.FileSystem } } if (Directory.Exists(targetFolder) == false) + { Directory.CreateDirectory(targetFolder); + } var tmpZip = Path.Combine(Configuration.BaseDirectory, "temp", Path.GetRandomFileName()); var tmp = Directory.CreateDirectory(tmpZip); var zipFile = Path.Combine(tmp.FullName, "zip.zip"); @@ -239,8 +241,9 @@ namespace ZeroLevel.Services.FileSystem deleted = true; } } - catch + catch(Exception ex) { + Log.SystemError(ex, $"[FSUtils.RemoveFolder] Fault remove folder {path}"); try_counter++; Thread.Sleep(fault_timeout_period); } diff --git a/ZeroLevel/Services/Utils/Clock.cs b/ZeroLevel/Services/Utils/Clock.cs new file mode 100644 index 0000000..c561b06 --- /dev/null +++ b/ZeroLevel/Services/Utils/Clock.cs @@ -0,0 +1,43 @@ +using System; +using System.Diagnostics; +using System.Threading; + +namespace ZeroLevel.Services.Utils +{ + public sealed class Clock + : IDisposable + { + private readonly long _maxIdleTime = TimeSpan.FromSeconds(10).Ticks; + private const long TicksMultiplier = 1000 * TimeSpan.TicksPerMillisecond; + + private readonly ThreadLocal _startTime = + new ThreadLocal(() => DateTime.UtcNow, false); + + private readonly ThreadLocal _startTimestamp = + new ThreadLocal(() => Stopwatch.GetTimestamp(), false); + + public DateTime UtcNow + { + get + { + double endTimestamp = Stopwatch.GetTimestamp(); + + var durationInTicks = (endTimestamp - _startTimestamp.Value) / Stopwatch.Frequency * TicksMultiplier; + if (durationInTicks >= _maxIdleTime) + { + _startTimestamp.Value = Stopwatch.GetTimestamp(); + _startTime.Value = DateTime.UtcNow; + return _startTime.Value; + } + + return _startTime.Value.AddTicks((long)durationInTicks); + } + } + + public void Dispose() + { + _startTime.Dispose(); + _startTimestamp.Dispose(); + } + } +}