/Horizon/MainForm.cs |
@@ -10,7 +10,6 @@ |
using System.Reflection; |
using System.Threading; |
using System.Threading.Tasks; |
using System.Threading.Tasks.Dataflow; |
using System.Windows.Forms; |
using Horizon.Database; |
using Horizon.Snapshots; |
@@ -23,7 +22,6 @@ |
using Serilog; |
using TrackedFolders; |
using CaptureMode = Configuration.CaptureMode; |
using Path = System.IO.Path; |
|
namespace Horizon |
{ |
@@ -79,6 +77,8 @@ |
|
public MainForm(Mutex mutex) : this() |
{ |
InitializeComponent(); |
|
_memorySink = new LogMemorySink(); |
|
Log.Logger = new LoggerConfiguration() |
@@ -88,7 +88,7 @@ |
rollingInterval: RollingInterval.Day) |
.CreateLogger(); |
|
_snapshotDatabase = new SnapshotDatabase(_cancellationToken); |
_snapshotDatabase = new SnapshotDatabase(); |
_snapshotDatabase.SnapshotRevert += SnapshotDatabase_SnapshotRevert; |
_snapshotDatabase.SnapshotCreate += SnapshotDatabase_SnapshotCreate; |
|
@@ -111,8 +111,6 @@ |
|
public MainForm() |
{ |
InitializeComponent(); |
|
_cancellationTokenSource = new CancellationTokenSource(); |
_cancellationToken = _cancellationTokenSource.Token; |
|
@@ -410,13 +408,13 @@ |
|
if (_changedFiles.Contains(e.FullPath)) |
{ |
_changedFilesContinuation.Schedule(delay, async () => await TakeSnapshots(color, _cancellationToken), _cancellationToken); |
_changedFilesContinuation.Schedule(delay, () => TakeSnapshots(color), _cancellationToken); |
return; |
} |
|
_changedFiles.Add(e.FullPath); |
|
_changedFilesContinuation.Schedule(delay, async () => await TakeSnapshots(color, _cancellationToken), _cancellationToken); |
_changedFilesContinuation.Schedule(delay, () => TakeSnapshots(color), _cancellationToken); |
} |
catch (Exception exception) |
{ |
@@ -528,7 +526,7 @@ |
color = folder.Color; |
} |
|
await _snapshotDatabase.CreateSnapshotAsync(fileName, file, color, _cancellationToken); |
await _snapshotDatabase.CreateSnapshot(fileName, file, color, _cancellationToken); |
} |
catch (SQLiteException exception) |
{ |
@@ -558,7 +556,7 @@ |
color = folder.Color; |
} |
|
await _snapshotDatabase.CreateSnapshotAsync(fileName, file, color, _cancellationToken); |
await _snapshotDatabase.CreateSnapshot(fileName, file, color, _cancellationToken); |
} |
catch (SQLiteException exception) |
{ |
@@ -734,63 +732,51 @@ |
} |
} |
|
private async Task TakeSnapshots(Color color, CancellationToken cancellationToken) |
private async void TakeSnapshots(Color color) |
{ |
var bufferBlock = new BufferBlock<string>(new DataflowBlockOptions() {CancellationToken = cancellationToken}); |
var actionBlock = new ActionBlock<string>(async path => |
await _changedFilesLock.WaitAsync(_cancellationToken); |
try |
{ |
// In case files have vanished strictly due to the time specified by the tracked folders delay. |
if (!File.Exists(path)) |
foreach (var path in _changedFiles) |
{ |
Log.Warning("File vanished after tracked folder delay.", path); |
// In case files have vanished strictly due to the time specified by the tracked folders delay. |
if (!File.Exists(path)) |
{ |
Log.Warning("File vanished after tracked folder delay.", path); |
|
return; |
} |
continue; |
} |
|
try |
{ |
var fileName = System.IO.Path.GetFileName(path); |
var screenCapture = ScreenCapture.Capture((Utilities.CaptureMode)Configuration.CaptureMode); |
try |
{ |
var fileName = Path.GetFileName(path); |
var screenCapture = ScreenCapture.Capture((Utilities.CaptureMode)Configuration.CaptureMode); |
|
await _snapshotDatabase.CreateSnapshotAsync(fileName, path, screenCapture, color, |
_cancellationToken); |
} |
catch (SQLiteException exception) |
{ |
if (exception.ResultCode == SQLiteErrorCode.Constraint) |
await _snapshotDatabase.CreateSnapshot(fileName, path, screenCapture, color, |
_cancellationToken); |
} |
catch (SQLiteException exception) |
{ |
Log.Information(exception, "Snapshot already exists."); |
if (exception.ResultCode == SQLiteErrorCode.Constraint) |
{ |
Log.Information(exception, "Snapshot already exists."); |
} |
} |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not take snapshot.", path); |
} |
}); |
|
using (var snapshotLink = |
bufferBlock.LinkTo(actionBlock, new DataflowLinkOptions() { PropagateCompletion = true })) |
{ |
await _changedFilesLock.WaitAsync(_cancellationToken); |
try |
{ |
foreach (var path in _changedFiles) |
catch (Exception exception) |
{ |
await bufferBlock.SendAsync(path, cancellationToken); |
Log.Error(exception, "Could not take snapshot.", path); |
} |
bufferBlock.Complete(); |
await bufferBlock.Completion; |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not take snapshots."); |
} |
finally |
{ |
_changedFiles.Clear(); |
_changedFilesLock.Release(); |
} |
} |
catch (Exception exception) |
{ |
Log.Error(exception, "Could not take snapshots."); |
} |
finally |
{ |
_changedFiles.Clear(); |
_changedFilesLock.Release(); |
} |
} |
|
#endregion |