Skip to content

Commit 950034b

Browse files
committed
Refactor Watcher functionality
- added manual running capability to link in with scheduler (personal note: scheduler is still in a separate unreferenced project) - removed duplication of checks from BackgroundCompactor
1 parent 5d010c2 commit 950034b

File tree

4 files changed

+64
-37
lines changed

4 files changed

+64
-37
lines changed

CompactGUI.Watcher/BackgroundCompactor.vb

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,13 @@ Public Class BackgroundCompactor
3232

3333
Private ReadOnly _logger As ILogger(Of Watcher)
3434

35-
Private ReadOnly _idleSettings As IdleSettings
3635

3736
Public Event IsCompactingEvent As EventHandler(Of Boolean)
3837

39-
Public Sub New(excludedFileTypes As String(), logger As ILogger(Of Watcher), settings As IdleSettings)
38+
Public Sub New(excludedFileTypes As String(), logger As ILogger(Of Watcher))
4039

4140
_excludedFileTypes = excludedFileTypes
4241
_logger = logger
43-
_idleSettings = settings
44-
4542
End Sub
4643

4744

@@ -55,7 +52,7 @@ Public Class BackgroundCompactor
5552

5653
End Function
5754

58-
Public Async Function StartCompactingAsync(folders As ObservableCollection(Of WatchedFolder)) As Task(Of Boolean)
55+
Public Async Function StartCompactingAsync(folders As IEnumerable(Of WatchedFolder)) As Task(Of Boolean)
5956
WatcherLog.BackgroundCompactingStarted(_logger)
6057
cancellationTokenSource = New CancellationTokenSource()
6158

@@ -64,31 +61,14 @@ Public Class BackgroundCompactor
6461
Dim currentProcess As Process = Process.GetCurrentProcess()
6562
currentProcess.PriorityClass = ProcessPriorityClass.Idle
6663

67-
Dim foldersCopy As List(Of WatchedFolder) = folders.Where(Function(f) f.DecayPercentage <> 0 AndAlso f.CompressionLevel <> Core.WOFCompressionAlgorithm.NO_COMPRESSION).ToList()
68-
64+
isCompacting = True
6965

70-
For Each folder In foldersCopy
66+
For Each folder In folders.ToList
7167
folder.IsWorking = True
72-
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-_idleSettings.LastSystemModifiedTimeThresholdSeconds)
73-
If folder.LastSystemModifiedDate > recentThresholdDate Then
74-
WatcherLog.SkippingRecentlyModifiedFolder(_logger, folder.DisplayName)
75-
Continue For
76-
End If
7768

7869
WatcherLog.CompactingFolder(_logger, folder.DisplayName)
7970
Dim compactingTask = BeginCompacting(folder.Folder, folder.CompressionLevel)
80-
isCompacting = True
81-
82-
'While Not cancellationToken.IsCancellationRequested AndAlso Not compactingTask.IsCompleted AndAlso Not compactingTask.IsCanceled
83-
' Dim ret = Await compactingTask
84-
85-
'' Check the idle state and adjust compacting status accordingly
86-
'If Not isSystemIdle AndAlso Not isCompactingPaused Then
87-
' PauseCompacting()
88-
'ElseIf isSystemIdle AndAlso isCompactingPaused Then
89-
' ResumeCompacting()
90-
'End If
91-
'End While
71+
9272

9373
If cancellationTokenSource.IsCancellationRequested Then
9474
Trace.WriteLine("Compacting cancelled by user.")

CompactGUI.Watcher/WatchedFolder.vb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ Public Class WatchedFolder
6464
End If
6565
End Sub
6666

67+
Public Sub PauseMonitoring()
68+
If FSWatcher IsNot Nothing Then
69+
FSWatcher.EnableRaisingEvents = False
70+
End If
71+
End Sub
72+
73+
Public Sub ResumeMonitoring()
74+
If FSWatcher IsNot Nothing Then
75+
FSWatcher.EnableRaisingEvents = True
76+
End If
77+
End Sub
6778

6879
' --- Monitoring Events ---
6980
Private Sub WatcherErrorEvent(sender As Object, e As ErrorEventArgs) Handles FSWatcher.Error
@@ -79,6 +90,7 @@ Public Class WatchedFolder
7990
LastChangedDate = DateTime.Now
8091
OnPropertyChanged(NameOf(HasTargetChanged))
8192
OnPropertyChanged(NameOf(LastChangedDate))
93+
OnPropertyChanged(NameOf(LastSystemModifiedDate))
8294
Debug.WriteLine("Folder " & Folder & " has changed!")
8395
End Sub
8496

CompactGUI.Watcher/Watcher.vb

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Imports System.Collections.ObjectModel
22
Imports System.Collections.Specialized
3+
Imports System.Runtime
34
Imports System.Text.Json
45
Imports System.Threading
56

@@ -63,7 +64,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
6364
AddHandler WatchedFolders.CollectionChanged, AddressOf WatchedFolders_CollectionChanged
6465

6566

66-
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger, IdleSettings)
67+
BGCompactor = New BackgroundCompactor(excludedFiletypes, _logger)
6768

6869

6970
AddHandler BGCompactor.IsCompactingEvent, Sub(sender, isCompacting)
@@ -83,24 +84,46 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
8384
WatcherLog.SystemIdleDetected(_logger)
8485
BGCompactor.ResumeCompacting()
8586

87+
Await RunWatcher(False)
88+
89+
End Sub
90+
91+
<RelayCommand>
92+
Public Async Function RunWatcher() As Task(Of Boolean)
93+
Return Await RunWatcher(True)
94+
End Function
95+
96+
Public Async Function RunWatcher(Optional runAll As Boolean = True) As Task(Of Boolean)
97+
_logger.LogDebug("RunWatcher called")
8698
RemoveHandler IdleDetector.IsIdle, _idleHandler
8799

100+
For Each watcher In WatchedFolders
101+
watcher.PauseMonitoring()
102+
Next
103+
88104
Try
89-
If Not IsWatchingEnabled Then Return
105+
If Not IsWatchingEnabled Then Return False
90106
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
91-
If WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return
107+
If Not runAll AndAlso WatchedFolders.Any(Function(x) x.LastChangedDate > recentThresholdDate) Then Return False
92108

93109
If _parseWatchersSemaphore.CurrentCount <> 0 Then
94-
Await ParseWatchers()
110+
Await ParseWatchers(runAll)
95111
End If
96-
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso IsBackgroundCompactingEnabled Then
97-
Await BackgroundCompact()
112+
If _parseWatchersSemaphore.CurrentCount <> 0 AndAlso (IsBackgroundCompactingEnabled OrElse runAll) Then
113+
Await BackgroundCompact(runAll)
98114
End If
115+
Return True
99116
Finally
100117

101118
AddHandler IdleDetector.IsIdle, _idleHandler
119+
For Each watcher In WatchedFolders
120+
watcher.ResumeMonitoring()
121+
Next
102122
End Try
103-
End Sub
123+
Return False
124+
End Function
125+
126+
104127

105128
Private Sub OnSystemNotIdle(sender As Object, e As EventArgs)
106129
_isSystemIdle = False
@@ -366,7 +389,7 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
366389

367390
End Function
368391

369-
Public Async Function BackgroundCompact() As Task
392+
Public Async Function BackgroundCompact(Optional runAll As Boolean = False) As Task
370393

371394
Dim acquired = Await _parseWatchersSemaphore.WaitAsync(0)
372395
If Not acquired Then Return
@@ -375,11 +398,22 @@ Partial Public Class Watcher : Inherits ObservableRecipient : Implements IRecipi
375398

376399
If BGCompactor.IsCompactorActive Then Return
377400

378-
If Not WatchedFolders.Any(Function(f) f.DecayPercentage <> 0 AndAlso f.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION) Then
379-
Return
380-
End If
401+
Dim recentThresholdDate As DateTime = DateTime.Now.AddSeconds(-IdleSettings.LastSystemModifiedTimeThresholdSeconds)
402+
403+
Dim foldersToCompress = WatchedFolders.
404+
Where(Function(folder)
405+
Dim eligible = folder.DecayPercentage <> 0 AndAlso folder.CompressionLevel <> WOFCompressionAlgorithm.NO_COMPRESSION
406+
Dim recentlyModified = folder.LastSystemModifiedDate > recentThresholdDate AndAlso Not runAll
407+
If eligible AndAlso recentlyModified Then
408+
WatcherLog.SkippingRecentlyModifiedFolder(_logger, folder.DisplayName)
409+
End If
410+
Return eligible AndAlso Not recentlyModified
411+
End Function)
412+
413+
If foldersToCompress.Any = 0 Then Return
414+
415+
Await BGCompactor.StartCompactingAsync(foldersToCompress)
381416

382-
Await BGCompactor.StartCompactingAsync(WatchedFolders)
383417
OnPropertyChanged(NameOf(TotalSaved))
384418
Finally
385419
_parseWatchersSemaphore.Release()

CompactGUI/Views/Components/FolderWatcherCard.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
Visibility="{Binding RefreshWatchedCommand.IsRunning, Converter={StaticResource BoolToVisConverter}}" />
8686

8787
<Button Content="Cancel" Margin="-100" Command="{Binding Watcher.CancelBackgroundingCommand}"/>
88+
<Button Content="Run Now" Margin="-200" Command="{Binding Watcher.RunWatcherCommand}"/>
8889
</Grid>
8990

9091

0 commit comments

Comments
 (0)